77#ifndef MFEM_USE_SUNDIALS
78#error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES
100 virtual void QuadratureIntegration(
const Vector &x,
Vector &y)
const;
103 virtual void AdjointRateMult(
const Vector &y,
Vector &yB,
107 virtual void QuadratureSensitivityMult(
const Vector &y,
const Vector &yB,
112 virtual int SUNImplicitSetupB(
const real_t t,
const Vector &y,
114 int *jcurB,
real_t gammaB);
119 ~RobertsTDAOperator()
121 delete adjointMatrix;
132int main(
int argc,
char *argv[])
139 const real_t reltol = 1e-4;
142 cout.precision(precision);
145 args.
AddOption(&dt,
"-dt",
"--time-step",
"Time step.");
172 RobertsTDAOperator adv(3,
p);
181 abstol_v[0] = 1.0e-8;
182 abstol_v[1] = 1.0e-14;
183 abstol_v[2] = 1.0e-6;
199 for (
int i = 0; i < y.
Size(); i++)
201 real_t ww = reltol * abs(y[i]) + abstol_v[i];
202 if (ww <= 0.) {
return -1; }
224 for (
int ti = 0; !done; )
226 real_t dt_real = max(dt, t_final -
t);
227 cvodes->
Step(
u,
t, dt_real);
230 done = (
t >= t_final - 1e-8*dt);
238 cout <<
"Final Solution: " <<
t << endl;
242 cout <<
" Final Quadrature " << endl;
262 real_t dt_real = max(dt,
t - TBout1);
263 cvodes->
StepB(w,
t, dt_real);
264 cout <<
"t: " <<
t << endl;
265 cout <<
"w:" << endl;
269 cout <<
"u:" << endl;
273 dt_real = max(dt,
t - 0.);
274 cvodes->
StepB(w,
t, dt_real);
275 cout <<
"t: " <<
t << endl;
276 cout <<
"w:" << endl;
280 cout <<
"u:" << endl;
285 cout <<
"dG/dp:" << endl;
297void RobertsTDAOperator::Mult(
const Vector &x,
Vector &y)
const
299 y[0] = -p_[0]*x[0] + p_[1]*x[1]*x[2];
300 y[2] = p_[2]*x[1]*x[1];
305void RobertsTDAOperator::QuadratureIntegration(
const Vector &y,
312void RobertsTDAOperator::AdjointRateMult(
const Vector &y,
Vector & yB,
315 real_t l21 = (yB[1]-yB[0]);
316 real_t l32 = (yB[2]-yB[1]);
320 yBdot[0] = -p1 * l21;
321 yBdot[1] = p2 * y[2] * l21 - 2. * p3 * y[1] * l32;
322 yBdot[2] = p2 * y[1] * l21 - 1.0;
326void RobertsTDAOperator::QuadratureSensitivityMult(
const Vector &y,
330 real_t l21 = (yB[1]-yB[0]);
331 real_t l32 = (yB[2]-yB[1]);
334 qBdot[0] = y[0] * l21;
335 qBdot[1] = -y23 * l21;
336 qBdot[2] = y[1]*y[1]*l32;
340int RobertsTDAOperator::SUNImplicitSetupB(
const real_t t,
const Vector &y,
342 const Vector &fyB,
int jokB,
343 int *jcurB,
real_t gammaB)
347 delete adjointMatrix;
349 for (
int j = 0; j < y.
Size(); j++)
355 AdjointRateMult(y, yBone, JacBj);
357 for (
int i = 0; i < y.
Size(); i++)
359 adjointMatrix->
Set(i,j, (i == j ? 1.0 : 0.) - gammaB * JacBj[i]);
371int RobertsTDAOperator::SUNImplicitSolveB(
Vector &x,
const Vector &
b,
376 adjointSolver.
Mult(
b, x);
void EvalQuadIntegrationB(double t, Vector &dG_dp)
Evaluate Quadrature solution.
void EvalQuadIntegration(double t, Vector &q)
Evaluate Quadrature.
void SetMaxNStepsB(int mxstepsB)
Set the maximum number of backward steps.
void InitB(TimeDependentAdjointOperator &f_)
Initialize the adjoint problem.
void GetForwardSolution(double tB, mfem::Vector &yy)
Get Interpolated Forward solution y at backward integration time tB.
virtual void Step(Vector &x, double &t, double &dt)
void SetWFTolerances(EWTFunction func)
Set multiplicative error weights.
void Init(TimeDependentAdjointOperator &f_)
virtual void StepB(Vector &w, double &t, double &dt)
Solve one adjoint time step.
void InitQuadIntegrationB(mfem::Vector &qB0, double reltolQB=1e-3, double abstolQB=1e-8)
Initialize Quadrature Integration (Adjoint)
void InitAdjointSolve(int steps, int interpolation)
Initialize Adjoint.
void InitQuadIntegration(mfem::Vector &q0, double reltolQ=1e-3, double abstolQ=1e-8)
Interface to the CVODE library – linear multi-step methods.
void PrintInfo() const
Print various CVODE statistics.
void UseSundialsLinearSolver()
Attach SUNDIALS GMRES linear solver to CVODE.
void SetSVtolerances(double reltol, Vector abstol)
Set the scalar relative and vector of absolute tolerances.
virtual void Mult(const Vector &b, Vector &x) const
Iterative solution of the linear system using the GMRES method.
virtual void SetOperator(const Operator &op) override
Also calls SetOperator for the preconditioner if there is one.
void SetRelTol(real_t rtol)
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set 'var' to receive the value. Enable/disable tags are used to set the bool...
bool Good() const
Return true if the command line options were parsed successfully.
virtual void Finalize(int skip_zeros=1)
Finalize the matrix initialization, switching the storage format from LIL to CSR.
void Set(const int i, const int j, const real_t val)
virtual void SetTime(const real_t t_)
Set the current time.
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
int Size() const
Returns the size of the vector.
real_t u(const Vector &xvec)
real_t p(const Vector &x, real_t t)