73#ifndef MFEM_USE_SUNDIALS
74#error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES
84 AdvDiffSUNDIALS(
int ydot_dim,
int ybdot_dim,
Vector p,
88 ess_tdof_list(ess_tdof),
91 M_solver(fes->GetComm())
95 cout <<
"Essential tdofs: " << endl;
96 ess_tdof_list.
Print();
147 virtual void AdjointRateMult(
const Vector &y,
Vector &yB,
150 virtual int SUNImplicitSetup(
const Vector &y,
151 const Vector &fy,
int jok,
int *jcur,
156 virtual void QuadratureSensitivityMult(
const Vector &y,
const Vector &yB,
199 return x[0]*(2. - x[0])*exp(2.*x[0]);
202int main(
int argc,
char *argv[])
210 int ser_ref_levels = 0;
211 int par_ref_levels = 0;
215 bool step_mode =
true;
218 cout.precision(precision);
222 args.
AddOption(&mx,
"-m",
"--mx",
"The number of mesh elements in the x-dir");
223 args.
AddOption(&ser_ref_levels,
"-r",
"--refine",
224 "Number of times to refine the mesh uniformly.");
225 args.
AddOption(&step_mode,
"-a",
"--adams",
"-no-a",
"--no-adams",
226 "A switch to toggle between CV_ADAMS, and CV_BDF stepping modes");
227 args.
AddOption(&t_final,
"-tf",
"--t-final",
228 "Final time; start time is 0.");
229 args.
AddOption(&dt,
"-dt",
"--time-step",
254 for (
int lev = 0; lev < ser_ref_levels; lev++)
261 for (
int lev = 0; lev < par_ref_levels; lev++)
273 cout <<
"Number of unknowns: " << global_vSize << endl;
286 u.ProjectCoefficient(u0);
288 cout <<
"Init u: " << endl;
297 essential_attr[0] = 1;
298 essential_attr[1] = 1;
302 AdvDiffSUNDIALS adv(U->
Size(), U->
Size(),
p, fes, ess_tdof_list);
310 step_mode ? CV_ADAMS : CV_BDF);
316 real_t reltol = 1e-8, abstol = 1e-6;
320 int checkpoint_steps = 50;
326 for (
int ti = 0; !done; )
328 real_t dt_real = max(dt, t_final -
t);
329 cvodes->
Step(*U,
t, dt_real);
332 done = (
t >= t_final - 1e-8*dt);
334 if (done && myid == 0)
343 cout <<
"Final Solution: " <<
t << endl;
346 cout <<
"u (" << myid <<
"):" << endl;
349 MPI_Barrier(MPI_COMM_WORLD);
361 cout <<
"g: " << g << endl;
382 cvodes->
StepB(*V,
t, dt_real);
385 cout <<
"t: " <<
t << endl;
388 cout <<
"v (" << myid <<
"):" << endl;
391 MPI_Barrier(MPI_COMM_WORLD);
396 MPI_Barrier(MPI_COMM_WORLD);
399 cout <<
"sensitivity:" << endl;
414void AdvDiffSUNDIALS::Mult(
const Vector &x,
Vector &y)
const
420 x1.SetSubVector(ess_tdof_list, 0.0);
429int AdvDiffSUNDIALS::SUNImplicitSetup(
const Vector &y,
431 int jok,
int *jcur,
real_t gamma)
438 Mf =
Add(1., *M, -gamma, *K);
453 solver.SetPreconditioner(prec);
454 solver.SetOperator(*Mf);
455 solver.SetRelTol(1E-14);
456 solver.SetMaxIter(1000);
464void AdvDiffSUNDIALS::AdjointRateMult(
const Vector &y,
Vector & yB,
472 M_solver.
Mult(z, yBdot);
476void AdvDiffSUNDIALS::QuadratureSensitivityMult(
const Vector &y,
520 qBdot[0] = -dp1_result;
521 qBdot[1] = -dp2_result;
real_t u_init(const Vector &x)
int Size() const
Return the logical size of the array.
void Print(std::ostream &out=mfem::out, int width=4) const
Prints array to stream with width elements per row.
Conjugate gradient method.
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
virtual void Mult(const Vector &b, Vector &x) const
Iterative solution of the linear system using the Conjugate Gradient method.
void EvalQuadIntegrationB(double t, Vector &dG_dp)
Evaluate Quadrature solution.
void InitB(TimeDependentAdjointOperator &f_)
Initialize the adjoint problem.
virtual void Step(Vector &x, double &t, double &dt)
void UseSundialsLinearSolverB()
Use built in SUNDIALS Newton solver.
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 SetSStolerancesB(double reltol, double abstol)
Tolerance specification functions for the adjoint problem.
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
void SetMaxNSteps(int steps)
Set the maximum number of time steps.
void PrintInfo() const
Print various CVODE statistics.
void UseSundialsLinearSolver()
Attach SUNDIALS GMRES linear solver to CVODE.
A coefficient that is constant across space and time.
Class for domain integration .
A general function coefficient.
Arbitrary order H1-conforming (continuous) finite elements.
Wrapper for hypre's ParCSR matrix class.
void EliminateRowsCols(const Array< int > &rows_cols, const HypreParVector &X, HypreParVector &B)
HYPRE_Int Mult(HypreParVector &x, HypreParVector &y, real_t alpha=1.0, real_t beta=0.0) const
Computes y = alpha * A * x + beta * y.
Wrapper for hypre's parallel vector class.
void Print(const char *fname) const
Prints the locally owned rows in parallel.
Parallel smoothers in hypre.
void SetType(HypreSmoother::Type type, int relax_times=1)
Set the relaxation type and number of sweeps.
static void Init()
Initialize hypre by calling HYPRE_Init() and set default options. After calling Hypre::Init(),...
void SetRelTol(real_t rtol)
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
virtual void SetPrintLevel(int print_lvl)
Legacy method to set the level of verbosity of the solver output.
void SetMaxIter(int max_it)
void SetAbsTol(real_t atol)
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
static Mesh MakeCartesian1D(int n, real_t sx=1.0)
Creates 1D mesh, divided into n equal intervals.
void Clear()
Clear the contents of the Mesh.
int SpaceDimension() const
Dimension of the physical space containing the mesh.
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
static int WorldRank()
Return the MPI rank in MPI_COMM_WORLD.
static void Init(int &argc, char **&argv, int required=default_thread_required, int *provided=nullptr)
Singleton creation with Mpi::Init(argc, argv).
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.
Abstract parallel finite element space.
void GetEssentialTrueDofs(const Array< int > &bdr_attr_is_ess, Array< int > &ess_tdof_list, int component=-1) const override
HYPRE_BigInt GlobalTrueVSize() const
ParMesh * GetParMesh() const
Class for parallel grid function.
HypreParVector * GetTrueDofs() const
Returns the true dofs in a new HypreParVector.
Class for parallel meshes.
virtual void SetTime(const real_t t_)
Set the current time.
Vector coefficient that is constant in space and time.
void Print(std::ostream &out=mfem::out, int width=8) const
Prints vector to stream out.
void SetSubVector(const Array< int > &dofs, const real_t value)
Set the entries listed in dofs to the given value.
int Size() const
Returns the size of the vector.
real_t u(const Vector &xvec)
real_t InnerProduct(HypreParVector *x, HypreParVector *y)
void Add(const DenseMatrix &A, const DenseMatrix &B, real_t alpha, DenseMatrix &C)
C = A + alpha*B.
real_t p(const Vector &x, real_t t)