43 #ifndef MFEM_USE_SUNDIALS
44 #error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES
66 class DG_Solver :
public Solver
80 linear_solver(M.GetComm()),
82 BlockILU::Reordering::MINIMUM_DISCARDED_FILL),
85 linear_solver.iterative_mode =
false;
86 linear_solver.SetRelTol(1e-9);
87 linear_solver.SetAbsTol(0.0);
88 linear_solver.SetMaxIter(100);
89 linear_solver.SetPrintLevel(0);
90 linear_solver.SetPreconditioner(prec);
95 void SetTimeStep(
double dt_)
102 A =
Add(-dt, K, 0.0, K);
105 A_diag.
Add(1.0, M_diag);
107 linear_solver.SetOperator(*A);
111 void SetOperator(
const Operator &op)
113 linear_solver.SetOperator(op);
118 linear_solver.Mult(x, y);
139 DG_Solver *dg_solver;
147 virtual void ImplicitSolve(
const double dt,
const Vector &x,
Vector &k);
153 int main(
int argc,
char *argv[])
157 MPI_Init(&argc, &argv);
158 MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
159 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
163 const char *mesh_file =
"../../data/periodic-hexagon.mesh";
164 int ser_ref_levels = 2;
165 int par_ref_levels = 0;
170 const char *device_config =
"cpu";
171 int ode_solver_type = 7;
172 double t_final = 10.0;
174 bool visualization =
false;
176 bool paraview =
false;
182 const double reltol = 1e-2, abstol = 1e-2;
185 cout.precision(precision);
188 args.
AddOption(&mesh_file,
"-m",
"--mesh",
189 "Mesh file to use.");
191 "Problem setup to use. See options in velocity_function().");
192 args.
AddOption(&ser_ref_levels,
"-rs",
"--refine-serial",
193 "Number of times to refine the mesh uniformly in serial.");
194 args.
AddOption(&par_ref_levels,
"-rp",
"--refine-parallel",
195 "Number of times to refine the mesh uniformly in parallel.");
197 "Order (degree) of the finite elements.");
198 args.
AddOption(&pa,
"-pa",
"--partial-assembly",
"-no-pa",
199 "--no-partial-assembly",
"Enable Partial Assembly.");
200 args.
AddOption(&ea,
"-ea",
"--element-assembly",
"-no-ea",
201 "--no-element-assembly",
"Enable Element Assembly.");
202 args.
AddOption(&fa,
"-fa",
"--full-assembly",
"-no-fa",
203 "--no-full-assembly",
"Enable Full Assembly.");
204 args.
AddOption(&device_config,
"-d",
"--device",
205 "Device configuration string, see Device::Configure().");
206 args.
AddOption(&ode_solver_type,
"-s",
"--ode-solver",
208 "1 - Forward Euler,\n\t"
213 "7 - CVODE (adaptive order implicit Adams),\n\t"
214 "8 - ARKODE default (4th order) explicit,\n\t"
216 args.
AddOption(&t_final,
"-tf",
"--t-final",
217 "Final time; start time is 0.");
218 args.
AddOption(&dt,
"-dt",
"--time-step",
220 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
221 "--no-visualization",
222 "Enable or disable GLVis visualization.");
223 args.
AddOption(&visit,
"-visit",
"--visit-datafiles",
"-no-visit",
224 "--no-visit-datafiles",
225 "Save data files for VisIt (visit.llnl.gov) visualization.");
226 args.
AddOption(¶view,
"-paraview",
"--paraview-datafiles",
"-no-paraview",
227 "--no-paraview-datafiles",
228 "Save data files for ParaView (paraview.org) visualization.");
229 args.
AddOption(&adios2,
"-adios2",
"--adios2-streams",
"-no-adios2",
230 "--no-adios2-streams",
231 "Save data using adios2 streams.");
232 args.
AddOption(&binary,
"-binary",
"--binary-datafiles",
"-ascii",
234 "Use binary (Sidre) or ascii format for VisIt data files.");
235 args.
AddOption(&vis_steps,
"-vs",
"--visualization-steps",
236 "Visualize every n-th timestep.");
253 if (ode_solver_type < 1 || ode_solver_type > 9)
257 cout <<
"Unknown ODE solver type: " << ode_solver_type <<
'\n';
263 Device device(device_config);
264 if (myid == 0) { device.
Print(); }
268 Mesh *mesh =
new Mesh(mesh_file, 1, 1);
275 for (
int lev = 0; lev < ser_ref_levels; lev++)
290 for (
int lev = 0; lev < par_ref_levels; lev++)
303 cout <<
"Number of unknowns: " << global_vSize << endl;
359 ostringstream mesh_name, sol_name;
360 mesh_name <<
"ex9-mesh." << setfill(
'0') << setw(6) << myid;
361 sol_name <<
"ex9-init." << setfill(
'0') << setw(6) << myid;
362 ofstream omesh(mesh_name.str().c_str());
363 omesh.precision(precision);
365 ofstream osol(sol_name.str().c_str());
366 osol.precision(precision);
377 #ifdef MFEM_USE_SIDRE
380 MFEM_ABORT(
"Must build with MFEM_USE_SIDRE=YES for binary output.");
412 #ifdef MFEM_USE_ADIOS2
416 std::string postfix(mesh_file);
417 postfix.erase(0, std::string(
"../data/").size() );
418 postfix +=
"_o" + std::to_string(order);
419 const std::string collection_name =
"ex9-p-" + postfix +
".bp";
423 adios2_dc->
SetParameter(
"SubStreams", std::to_string(num_procs/2) );
436 sout.
open(vishost, visport);
440 cout <<
"Unable to connect to GLVis server at "
441 << vishost <<
':' << visport << endl;
442 visualization =
false;
445 cout <<
"GLVis visualization disabled.\n";
450 sout <<
"parallel " << num_procs <<
" " << myid <<
"\n";
451 sout.precision(precision);
452 sout <<
"solution\n" << *pmesh << *
u;
456 cout <<
"GLVis visualization paused."
457 <<
" Press space (in the GLVis window) to resume it.\n";
472 switch (ode_solver_type)
475 case 2: ode_solver =
new RK2Solver(1.0);
break;
477 case 4: ode_solver =
new RK4Solver;
break;
478 case 6: ode_solver =
new RK6Solver;
break;
485 ode_solver = cvode;
break;
488 arkode =
new ARKStepSolver(MPI_COMM_WORLD, ARKStepSolver::EXPLICIT);
492 if (ode_solver_type == 9) { arkode->
SetERKTableNum(FEHLBERG_13_7_8); }
493 ode_solver = arkode;
break;
497 if (ode_solver_type < 7) { ode_solver->
Init(adv); }
502 for (
int ti = 0; !done; )
504 double dt_real = min(dt, t_final - t);
505 ode_solver->
Step(*U, t, dt_real);
508 done = (t >= t_final - 1e-8*dt);
510 if (done || ti % vis_steps == 0)
514 cout <<
"time step: " << ti <<
", time: " << t << endl;
525 sout <<
"parallel " << num_procs <<
" " << myid <<
"\n";
526 sout <<
"solution\n" << *pmesh << *u << flush;
543 #ifdef MFEM_USE_ADIOS2
559 ostringstream sol_name;
560 sol_name <<
"ex9-final." << setfill(
'0') << setw(6) << myid;
561 ofstream osol(sol_name.str().c_str());
562 osol.precision(precision);
577 #ifdef MFEM_USE_ADIOS2
595 M_solver(M_.ParFESpace()->GetComm()),
619 dg_solver =
new DG_Solver(M_mat, K_mat, *M_.
FESpace());
639 dg_solver->SetTimeStep(dt);
640 dg_solver->Mult(z, k);
665 for (
int i = 0; i <
dim; i++)
678 case 1: v(0) = 1.0;
break;
679 case 2: v(0) = sqrt(2./3.); v(1) = sqrt(1./3.);
break;
680 case 3: v(0) = sqrt(3./6.); v(1) = sqrt(2./6.); v(2) = sqrt(1./6.);
689 const double w = M_PI/2;
692 case 1: v(0) = 1.0;
break;
693 case 2: v(0) = w*X(1); v(1) = -w*X(0);
break;
694 case 3: v(0) = w*X(1); v(1) = -w*X(0); v(2) = 0.0;
break;
701 const double w = M_PI/2;
702 double d = max((X(0)+1.)*(1.-X(0)),0.) * max((X(1)+1.)*(1.-X(1)),0.);
706 case 1: v(0) = 1.0;
break;
707 case 2: v(0) = d*w*X(1); v(1) = -d*w*X(0);
break;
708 case 3: v(0) = d*w*X(1); v(1) = -d*w*X(0); v(2) = 0.0;
break;
722 for (
int i = 0; i <
dim; i++)
736 return exp(-40.*pow(X(0)-0.5,2));
740 double rx = 0.45, ry = 0.25, cx = 0., cy = -0.2, w = 10.;
743 const double s = (1. + 0.25*cos(2*M_PI*X(2)));
747 return ( erfc(w*(X(0)-cx-rx))*erfc(-w*(X(0)-cx+rx)) *
748 erfc(w*(X(1)-cy-ry))*erfc(-w*(X(1)-cy+ry)) )/16;
754 double x_ = X(0), y_ = X(1), rho, phi;
757 return pow(sin(M_PI*rho),2)*sin(3*phi);
761 const double f = M_PI;
762 return sin(f*X(0))*sin(f*X(1));
void SetPrecision(int prec)
Set the precision (number of digits) used for the text output of doubles.
void Init(TimeDependentOperator &f_)
Initialize CVODE: calls CVodeCreate() to create the CVODE memory and set some defaults.
void Add(const int i, const int j, const double a)
Conjugate gradient method.
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
void SetDataFormat(VTKFormat fmt)
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Helper class for ParaView visualization data.
Base abstract class for first order time dependent operators.
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
void GetBoundingBox(Vector &min, Vector &max, int ref=2)
Returns the minimum and maximum corners of the mesh bounding box.
Pointer to an Operator of a specified type.
virtual void Step(Vector &x, double &t, double &dt)=0
Perform a time step from time t [in] to time t [out] based on the requested step size dt [in]...
HYPRE_BigInt GlobalTrueVSize() const
int Size() const
Returns the size of the vector.
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
virtual void Save(std::ostream &out) const
void SetMaxStep(double dt_max)
Set the maximum time step.
void Print(std::ostream &out=mfem::out)
Print the configuration of the MFEM virtual device object.
virtual void SetTime(const double t_)
Set the current time.
Abstract parallel finite element space.
virtual void ProjectCoefficient(Coefficient &coeff)
Project coeff Coefficient to this GridFunction. The projection computation depends on the choice of t...
bool iterative_mode
If true, use the second argument of Mult() as an initial guess.
Interface to ARKode's ARKStep module – additive Runge-Kutta methods.
Data collection with Sidre routines following the Conduit mesh blueprint specification.
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
void SetPrintLevel(int print_lvl)
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
virtual void Save()
Save the collection to disk.
Interface to the CVODE library – linear multi-step methods.
Jacobi smoothing for a given bilinear form (no matrix necessary).
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Data collection with VisIt I/O routines.
void SetMaxIter(int max_it)
virtual void SetCurvature(int order, bool discont=false, int space_dim=-1, int ordering=1)
void SetMaxStep(double dt_max)
Set the maximum time step.
virtual void Print(std::ostream &out=mfem::out) const
void SetParameter(const std::string key, const std::string value) noexcept
Parallel smoothers in hypre.
void SetHighOrderOutput(bool high_order_output_)
void PrintUsage(std::ostream &out) const
Print the usage message.
void SetTime(double t)
Set physical time (for time-dependent simulations)
A general vector function coefficient.
Wrapper for hypre's parallel vector class.
The classical explicit forth-order Runge-Kutta method, RK4.
void SetAbsTol(double atol)
virtual void ImplicitSolve(const double dt, const Vector &x, Vector &k)
Solve the equation: k = f(x + dt k, t), for the unknown k at the current time t.
void SetRelTol(double rtol)
void velocity_function(const Vector &x, Vector &v)
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
int GetDof() const
Returns the number of degrees of freedom in the finite element.
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...
Third-order, strong stability preserving (SSP) Runge-Kutta method.
virtual void Mult(const Vector &x, Vector &y) const
Perform the action of the operator: y = k = f(x, t), where k solves the algebraic equation F(x...
FE_Evolution(FiniteElementSpace &vfes_, Operator &A_, SparseMatrix &Aflux_)
NURBSExtension * NURBSext
Optional NURBS mesh extension.
void GetDiag(Vector &d) const
Returns the Diagonal of A.
void PrintInfo() const
Print various ARKStep statistics.
virtual const FiniteElement * GetFE(int i) const
Returns pointer to the FiniteElement in the FiniteElementCollection associated with i'th element in t...
void PrintOptions(std::ostream &out) const
Print the options.
HypreParVector * GetTrueDofs() const
Returns the true dofs in a new HypreParVector.
int open(const char hostname[], int port)
Open the socket stream on 'port' at 'hostname'.
double u0_function(const Vector &x)
void SetLevelsOfDetail(int levels_of_detail_)
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
A general function coefficient.
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
virtual void Save() override
void PrintInfo() const
Print various CVODE statistics.
double u(const Vector &xvec)
Class for parallel grid function.
void SetERKTableNum(int table_num)
Choose a specific Butcher table for an explicit RK method.
The MFEM Device class abstracts hardware devices such as GPUs, as well as programming models such as ...
The classical forward Euler method.
Wrapper for hypre's ParCSR matrix class.
void Init(TimeDependentOperator &f_)
Initialize ARKode: calls ARKStepCreate() to create the ARKStep memory and set some defaults...
Class for parallel meshes.
void SetSStolerances(double reltol, double abstol)
Set the scalar relative and scalar absolute tolerances.
virtual void Init(TimeDependentOperator &f_)
Associate a TimeDependentOperator with the ODE solver.
void SetPrefixPath(const std::string &prefix)
Set the path where the DataCollection will be saved.
double inflow_function(const Vector &x)
void UseSundialsLinearSolver()
Attach SUNDIALS GMRES linear solver to CVODE.
Arbitrary order "L2-conforming" discontinuous finite elements.
double f(const Vector &p)
bool Good() const
Return true if the command line options were parsed successfully.