MFEM v4.9.0
Finite element discretization library
Loading...
Searching...
No Matches
mtop_test_iso_elasticity.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11//
12// Sample runs:
13// mpirun -np 4 mtop_test_iso_elasticity -tri -o 2
14// mpirun -np 4 mtop_test_iso_elasticity -tri -o 2 -pa
15// mpirun -np 4 mtop_test_iso_elasticity -tri -o 2 -dfem
16// mpirun -np 4 mtop_test_iso_elasticity -tri -o 3 -dfem
17//
18// mpirun -np 4 mtop_test_iso_elasticity -quad -o 2
19// mpirun -np 4 mtop_test_iso_elasticity -quad -o 2 -pa
20// mpirun -np 4 mtop_test_iso_elasticity -quad -o 2 -dfem
21// mpirun -np 4 mtop_test_iso_elasticity -quad -o 3 -dfem -prl 2
22//
23// Device sample runs:
24// mpirun -np 4 mtop_test_iso_elasticity -d gpu -quad -o 2
25// mpirun -np 4 mtop_test_iso_elasticity -d gpu -quad -o 2 -pa
26// mpirun -np 4 mtop_test_iso_elasticity -d gpu -quad -o 2 -dfem
27// mpirun -np 4 mtop_test_iso_elasticity -d gpu -quad -o 3 -dfem
28
29#include "mtop_solvers.hpp"
30
31using namespace std;
32using namespace mfem;
33
34constexpr auto MESH_TRI = MFEM_SOURCE_DIR "/miniapps/mtop/sq_2D_9_tri.mesh";
35constexpr auto MESH_QUAD = MFEM_SOURCE_DIR "/miniapps/mtop/sq_2D_9_quad.mesh";
36
37int main(int argc, char *argv[])
38{
39 // Initialize MPI and HYPRE.
40 Mpi::Init();
42
43 // Parse command-line options.
44 const char *mesh_file = MESH_QUAD;
45 const char *device_config = "cpu";
46 int order = 2;
47 bool pa = false;
48 bool dfem = false;
49 bool mesh_tri = false;
50 bool mesh_quad = false;
51 int par_ref_levels = 1;
52 bool paraview = false;
53 bool visualization = true;
54
55 OptionsParser args(argc, argv);
56 args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use.");
57 args.AddOption(&device_config, "-d", "--device",
58 "Device configuration string, see Device::Configure().");
59 args.AddOption(&order, "-o", "--order",
60 "Finite element order (polynomial degree) or -1 for"
61 " isoparametric space.");
62 args.AddOption(&pa, "-pa", "--partial-assembly", "-no-pa",
63 "--no-partial-assembly", "Enable Partial Assembly.");
64 args.AddOption(&dfem, "-dfem", "--dFEM", "-no-dfem", "--no-dFEM",
65 "Enable or not dFEM.");
66 args.AddOption(&mesh_tri, "-tri", "--triangular", "-no-tri",
67 "--no-triangular", "Enable or not triangular mesh.");
68 args.AddOption(&mesh_quad, "-quad", "--quadrilateral", "-no-quad",
69 "--no-quadrilateral", "Enable or not quadrilateral mesh.");
70 args.AddOption(&par_ref_levels, "-prl", "--par-ref-levels",
71 "Number of times to refine the mesh uniformly in parallel.");
72 args.AddOption(&paraview, "-pv", "--paraview", "-no-pv", "--no-paraview",
73 "Enable or not Paraview visualization");
74 args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
75 "--no-visualization",
76 "Enable or disable GLVis visualization.");
77 args.ParseCheck();
78 MFEM_VERIFY(!(pa && dfem), "pa and dfem cannot be both set");
79
80 // Enable hardware devices such as GPUs, and programming models such as
81 // CUDA, OCCA, RAJA and OpenMP based on command line options.
82 Device device(device_config);
83 if (Mpi::Root()) { device.Print(); }
84
85 // Read the (serial) mesh from the given mesh file on all processors. We
86 // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
87 // and volume meshes with the same code.
88 Mesh mesh(mesh_tri ? MESH_TRI : mesh_quad ? MESH_QUAD : mesh_file, 1, 1);
89 const int dim = mesh.Dimension();
90
91 // Refine the serial mesh on all processors to increase the resolution. In
92 // this example we do 'ref_levels' of uniform refinement. We choose
93 // 'ref_levels' to be the largest number that gives a final mesh with no
94 // more than 1000 elements.
95 {
96 const int ref_levels =
97 (int)floor(log(1000. / mesh.GetNE()) / log(2.) / dim);
98 for (int l = 0; l < ref_levels; l++) { mesh.UniformRefinement(); }
99 }
100 if (Mpi::Root())
101 {
102 std::cout << "Number of elements: " << mesh.GetNE() << std::endl;
103 }
104
105 // Define a parallel mesh by a partitioning of the serial mesh. Refine
106 // this mesh further in parallel to increase the resolution. Once the
107 // parallel mesh is defined, the serial mesh can be deleted.
108 ParMesh pmesh(MPI_COMM_WORLD, mesh);
109 mesh.Clear();
110 for (int l = 0; l < par_ref_levels; l++) { pmesh.UniformRefinement(); }
111
112 // Create the solver
113 IsoLinElasticSolver elsolver(&pmesh, order, pa, dfem);
114 if (Mpi::Root())
115 {
116 std::cout << "Number of unknowns: "
117 << elsolver.GetSolutionVector().Size() << std::endl;
118 }
119
120 // set boundary conditions
121 elsolver.AddDispBC(2, -1, 0.0);
122 elsolver.AddDispBC(5, -1, 0.0);
123
124 // set material properties
125 ConstantCoefficient E(1.0), nu(0.2);
126 elsolver.SetMaterial(E, nu);
127
128 // set surface load
129 elsolver.AddSurfLoad(1, 0.0, 1.0);
130
131 // set convergence tolerances and max iterations
132 elsolver.SetLinearSolver(1e-6,1e-8,100);
133
134 // assemble the discrete system
135 elsolver.Assemble();
136
137 // solve the system
138 elsolver.FSolve();
139
140 // extract the solution
141 ParGridFunction &sol = elsolver.GetDisplacements();
142
143 if (paraview)
144 {
145 ParaViewDataCollection paraview_dc("isoel", &pmesh);
146 paraview_dc.SetPrefixPath("ParaView");
147 paraview_dc.SetLevelsOfDetail(order);
148 paraview_dc.SetDataFormat(VTKFormat::BINARY);
149 paraview_dc.SetHighOrderOutput(true);
150 paraview_dc.SetCycle(0);
151 paraview_dc.SetTime(0.0);
152 paraview_dc.RegisterField("disp", &sol);
153 paraview_dc.Save();
154 }
155
156 if (socketstream glvis; visualization &&
157 (glvis.open("localhost", 19916), glvis.is_open()))
158 {
159 glvis << "parallel " << Mpi::WorldSize() << " " << Mpi::WorldRank() << "\n";
160 glvis << "solution\n" << pmesh << sol << std::flush;
161 }
162
163 return EXIT_SUCCESS;
164}
The IsoLinElasticSolver class provides a solver for linear isotropic elasticity. The solver provides ...
void AddDispBC(int id, int dir, real_t val)
Adds displacement BC in direction 0(x), 1(y), 2(z), or -1(all).
void FSolve()
Solves the forward problem.
void SetMaterial(mfem::Coefficient &E_, mfem::Coefficient &nu_)
Set material.
mfem::ParGridFunction & GetDisplacements()
Returns the displacements.
void SetLinearSolver(real_t rtol=1e-8, real_t atol=1e-12, int miter=200)
void AddSurfLoad(int id, real_t fx, real_t fy, real_t fz=0.0)
Add surface load.
mfem::Vector & GetSolutionVector()
Returns the solution vector.
virtual void Assemble()
A coefficient that is constant across space and time.
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
void SetTime(real_t t)
Set physical time (for time-dependent simulations)
void SetPrefixPath(const std::string &prefix)
Set the path where the DataCollection will be saved.
The MFEM Device class abstracts hardware devices such as GPUs, as well as programming models such as ...
Definition device.hpp:124
void Print(std::ostream &os=mfem::out)
Print the configuration of the MFEM virtual device object.
Definition device.cpp:317
static void Init()
Initialize hypre by calling HYPRE_Init() and set default options. After calling Hypre::Init(),...
Definition hypre.cpp:33
Mesh data type.
Definition mesh.hpp:65
void Clear()
Clear the contents of the Mesh.
Definition mesh.hpp:827
int GetNE() const
Returns number of elements.
Definition mesh.hpp:1377
int Dimension() const
Dimension of the reference space used within the elements.
Definition mesh.hpp:1306
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition mesh.cpp:11637
static bool Root()
Return true if the rank in MPI_COMM_WORLD is zero.
static int WorldRank()
Return the MPI rank in MPI_COMM_WORLD.
static int WorldSize()
Return the size of 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 ParseCheck(std::ostream &out=mfem::out)
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...
Definition optparser.hpp:82
Class for parallel grid function.
Definition pgridfunc.hpp:50
Class for parallel meshes.
Definition pmesh.hpp:34
void SetLevelsOfDetail(int levels_of_detail_)
Set the refinement level.
void SetHighOrderOutput(bool high_order_output_)
Sets whether or not to output the data as high-order elements (false by default).
void SetDataFormat(VTKFormat fmt)
Set the data format for the ParaView output files.
Writer for ParaView visualization (PVD and VTU format)
int Size() const
Returns the size of the vector.
Definition vector.hpp:234
int dim
Definition ex24.cpp:53
int main()
constexpr auto MESH_TRI
constexpr auto MESH_QUAD