41 #ifndef MFEM_USE_PETSC
42 #error This example requires that MFEM is built with MFEM_USE_PETSC=YES
48 class ReducedSystemOperator;
77 ReducedSystemOperator *reduced_oper;
94 double visc,
double mu,
double K,
bool use_petsc);
100 virtual void ImplicitSolve(
const double dt,
const Vector &x,
Vector &k);
107 virtual ~HyperelasticOperator();
114 class ReducedSystemOperator :
public Operator
130 void SetParameters(
double dt_,
const Vector *v_,
const Vector *x_);
138 virtual ~ReducedSystemOperator();
144 class ElasticEnergyCoefficient :
public Coefficient
153 : model(m), x(x_) { }
155 virtual ~ElasticEnergyCoefficient() { }
164 bool init_vis =
false);
167 int main(
int argc,
char *argv[])
171 MPI_Init(&argc, &argv);
172 MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
173 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
176 const char *mesh_file =
"../../data/beam-quad.mesh";
177 int ser_ref_levels = 2;
178 int par_ref_levels = 0;
180 int ode_solver_type = 3;
181 double t_final = 300.0;
186 bool visualization =
true;
188 bool use_petsc =
true;
189 const char *petscrc_file =
"";
192 args.
AddOption(&mesh_file,
"-m",
"--mesh",
193 "Mesh file to use.");
194 args.
AddOption(&ser_ref_levels,
"-rs",
"--refine-serial",
195 "Number of times to refine the mesh uniformly in serial.");
196 args.
AddOption(&par_ref_levels,
"-rp",
"--refine-parallel",
197 "Number of times to refine the mesh uniformly in parallel.");
199 "Order (degree) of the finite elements.");
200 args.
AddOption(&ode_solver_type,
"-s",
"--ode-solver",
201 "ODE solver: 1 - Backward Euler, 2 - SDIRK2, 3 - SDIRK3,\n\t"
202 " 11 - Forward Euler, 12 - RK2,\n\t"
203 " 13 - RK3 SSP, 14 - RK4.");
204 args.
AddOption(&t_final,
"-tf",
"--t-final",
205 "Final time; start time is 0.");
206 args.
AddOption(&dt,
"-dt",
"--time-step",
208 args.
AddOption(&visc,
"-v",
"--viscosity",
209 "Viscosity coefficient.");
210 args.
AddOption(&mu,
"-mu",
"--shear-modulus",
211 "Shear modulus in the Neo-Hookean hyperelastic model.");
212 args.
AddOption(&K,
"-K",
"--bulk-modulus",
213 "Bulk modulus in the Neo-Hookean hyperelastic model.");
214 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
215 "--no-visualization",
216 "Enable or disable GLVis visualization.");
217 args.
AddOption(&vis_steps,
"-vs",
"--visualization-steps",
218 "Visualize every n-th timestep.");
219 args.
AddOption(&use_petsc,
"-usepetsc",
"--usepetsc",
"-no-petsc",
221 "Use or not PETSc to solve the nonlinear system.");
222 args.
AddOption(&petscrc_file,
"-petscopts",
"--petscopts",
223 "PetscOptions file to use.");
242 PetscInitialize(NULL,NULL,petscrc_file,NULL);
248 Mesh *mesh =
new Mesh(mesh_file, 1, 1);
255 switch (ode_solver_type)
263 case 12: ode_solver =
new RK2Solver(0.5);
break;
265 case 14: ode_solver =
new RK4Solver;
break;
273 cout <<
"Unknown ODE solver type: " << ode_solver_type <<
'\n';
282 for (
int lev = 0; lev < ser_ref_levels; lev++)
292 for (
int lev = 0; lev < par_ref_levels; lev++)
309 cout <<
"Number of velocity/deformation unknowns: " << glob_size << endl;
314 true_offset[1] = true_size;
315 true_offset[2] = 2*true_size;
319 v_gf.
MakeTRef(&fespace, vx, true_offset[0]);
320 x_gf.
MakeTRef(&fespace, vx, true_offset[1]);
346 HyperelasticOperator *oper =
new HyperelasticOperator(fespace, ess_bdr, visc,
352 char vishost[] =
"localhost";
354 vis_v.
open(vishost, visport);
356 visualize(vis_v, pmesh, &x_gf, &v_gf,
"Velocity",
true);
360 vis_w.
open(vishost, visport);
363 oper->GetElasticEnergyDensity(x_gf, w_gf);
365 visualize(vis_w, pmesh, &x_gf, &w_gf,
"Elastic energy density",
true);
369 double ee0 = oper->ElasticEnergy(x_gf);
370 double ke0 = oper->KineticEnergy(v_gf);
373 cout <<
"initial elastic energy (EE) = " << ee0 << endl;
374 cout <<
"initial kinetic energy (KE) = " << ke0 << endl;
375 cout <<
"initial total energy (TE) = " << (ee0 + ke0) << endl;
380 ode_solver->
Init(*oper);
384 bool last_step =
false;
385 for (
int ti = 1; !last_step; ti++)
387 double dt_real = min(dt, t_final - t);
389 ode_solver->
Step(vx, t, dt_real);
391 last_step = (t >= t_final - 1e-8*dt);
393 if (last_step || (ti % vis_steps) == 0)
397 double ee = oper->ElasticEnergy(x_gf);
398 double ke = oper->KineticEnergy(v_gf);
402 cout <<
"step " << ti <<
", t = " << t <<
", EE = " << ee
403 <<
", KE = " << ke <<
", ΔTE = " << (ee+ke)-(ee0+ke0) << endl;
411 oper->GetElasticEnergyDensity(x_gf, w_gf);
425 ostringstream mesh_name, velo_name, ee_name;
426 mesh_name <<
"deformed." << setfill(
'0') << setw(6) << myid;
427 velo_name <<
"velocity." << setfill(
'0') << setw(6) << myid;
428 ee_name <<
"elastic_energy." << setfill(
'0') << setw(6) << myid;
430 ofstream mesh_ofs(mesh_name.str().c_str());
431 mesh_ofs.precision(8);
432 pmesh->
Print(mesh_ofs);
434 ofstream velo_ofs(velo_name.str().c_str());
435 velo_ofs.precision(8);
437 ofstream ee_ofs(ee_name.str().c_str());
439 oper->GetElasticEnergyDensity(x_gf, w_gf);
449 if (use_petsc) { PetscFinalize(); }
470 out <<
"solution\n" << *mesh << *field;
476 out <<
"window_size 800 800\n";
477 out <<
"window_title '" << field_name <<
"'\n";
484 out <<
"autoscale value\n";
491 ReducedSystemOperator::ReducedSystemOperator(
494 :
Operator(M_->ParFESpace()->TrueVSize()), M(M_), S(S_), H(H_),
495 Jacobian(NULL), dt(0.0), v(NULL), x(NULL), w(height), z(height),
496 ess_tdof_list(ess_tdof_list_)
499 void ReducedSystemOperator::SetParameters(
double dt_,
const Vector *v_,
502 dt = dt_; v = v_; x = x_;
511 M->TrueAddMult(k, y);
512 S->TrueAddMult(w, y);
516 Operator &ReducedSystemOperator::GetGradient(
const Vector &k)
const
522 localJ->
Add(dt*dt, H->GetLocalGradient(z));
525 Jacobian = M->ParallelAssemble(localJ);
532 ReducedSystemOperator::~ReducedSystemOperator()
540 double mu,
double K,
bool use_petsc)
542 M(&fespace), S(&fespace), H(&fespace),
543 viscosity(visc), M_solver(f.GetComm()),
544 newton_solver(f.GetComm()), pnewton_solver(NULL), z(height/2)
546 const double rel_tol = 1e-8;
547 const int skip_zero_entries = 0;
549 const double ref_density = 1.0;
552 M.Assemble(skip_zero_entries);
553 M.Finalize(skip_zero_entries);
554 Mmat = M.ParallelAssemble();
555 fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
559 M_solver.iterative_mode =
false;
560 M_solver.SetRelTol(rel_tol);
561 M_solver.SetAbsTol(0.0);
562 M_solver.SetMaxIter(30);
563 M_solver.SetPrintLevel(0);
564 M_prec.SetType(HypreSmoother::Jacobi);
565 M_solver.SetPreconditioner(M_prec);
566 M_solver.SetOperator(*Mmat);
570 H.SetEssentialTrueDofs(ess_tdof_list);
574 S.Assemble(skip_zero_entries);
575 S.Finalize(skip_zero_entries);
577 reduced_oper =
new ReducedSystemOperator(&M, &S, &H, ess_tdof_list);
581 J_hypreSmoother->
SetType(HypreSmoother::l1Jacobi);
583 J_prec = J_hypreSmoother;
594 newton_solver.SetSolver(*J_solver);
595 newton_solver.SetOperator(*reduced_oper);
596 newton_solver.SetPrintLevel(1);
597 newton_solver.SetRelTol(rel_tol);
598 newton_solver.SetAbsTol(0.0);
599 newton_solver.SetMaxIter(10);
610 pnewton_solver->SetRelTol(rel_tol);
611 pnewton_solver->SetAbsTol(0.0);
612 pnewton_solver->SetMaxIter(10);
626 if (viscosity != 0.0)
632 M_solver.Mult(z, dv_dt);
637 void HyperelasticOperator::ImplicitSolve(
const double dt,
652 reduced_oper->SetParameters(dt, &v, &x);
656 newton_solver.Mult(zero, dv_dt);
657 MFEM_VERIFY(newton_solver.GetConverged(),
658 "Newton solver did not converge.");
662 pnewton_solver->Mult(zero, dv_dt);
663 MFEM_VERIFY(pnewton_solver->GetConverged(),
664 "Newton solver did not converge.");
666 add(v, dt, dv_dt, dx_dt);
669 double HyperelasticOperator::ElasticEnergy(
const ParGridFunction &x)
const
671 return H.GetEnergy(x);
674 double HyperelasticOperator::KineticEnergy(
const ParGridFunction &v)
const
676 double loc_energy = 0.5*M.InnerProduct(v, v);
678 MPI_Allreduce(&loc_energy, &energy, 1, MPI_DOUBLE, MPI_SUM,
683 void HyperelasticOperator::GetElasticEnergyDensity(
686 ElasticEnergyCoefficient w_coeff(*model, x);
690 HyperelasticOperator::~HyperelasticOperator()
697 delete pnewton_solver;
704 model.SetTransformation(T);
707 return model.EvalW(J)/J.Det();
721 const double s = 0.1/64.;
724 v(dim-1) = s*x(0)*x(0)*(8.0-x(0));
void visualize(ostream &out, Mesh *mesh, GridFunction *deformed_nodes, GridFunction *field, const char *field_name=NULL, bool init_vis=false)
void EliminateRowsCols(const Array< int > &rows_cols, const HypreParVector &X, HypreParVector &B)
void SetSubVector(const Array< int > &dofs, const double value)
Set the entries listed in dofs to the given value.
void SetPrintLevel(int plev)
double Eval(ElementTransformation &T, const IntegrationPoint &ip, double t)
void Add(const int i, const int j, const double a)
void InitialDeformation(const Vector &x, Vector &y)
Conjugate gradient method.
Class for grid function - Vector with associated FE space.
void SetFromTrueVector()
Shortcut for calling SetFromTrueDofs() with GetTrueVector() as argument.
Subclass constant coefficient.
virtual void Init(TimeDependentOperator &f)
Associate a TimeDependentOperator with the ODE solver.
Base abstract class for time dependent operators.
void SwapNodes(GridFunction *&nodes, int &own_nodes_)
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
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]...
Data type dense matrix using column-major storage.
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
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[])
Backward Euler ODE solver. L-stable.
double * GetData() const
Return a pointer to the beginning of the Vector data.
void add(const Vector &v1, const Vector &v2, Vector &v)
void InitialVelocity(const Vector &x, Vector &v)
void SetPositiveDiagonal(bool pos=true)
After computing l1-norms, replace them with their absolute values.
void Add(const DenseMatrix &A, const DenseMatrix &B, double alpha, DenseMatrix &C)
C = A + alpha*B.
void SetTrueVector()
Shortcut for calling GetTrueDofs() with GetTrueVector() as argument.
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
void SetPrintLevel(int print_lvl)
Mesh * GetMesh() const
Returns the mesh.
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
void MakeTRef(FiniteElementSpace *f, double *tv)
Associate a new FiniteElementSpace and new true-dof data with the GridFunction.
void SetMaxIter(int max_it)
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
HYPRE_Int GlobalTrueVSize() const
Newton's method for solving F(x)=b for a given operator F.
virtual void Print(std::ostream &out=mfem::out) const
Parallel smoothers in hypre.
void PrintUsage(std::ostream &out) const
Abstract class for PETSc's nonlinear solvers.
int SpaceDimension() const
The classical explicit forth-order Runge-Kutta method, RK4.
void SetAbsTol(double atol)
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
void SetRelTol(double rtol)
Base class Coefficient that may optionally depend on time.
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.
void GetVectorGradient(ElementTransformation &tr, DenseMatrix &grad) const
Implicit midpoint method. A-stable, not L-stable.
Class for integration point with weight.
void PrintOptions(std::ostream &out) const
Abstract class for hyperelastic models.
int open(const char hostname[], int port)
void GetNodes(Vector &node_coord) const
Arbitrary order H1-conforming (continuous) finite elements.
Class for parallel grid function.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
The classical forward Euler method.
Wrapper for hypre's ParCSR matrix class.
Class for parallel meshes.
void SetType(HypreSmoother::Type type, int relax_times=1)
Set the relaxation type and number of sweeps.
Arbitrary order "L2-conforming" discontinuous finite elements.