32 #ifndef MFEM_USE_SUNDIALS
33 #error This example requires that MFEM is built with MFEM_USE_SUNDIALS=YES
80 int main(
int argc,
char *argv[])
84 MPI_Init(&argc, &argv);
85 MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
86 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
90 const char *mesh_file =
"../../data/periodic-hexagon.mesh";
91 int ser_ref_levels = 2;
92 int par_ref_levels = 0;
94 int ode_solver_type = 4;
95 double t_final = 10.0;
97 bool visualization =
true;
103 const double reltol = 1e-2, abstol = 1e-2;
106 cout.precision(precision);
109 args.
AddOption(&mesh_file,
"-m",
"--mesh",
110 "Mesh file to use.");
112 "Problem setup to use. See options in velocity_function().");
113 args.
AddOption(&ser_ref_levels,
"-rs",
"--refine-serial",
114 "Number of times to refine the mesh uniformly in serial.");
115 args.
AddOption(&par_ref_levels,
"-rp",
"--refine-parallel",
116 "Number of times to refine the mesh uniformly in parallel.");
118 "Order (degree) of the finite elements.");
119 args.
AddOption(&ode_solver_type,
"-s",
"--ode-solver",
121 "1 - Forward Euler,\n\t"
126 "7 - CVODE (adaptive order implicit Adams),\n\t"
127 "8 - ARKODE default (4th order) explicit,\n\t"
129 args.
AddOption(&t_final,
"-tf",
"--t-final",
130 "Final time; start time is 0.");
131 args.
AddOption(&dt,
"-dt",
"--time-step",
133 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
134 "--no-visualization",
135 "Enable or disable GLVis visualization.");
136 args.
AddOption(&visit,
"-visit",
"--visit-datafiles",
"-no-visit",
137 "--no-visit-datafiles",
138 "Save data files for VisIt (visit.llnl.gov) visualization.");
139 args.
AddOption(&binary,
"-binary",
"--binary-datafiles",
"-ascii",
141 "Use binary (Sidre) or ascii format for VisIt data files.");
142 args.
AddOption(&vis_steps,
"-vs",
"--visualization-steps",
143 "Visualize every n-th timestep.");
159 if (ode_solver_type < 1 || ode_solver_type > 9)
163 cout <<
"Unknown ODE solver type: " << ode_solver_type <<
'\n';
171 Mesh *mesh =
new Mesh(mesh_file, 1, 1);
178 for (
int lev = 0; lev < ser_ref_levels; lev++)
193 for (
int lev = 0; lev < par_ref_levels; lev++)
206 cout <<
"Number of unknowns: " << global_vSize << endl;
248 ostringstream mesh_name, sol_name;
249 mesh_name <<
"ex9-mesh." << setfill(
'0') << setw(6) << myid;
250 sol_name <<
"ex9-init." << setfill(
'0') << setw(6) << myid;
251 ofstream omesh(mesh_name.str().c_str());
252 omesh.precision(precision);
254 ofstream osol(sol_name.str().c_str());
255 osol.precision(precision);
266 #ifdef MFEM_USE_SIDRE
269 MFEM_ABORT(
"Must build with MFEM_USE_SIDRE=YES for binary output.");
286 char vishost[] =
"localhost";
288 sout.
open(vishost, visport);
292 cout <<
"Unable to connect to GLVis server at "
293 << vishost <<
':' << visport << endl;
294 visualization =
false;
297 cout <<
"GLVis visualization disabled.\n";
302 sout <<
"parallel " << num_procs <<
" " << myid <<
"\n";
303 sout.precision(precision);
304 sout <<
"solution\n" << *pmesh << *u;
308 cout <<
"GLVis visualization paused."
309 <<
" Press space (in the GLVis window) to resume it.\n";
324 switch (ode_solver_type)
327 case 2: ode_solver =
new RK2Solver(1.0);
break;
329 case 4: ode_solver =
new RK4Solver;
break;
330 case 6: ode_solver =
new RK6Solver;
break;
337 ode_solver = cvode;
break;
340 arkode =
new ARKStepSolver(MPI_COMM_WORLD, ARKStepSolver::EXPLICIT);
344 if (ode_solver_type == 9) { arkode->
SetERKTableNum(FEHLBERG_13_7_8); }
345 ode_solver = arkode;
break;
349 if (ode_solver_type < 7) { ode_solver->
Init(adv); }
354 for (
int ti = 0; !done; )
356 double dt_real = min(dt, t_final - t);
357 ode_solver->
Step(*U, t, dt_real);
360 done = (t >= t_final - 1e-8*dt);
362 if (done || ti % vis_steps == 0)
366 cout <<
"time step: " << ti <<
", time: " << t << endl;
377 sout <<
"parallel " << num_procs <<
" " << myid <<
"\n";
378 sout <<
"solution\n" << *pmesh << *u << flush;
394 ostringstream sol_name;
395 sol_name <<
"ex9-final." << setfill(
'0') << setw(6) << myid;
396 ofstream osol(sol_name.str().c_str());
397 osol.precision(precision);
424 M(_M), K(_K),
b(_b), M_solver(M.GetComm()), z(_M.Height())
426 M_prec.SetType(HypreSmoother::Jacobi);
453 for (
int i = 0; i <
dim; i++)
466 case 1: v(0) = 1.0;
break;
467 case 2: v(0) = sqrt(2./3.); v(1) = sqrt(1./3.);
break;
468 case 3: v(0) = sqrt(3./6.); v(1) = sqrt(2./6.); v(2) = sqrt(1./6.);
477 const double w = M_PI/2;
480 case 1: v(0) = 1.0;
break;
481 case 2: v(0) = w*X(1); v(1) = -w*X(0);
break;
482 case 3: v(0) = w*X(1); v(1) = -w*X(0); v(2) = 0.0;
break;
489 const double w = M_PI/2;
490 double d = max((X(0)+1.)*(1.-X(0)),0.) * max((X(1)+1.)*(1.-X(1)),0.);
494 case 1: v(0) = 1.0;
break;
495 case 2: v(0) = d*w*X(1); v(1) = -d*w*X(0);
break;
496 case 3: v(0) = d*w*X(1); v(1) = -d*w*X(0); v(2) = 0.0;
break;
510 for (
int i = 0; i <
dim; i++)
524 return exp(-40.*pow(X(0)-0.5,2));
528 double rx = 0.45, ry = 0.25, cx = 0., cy = -0.2, w = 10.;
531 const double s = (1. + 0.25*cos(2*M_PI*X(2)));
535 return ( erfc(w*(X(0)-cx-rx))*erfc(-w*(X(0)-cx+rx)) *
536 erfc(w*(X(1)-cy-ry))*erfc(-w*(X(1)-cy+ry)) )/16;
542 double x_ = X(0), y_ = X(1), rho, phi;
545 return pow(sin(M_PI*rho),2)*sin(3*phi);
549 const double f = M_PI;
550 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.
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.
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
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.
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]...
virtual void Init(TimeDependentOperator &f)
Associate a TimeDependentOperator with the ODE solver.
virtual void SetTime(const double _t)
Set the current time.
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.
Abstract parallel finite element space.
virtual void ProjectCoefficient(Coefficient &coeff)
bool iterative_mode
If true, use the second argument of Mult() as an initial guess.
int main(int argc, char *argv[])
FE_Evolution(FiniteElementSpace &_vfes, Operator &_A, SparseMatrix &_Aflux)
Interface to ARKode's ARKStep module – additive Runge-Kutta methods.
Data collection with Sidre routines following the Conduit mesh blueprint specification.
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
void SetPrintLevel(int print_lvl)
virtual void Save()
Save the collection to disk.
Interface to the CVODE library – linear multi-step methods.
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)
HYPRE_Int GlobalTrueVSize() const
void SetMaxStep(double dt_max)
Set the maximum time step.
virtual void Print(std::ostream &out=mfem::out) const
Parallel smoothers in hypre.
void PrintUsage(std::ostream &out) const
void SetTime(double t)
Set physical time (for time-dependent simulations)
Wrapper for hypre's parallel vector class.
The classical explicit forth-order Runge-Kutta method, RK4.
void SetAbsTol(double atol)
void SetRelTol(double rtol)
void velocity_function(const Vector &x, Vector &v)
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)
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...
NURBSExtension * NURBSext
Optional NURBS mesh extension.
void PrintInfo() const
Print various ARKStep statistics.
void PrintOptions(std::ostream &out) const
HypreParVector * GetTrueDofs() const
Returns the true dofs in a new HypreParVector.
int open(const char hostname[], int port)
double u0_function(const Vector &x)
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
class for C-function coefficient
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
void PrintInfo() const
Print various CVODE statistics.
Class for parallel grid function.
void SetERKTableNum(int table_num)
Choose a specific Butcher table for an explicit RK method.
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.
double inflow_function(const Vector &x)
void UseSundialsLinearSolver()
Attach SUNDIALS GMRES linear solver to CVODE.
Arbitrary order "L2-conforming" discontinuous finite elements.