45 GeneralResidualMonitor(MPI_Comm comm,
const std::string& prefix_,
50 print_level = print_lvl;
53 MPI_Comm_rank(comm, &rank);
56 print_level = print_lvl;
65 virtual void MonitorResidual(
int it,
double norm,
const Vector &r,
bool final);
68 const std::string prefix;
73 void GeneralResidualMonitor::MonitorResidual(
int it,
double norm,
74 const Vector &r,
bool final)
76 if (print_level == 1 || (print_level == 3 && (
final || it == 0)))
78 mfem::out << prefix <<
" iteration " << setw(2) << it
79 <<
" : ||r|| = " << norm;
82 mfem::out <<
", ||r||/||r_0|| = " << norm/norm0;
107 class JacobianPreconditioner :
public Solver
134 virtual void SetOperator(
const Operator &op);
136 virtual ~JacobianPreconditioner();
143 class RubberOperator :
public Operator
157 GeneralResidualMonitor newton_monitor;
161 GeneralResidualMonitor j_monitor;
174 Array<int> &block_trueOffsets,
double rel_tol,
double abs_tol,
182 void Solve(
Vector &xp)
const;
184 virtual ~RubberOperator();
190 bool init_vis =
false);
197 int main(
int argc,
char *argv[])
199 #ifdef HYPRE_USING_CUDA
200 cout <<
"\nAs of mfem-4.3 and hypre-2.22.0 (July 2021) this example\n"
201 <<
"is NOT supported with the CUDA version of hypre.\n\n";
210 const char *mesh_file =
"../data/beam-tet.mesh";
211 int ser_ref_levels = 0;
212 int par_ref_levels = 0;
214 bool visualization =
true;
215 double newton_rel_tol = 1e-4;
216 double newton_abs_tol = 1e-6;
217 int newton_iter = 500;
221 args.
AddOption(&mesh_file,
"-m",
"--mesh",
222 "Mesh file to use.");
223 args.
AddOption(&ser_ref_levels,
"-rs",
"--refine-serial",
224 "Number of times to refine the mesh uniformly in serial.");
225 args.
AddOption(&par_ref_levels,
"-rp",
"--refine-parallel",
226 "Number of times to refine the mesh uniformly in parallel.");
228 "Order (degree) of the finite elements.");
229 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
230 "--no-visualization",
231 "Enable or disable GLVis visualization.");
232 args.
AddOption(&newton_rel_tol,
"-rel",
"--relative-tolerance",
233 "Relative tolerance for the Newton solve.");
234 args.
AddOption(&newton_abs_tol,
"-abs",
"--absolute-tolerance",
235 "Absolute tolerance for the Newton solve.");
236 args.
AddOption(&newton_iter,
"-it",
"--newton-iterations",
237 "Maximum iterations for the Newton solve.");
238 args.
AddOption(&mu,
"-mu",
"--shear-modulus",
239 "Shear modulus for the neo-Hookean material.");
257 Mesh *mesh =
new Mesh(mesh_file, 1, 1);
263 for (
int lev = 0; lev < ser_ref_levels; lev++)
273 for (
int lev = 0; lev < par_ref_levels; lev++)
291 spaces[0] = &R_space;
292 spaces[1] = &W_space;
308 ess_bdr[0] = &ess_bdr_u;
309 ess_bdr[1] = &ess_bdr_p;
314 std::cout <<
"***********************************************************\n";
315 std::cout <<
"dim(u) = " << glob_R_size <<
"\n";
316 std::cout <<
"dim(p) = " << glob_W_size <<
"\n";
317 std::cout <<
"dim(u+p) = " << glob_R_size + glob_W_size <<
"\n";
318 std::cout <<
"***********************************************************\n";
323 block_trueOffsets[0] = 0;
324 block_trueOffsets[1] = R_space.
TrueVSize();
325 block_trueOffsets[2] = W_space.
TrueVSize();
349 RubberOperator oper(spaces, ess_bdr, block_trueOffsets,
350 newton_rel_tol, newton_abs_tol, newton_iter, c_mu);
368 vis_u.
open(vishost, visport);
370 visualize(vis_u, pmesh, &x_gf, &x_def,
"Deformation",
true);
374 vis_p.
open(vishost, visport);
376 visualize(vis_p, pmesh, &x_gf, &p_gf,
"Pressure",
true);
385 ostringstream mesh_name, pressure_name, deformation_name;
386 mesh_name <<
"mesh." << setfill(
'0') << setw(6) << myid;
387 pressure_name <<
"pressure." << setfill(
'0') << setw(6) << myid;
388 deformation_name <<
"deformation." << setfill(
'0') << setw(6) << myid;
390 ofstream mesh_ofs(mesh_name.str().c_str());
391 mesh_ofs.precision(8);
392 pmesh->
Print(mesh_ofs);
394 ofstream pressure_ofs(pressure_name.str().c_str());
395 pressure_ofs.precision(8);
396 p_gf.
Save(pressure_ofs);
398 ofstream deformation_ofs(deformation_name.str().c_str());
399 deformation_ofs.precision(8);
400 x_def.
Save(deformation_ofs);
414 :
Solver(offsets[2]), block_trueOffsets(offsets), pressure_mass(&mass)
425 mass_prec = mass_prec_amg;
436 mass_pcg = mass_pcg_iter;
448 disp_in.
MakeRef(const_cast<Vector&>(k), block_trueOffsets[0],
449 block_trueOffsets[1]-block_trueOffsets[0]);
451 pres_in.
MakeRef(const_cast<Vector&>(k), block_trueOffsets[1],
452 block_trueOffsets[2]-block_trueOffsets[1]);
455 disp_out.
MakeRef(y, block_trueOffsets[0],
456 block_trueOffsets[1]-block_trueOffsets[0]);
458 pres_out.
MakeRef(y, block_trueOffsets[1],
459 block_trueOffsets[2]-block_trueOffsets[1]);
461 Vector temp(block_trueOffsets[1]-block_trueOffsets[0]);
462 Vector temp2(block_trueOffsets[1]-block_trueOffsets[0]);
465 mass_pcg->Mult(pres_in, pres_out);
468 jacobian->GetBlock(0,1).Mult(pres_out, temp);
471 stiff_pcg->Mult(temp2, disp_out);
477 void JacobianPreconditioner::SetOperator(
const Operator &op)
482 if (stiff_prec == NULL)
487 if (!spaces[0]->GetParMesh()->Nonconforming())
489 #ifndef HYPRE_USING_CUDA
495 stiff_prec = stiff_prec_amg;
505 stiff_pcg = stiff_pcg_iter;
513 JacobianPreconditioner::~JacobianPreconditioner()
529 :
Operator(fes[0]->TrueVSize() + fes[1]->TrueVSize()),
530 newton_solver(fes[0]->GetComm()),
531 newton_monitor(fes[0]->GetComm(),
"Newton", 1),
532 j_monitor(fes[0]->GetComm(),
" GMRES", 3),
533 mu(c_mu), block_trueOffsets(trueOffsets)
547 Hform->SetEssentialBC(ess_bdr, rhs);
559 mass.SetOperatorOwner(
false);
560 pressure_mass = mass.Ptr();
563 JacobianPreconditioner *jac_prec =
564 new JacobianPreconditioner(fes, *pressure_mass, block_trueOffsets);
580 newton_solver.SetSolver(*j_solver);
581 newton_solver.SetOperator(*
this);
582 newton_solver.SetPrintLevel(-1);
583 newton_solver.SetMonitor(newton_monitor);
584 newton_solver.SetRelTol(rel_tol);
585 newton_solver.SetAbsTol(abs_tol);
586 newton_solver.SetMaxIter(iter);
590 void RubberOperator::Solve(
Vector &xp)
const
593 newton_solver.Mult(zero, xp);
594 MFEM_VERIFY(newton_solver.GetConverged(),
595 "Newton Solver did not converge.");
610 RubberOperator::~RubberOperator()
613 delete pressure_mass;
634 out <<
"solution\n" << *mesh << *field;
640 out <<
"window_size 800 800\n";
641 out <<
"window_title '" << field_name <<
"'\n";
648 out <<
"autoscale value\n";
664 y[1] = x[1] + 0.25*x[0];
void visualize(ostream &out, Mesh *mesh, GridFunction *deformed_nodes, GridFunction *field, const char *field_name=NULL, bool init_vis=false)
void InitialDeformation(const Vector &x, Vector &y)
Conjugate gradient method.
void ReferenceConfiguration(const Vector &x, Vector &y)
Class for grid function - Vector with associated FE space.
A class to handle Vectors in a block fashion.
A coefficient that is constant across space and time.
int TrueVSize() const
Obsolete, kept for backward compatibility.
virtual Operator & GetGradient(const Vector &x) const
Evaluate the gradient operator at the point x. The default behavior in class Operator is to generate ...
void SwapNodes(GridFunction *&nodes, int &own_nodes_)
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Pointer to an Operator of a specified type.
void Copy(Array ©) const
Create a copy of the internal array to the provided copy.
HYPRE_BigInt GlobalTrueVSize() const
virtual void Save(std::ostream &out) const
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.
void SyncAliasMemory(const Vector &v) const
Update the alias memory location of the vector to match v.
The BoomerAMG solver in hypre.
A simple convenience class that calls MPI_Init() at construction and MPI_Finalize() at destruction...
void SetPrintLevel(int print_lvl)
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Mesh * GetMesh() const
Returns the mesh.
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
void SetPrintLevel(int print_level)
void SetMaxIter(int max_it)
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Newton's method for solving F(x)=b for a given operator F.
virtual void Print(std::ostream &out=mfem::out) const
void SetElasticityOptions(ParFiniteElementSpace *fespace)
void PrintUsage(std::ostream &out) const
Print the usage message.
A general vector function coefficient.
int SpaceDimension() const
Abstract base class for an iterative solver monitor.
void SetAbsTol(double atol)
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
void SetRelTol(double rtol)
int WorldRank() const
Return MPI_COMM_WORLD's rank.
Base class Coefficients that optionally depend on space and time. These are used by the BilinearFormI...
void subtract(const Vector &x, const Vector &y, Vector &z)
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...
void PartialSum()
Fill the entries of the array with the cumulative sum of the entries.
void Distribute(const Vector *tv)
void SetMonitor(IterativeSolverMonitor &m)
Set the iterative solver monitor.
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'.
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
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...
A class to handle Block systems in a matrix-free implementation.
Class for parallel meshes.
Vector & GetBlock(int i)
Get the i-th vector in the block.
bool Good() const
Return true if the command line options were parsed successfully.