CEPS  24.01
Cardiac ElectroPhysiology Simulator
AbstractStaticPdeSolver.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
33 
36  m_writeInitialGuess(false)
37 {
38  if (m_doOutput)
39  this->initializeWriter();
40  m_timeWriter = ceps::getNew<TimeWriter>(m_problem->getOutputFileBase()+"_probes.dat",
43 
44  if (pb->getParameters())
46 
48  if (m_doError)
50 
51 
53 }
54 
56 {
57 }
58 
59 void
61 {
63  CEPS_SAYS("Now solving PDE " + m_problem->getProblemName());
64  auto lpb = getStaticProblem();
65  lpb->getInitialGuess(m_solution);
66 
69 
71 
73 
74  if (m_doError)
76 
77  CEPS_SAYS_NOENDL(" Writing result...");
79  CEPS_SAYS_INLINE(" Done");
80 }
81 
82 void
84 {
85  m_writeInitialGuess = params->isActiveOption("output initial guess");
86 }
87 
88 CepsUInt
90 {
91  return m_writeInitialGuess ? 2 : 1;
92 }
93 
94 void
96 {
97  if (m_doOutput)
98  {
99  for (Unknown* u: m_problem->getUnknowns())
100  m_writer->addScalarData(solution,u->getName(),u);
101 
103  {
104  auto uNp1 = m_discretization->newDofHaloVector();
106  m_problem->getAnalyticSolution(), uNp1, 0.0);
107 
108  for (Unknown* u: m_problem->getUnknowns())
109  m_writer->addScalarData(uNp1, "REF " + u->getName(), u);
110 
111  }
112 
113  for (auto [key, f] : *m_problem->getSourceTermManager()->getManager())
114  {
115  auto vec = m_discretization->newDofHaloVector();
116  addFieldValuesTo(*f, vec.get(), 0);
117 
119  m_writer->addScalarData(vec, key + " [" + u->getName() + "]", u);
120  }
121 
122  if (immediateWriting)
123  m_writer->write(0.);
124  // m_writer->writeSolution(m_problem->getSpatialUnknowns(),solution,0);
125  }
126 
128  m_timeWriter->write(0.,solution);
129 }
130 
131 DHVecPtr
133 {
134  return DHVecPtr(ceps::getNew<DistributedHaloVector>(*(m_solution)));
135 }
136 
137 void
139 {
140  m_doError = true;
142  m_errors = ceps::getNew<PdeErrorCalculator>(m_problem);
143 }
144 
147 {
149  if (not m_doError)
150  return res;
151 
152  for (CepsUInt norm=0; norm<3; norm++)
153  {
154  res[0][norm] = m_errors->getAbsoluteErrorNow(norm);
155  res[1][norm] = m_errors->getRelativeErrorNow(norm);
156  }
157  return res;
158 }
159 
160 // -----------------------------------------------------------------------------------------
161 
164 {
165  return ceps::runtimeCast<AbstractStaticPdeProblem*>(m_problem);
166 }
167 
168 void
170 {
171 
172  // Assemble operator matrix terms
173  m_opMat->zero();
174  m_opAsb->setMatrix(m_opMat.get());
175  m_opAsb->setScaleFactor(1.0);
176  m_opAsb->assemble();
177 
178  // Assemble only the matrix terms of the Neumann and Robin BCs contributions
179  m_bcMat->zero();
180  m_bcVec->zero();
181  m_bcAsb->setMatrix(m_bcMat.get());
182  m_bcAsb->setVector(m_bcVec.get());
183  m_bcAsb->setScaleFactor(1.0);
184  m_bcAsb->assemble();
185 
186  // Laplace source terms
187  if (m_hasLapSrc)
188  {
189  m_lapSrcMat->zero();
192  }
193 
194  return;
195 }
#define CEPS_SAYS(message)
Writes a message in the debug log and in the terminal (stdio).
#define CEPS_SAYS_NOENDL(message)
Writes a message in the debug log and in the terminal (stdio).
#define CEPS_SAYS_INLINE(message)
Writes a message in the debug log and in the terminal (stdio).
#define CEPS_SEPARATOR
Writes a separator in the debug log and in the terminal.
CepsArray< _Type, 2U > CepsArray2
C++ array, 2 elements.
Definition: CepsTypes.hpp:162
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
std::shared_ptr< DistributedHaloVector > DHVecPtr
Typedef for pointer on Distributed Halo CepsVector.
void addFieldValuesTo(ScalarField< _SupportType > &f, DistributedVector *vec, CepsReal t=0.)
Add values to a distributed vector. Exclusive to scalar fields. Field data may be evaluated on the fl...
void setMatrix(DistributedMatrix *mat)
The matrix to assemble.
void setScaleFactor(CepsReal scaleFactor)
Factor that multiplies all coefficients to put in linear system.
void setVector(DistributedVector *vec)
The vector to assemble.
virtual void assemble(CepsReal t=0., CepsBool finalize=true)=0
The main routine to call to assemble linear system.
DHVecPtr newDofHaloVector() const
Get a new vector from the factory, with halo data.
void evaluateFunctionOnDofs(ceps::Function< CepsReal(CepsStandardArgs)> *func, DHVecPtr v, CepsReal t=0.) const
Fills vector v with values of function func at owned and halo dofs and time t.
CepsVector< Unknown * > getSpatialUnknowns() const
A vector of all unknowns of pb defined on cells or points.
ScalarFunction * getAnalyticSolution() const
Pointer on analytic or refScalarFunction solution.
CepsBool hasAnalyticSolution() const
Tells if there is an analytic or reference solution.
CepsString getOutputFileBase() const
Output file name includes the directory.
SourceTermManager * getSourceTermManager() const
Get boundary condition manager.
CepsVector< CepsReal3D > getProbePoints() const
Returns points where single data output should be written.
CepsString getProblemName() const
Get the name of the problem.
AbstractDiscretization * getSpatialDiscretization() const
Link to the spatial discretization (FE, FV, etc)
InputParameters * getParameters() const
Text parameters.
const CepsVector< Unknown * > & getUnknowns() const
List of unknowns of the pb.
Base class for PDE solving.
virtual void initializeWriter()
Creates the solution writer.
virtual void initializeAssemblers()=0
Creates the right type of assemblers for LHS and BCs. Needs to be overriden.
VtkWriter * m_writer
Manages output.
AbstractAssembler * m_lapSrcAsb
Assembler for laplace source terms.
virtual void assembleAndSolve()=0
Main routine used during solving, perform one single step on time problem or solve directly in static...
DMatPtr m_bcMat
Matrix of boundary conditions.
AbstractDiscretization * m_discretization
Link to PDE discretization.
AbstractAssembler * m_bcAsb
Assembler for Robin and Neumann BCs.
PdeErrorCalculator * m_errors
Error computation.
CepsBool m_doOutput
Enables/disables outputs.
DMatPtr m_opMat
Matrix of operator.
TimeWriter * m_timeWriter
Writer for time dependant data (even for static problems...)
DVecPtr m_bcVec
Vector of boundary conditions.
AbstractAssembler * m_opAsb
Assembler for the operator matrix.
CepsBool m_hasLapSrc
Flag telling if there are Delta f source terms.
AbstractPdeProblem * m_problem
Link to PDE to solve.
CepsBool m_doError
Compute error wrt analytic solution or reference.
DMatPtr m_lapSrcMat
Matrix used for laplace source terms.
Astract Problem which does not depend on time.
AbstractStaticPdeProblem * getStaticProblem() const
Returns a pointer with the appropriate type of pb.
virtual ~AbstractStaticPdeSolver()
Destructor.
void updateAssemblers() override
Update assemblers and recompute everything is needed.
void enableErrorComputation()
Sets the comparison with analytic solution.
CepsUInt getExpectedNumberOfOutputs() const final
Number of files written.
void solve() override
Solves the whole PDE in time.
DHVecPtr getSolution() const
Returns a copy of the distributed vector containing the current solution.
virtual void output(DHVecPtr solution, CepsBool immediateWriting=true)
Prints the solution and flush the writer if immediateWriting is true.
DHVecPtr m_solution
The actual vector with the solution.
CepsBool m_writeInitialGuess
Flag for outputs.
AbstractStaticPdeSolver(AbstractStaticPdeProblem *pb)
Constructor with problem.
void setupWithParameters(InputParameters *params) override
Set attributes from input file. Parameters are passed as arguments in case one wants to use other par...
CepsArray2< CepsArray3< CepsReal > > getErrors() const
Gets the currently computed errors. First index selects absolute(0) orrelative(1) second index is L-i...
Reads and stores simulation configuration.
CepsBool isActiveOption(const keyType &key) const
Tells if key exists in configuration and is of "1","YES","ON" or "TRUE".
CepsReal getAbsoluteErrorNow(CepsInt px) const
Get norm of difference at current time.
CepsReal getRelativeErrorNow(CepsInt px) const
Get relative norm of difference at current time.
void compute(DHVecPtr num, CepsReal time=0.)
Compute the error at current time and add to total.
Manager *const getManager() const
Get a map of all source terms.
CepsBool hasSomethingToWrite() const
Tells if some data must be written. If not, why bother ?
Definition: TimeWriter.cpp:66
void write(CepsReal t, DHVecPtr data)
Writes the content of data at indices set in constructor.
Definition: TimeWriter.cpp:82
A class used to defined an unknown of a PDE problem The unknown can be defined on a specific region,...
Definition: Unknown.hpp:45
void addScalarData(const DHVecPtr &v, const CepsVector< CepsString > &fieldNames, const CepsVector< Unknown * > &us)
Set multiple scalar fields to be output by this writer. addScalarData for several unknowns.
Definition: VtkWriter.cpp:101
void write(CepsReal time=0., CepsBool writeXmlEntry=true)
Write all stored data.
Definition: VtkWriter.cpp:176
void destroyObject(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:116