CEPS  24.01
Cardiac ElectroPhysiology Simulator
PdeErrorCalculator.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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
32 
33 
35  m_problem (pb),
36  m_solution (nullptr),
37  m_dt (dt),
38  m_refNow ({0,0,0}),
39  m_errNow ({0,0,0}),
40  m_mxTNormsRef({0,0,0}),
41  m_mxTNormsErr({0,0,0}),
42  m_l1TNormsRef({nullptr,nullptr,nullptr}),
43  m_l1TNormsErr({nullptr,nullptr,nullptr}),
44  m_ignoreZeroD(false)
45 {
47  "Passed ptr to problem is null"
48  );
49  setAnalyticSolution(pb->getAnalyticSolution());
50  m_ignoreZeroD = pb->ignoreZeroDUnknownsForError();
51 
52  reset();
53 }
54 
56 {
57  for (auto& itg: m_l1TNormsErr)
59  for (auto& itg: m_l1TNormsRef)
61 }
62 
63 void
65 {
66 
67  // Build a vector with the values of the analytic solution
69  DHVecPtr ana = sd->newDofHaloVector();
70  sd->evaluateFunctionOnDofs(m_solution,ana,time);
71 
72  m_refNow[0] = ana->lInfNorm();
73  m_refNow[1] = sd->l1Norm(ana);
74  m_refNow[2] = sd->l2Norm(ana);
75 
76  for (CepsUInt i=0;i<3;i++)
77  {
79  m_l1TNormsRef[i]->update();
80  }
81 
82  ana->addScaled(*num,-1.0);
83  if (m_ignoreZeroD)
84  {
85  ana->getLocalData();
86  for (Unknown* u : m_problem->getZeroDUnknowns())
87  for (DegreeOfFreedom* dof: sd->getDegreesOfFreedomForUnknown(u))
88  if (dof->getOwner()==ceps::getRank())
89  (*ana)[dof->getGlobalIndex()] = 0.;
90  ana->releaseLocalData();
91  }
92 
93  m_errNow[0] = ana->lInfNorm();
94  m_errNow[1] = sd->l1Norm(ana);
95  m_errNow[2] = sd->l2Norm(ana);
96 
97  for (CepsUInt i=0;i<3;i++)
98  {
100  m_l1TNormsErr[i]->update();
101  }
102 
103  return;
104 }
105 
106 CepsReal
108 {
109  checkPxPt(px,0);
110  return m_refNow[px];
111 }
112 
113 CepsReal
115 {
116  checkPxPt(px,pt);
117  if (pt==1)
118  return m_l1TNormsRef[px]->get();
119  return m_mxTNormsRef[px];
120 }
121 
122 CepsReal
124 {
125  checkPxPt(px,0);
126  return m_errNow[px];
127 }
128 
129 CepsReal
131 {
132  checkPxPt(px,pt);
133  if (pt==1)
134  return m_l1TNormsErr[px]->get();
135  return m_mxTNormsErr[px];
136 }
137 
138 CepsReal
140 {
141  checkPxPt(px,0);
142  CepsReal ref = std::max(1E-08,m_refNow[px]);
143  return m_errNow[px]/ref;
144 }
145 
146 CepsReal
148 {
149  checkPxPt(px,pt);
150  if (pt==1)
151  return m_l1TNormsErr[px]->get()/std::max(1E-08,m_l1TNormsRef[px]->get());
152  return m_mxTNormsErr[px]/std::max(1E-08,m_mxTNormsRef[px]);
153 }
154 
155 void
157 {
159  "Passed ptr on analytic solution or reference solution is null"
160  );
161  m_solution = ceps::runtimeCast<ScalarSAFunc*>(func);
162 }
163 
164 void
166 {
167  m_refNow = m_errNow = m_mxTNormsErr = m_mxTNormsRef = {0.,0.,0.};
168  for (CepsUInt i=0;i<3;i++)
169  {
172  m_l1TNormsErr[i] = ceps::getNew<TimeIntegrator>(&m_errNow[i],m_dt,4);
173  m_l1TNormsRef[i] = ceps::getNew<TimeIntegrator>(&m_refNow[i],m_dt,4);
174  }
175 }
176 
177 void
179 {
180  CEPS_ABORT_IF(px>2 or px<0 or pt>1 or pt<0,
181  "When requesting error in (l-px,l-pt) norm, px and pt must be either" << std::endl <<
182  " 0 for l-infinity norm" << std::endl <<
183  " 1 for l-1 norm" << std::endl <<
184  " 2 for l-2 norm, but not 2 for pt"
185  );
186 }
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
std::shared_ptr< DistributedHaloVector > DHVecPtr
Typedef for pointer on Distributed Halo CepsVector.
DHVecPtr newDofHaloVector() const
Get a new vector from the factory, with halo data.
Base class for creating PDEs to solve.
AbstractDiscretization * getSpatialDiscretization() const
Link to the spatial discretization (FE, FV, etc)
CepsVector< Unknown * > getZeroDUnknowns() const
A vector of all zeroD unknowns of the pb.
A degree of freedom for any kind of problem The dof can be associated to a geometrical element or not...
CepsReal getAbsoluteErrorNow(CepsInt px) const
Get norm of difference at current time.
void setAnalyticSolution(ceps::Function< CepsReal(CepsStandardArgs)> *func)
Manually set the analytic solution.
CepsArray3< CepsReal > m_refNow
Norm of reference at current time.
AbstractPdeProblem * m_problem
Link to problem.
CepsArray3< CepsReal > m_errNow
Norm of difference at current time.
CepsReal m_dt
Time step, 1 by default,.
CepsReal getRelativeErrorNow(CepsInt px) const
Get relative norm of difference at current time.
CepsReal getReferenceNormNow(CepsInt px) const
Get norm of reference at current time. p=0 max, p=1 l1 normm, p=2 l2 norm.
PdeErrorCalculator(AbstractPdeProblem *pb, CepsReal dt=1.)
Constructor with discretization.
CepsArray3< CepsReal > m_mxTNormsRef
max norm in time
CepsBool m_ignoreZeroD
Do not add errors from 0D unknowns.
CepsArray3< TimeIntegrator * > m_l1TNormsRef
Integrator for L1 norm in time.
CepsArray3< TimeIntegrator * > m_l1TNormsErr
Integrator for L1 norm in time.
void reset()
Reset all cumulated norms.
CepsReal getAbsoluteErrorCumulative(CepsInt px, CepsInt pt) const
Get cumulated norm of difference.
~PdeErrorCalculator()
Destructor.
CepsReal getRelativeErrorCumulative(CepsInt px, CepsInt pt) const
Get relative cumulated norm of difference.
void compute(DHVecPtr num, CepsReal time=0.)
Compute the error at current time and add to total.
ScalarSAFunc * m_solution
Analytic function.
CepsArray3< CepsReal > m_mxTNormsErr
max norm in time
void checkPxPt(CepsInt px, CepsInt pt) const
Aborts if pw or pt is not 0,1,2.
CepsReal getReferenceNormCumulative(CepsInt px, CepsInt pt) const
Get cumulated norm of reference so far.
A class used to defined an unknown of a PDE problem The unknown can be defined on a specific region,...
Definition: Unknown.hpp:45
constexpr CepsHash get(_Type const &i)
get a hash from a single value
CepsUInt getRank()
Returns current processor rank.
const _Type & max(const CepsVector< _Type, _Alloc > &vec)
Returns the maximum of the vector, const version.
Definition: CepsVector.hpp:464
CepsBool isNullPtr(_Type *ptr)
Tells if passed pointer is null.
Definition: CepsMemory.hpp:45
void destroyObject(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:116
Structure used to pass arguments to SAFunc (see pde directory) The flags of the SAFunc allows extract...
Definition: CepsTypes.hpp:239
function caller : abstract base, only contains an variadic operator()