-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex2.cpp
267 lines (238 loc) · 9.76 KB
/
ex2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// MFEM Example 2
//
// Compile with: make ex2
//
// Sample runs: ex2 -m ../data/beam-tri.mesh
// ex2 -m ../data/beam-quad.mesh
// ex2 -m ../data/beam-tet.mesh
// ex2 -m ../data/beam-hex.mesh
// ex2 -m ../data/beam-wedge.mesh
// ex2 -m ../data/beam-quad.mesh -o 3 -sc
// ex2 -m ../data/beam-quad-nurbs.mesh
// ex2 -m ../data/beam-hex-nurbs.mesh
//
// Description: This example code solves a simple linear elasticity problem
// describing a multi-material cantilever beam.
//
// Specifically, we approximate the weak form of -div(sigma(u))=0
// where sigma(u)=lambda*div(u)*I+mu*(grad*u+u*grad) is the stress
// tensor corresponding to displacement field u, and lambda and mu
// are the material Lame constants. The boundary conditions are
// u=0 on the fixed part of the boundary with attribute 1, and
// sigma(u).n=f on the remainder with f being a constant pull down
// vector on boundary elements with attribute 2, and zero
// otherwise. The geometry of the domain is assumed to be as
// follows:
//
// +----------+----------+
// boundary --->| material | material |<--- boundary
// attribute 1 | 1 | 2 | attribute 2
// (fixed) +----------+----------+ (pull down)
//
// The example demonstrates the use of high-order and NURBS vector
// finite element spaces with the linear elasticity bilinear form,
// meshes with curved elements, and the definition of piece-wise
// constant and vector coefficient objects. Static condensation is
// also illustrated.
//
// We recommend viewing Example 1 before viewing this example.
#include "FEMPlugin.h"
#include "Material.h"
#include "PostProc.h"
#include "mfem.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using namespace mfem;
int main( int argc, char* argv[] )
{
// 1. Parse command-line options.
const char* mesh_file = "/Users/dimiao/repo/mfem_test/tensile.mesh";
int order = 1;
bool static_cond = false;
bool visualization = 1;
int refineLvl = 0;
int localRefineLvl = 0;
OptionsParser args( argc, argv );
args.AddOption( &mesh_file, "-m", "--mesh", "Mesh file to use." );
args.AddOption( &order, "-o", "--order", "Finite element order (polynomial degree)." );
args.AddOption( &static_cond, "-sc", "--static-condensation", "-no-sc", "--no-static-condensation",
"Enable static condensation." );
args.AddOption( &visualization, "-vis", "--visualization", "-no-vis", "--no-visualization",
"Enable or disable GLVis visualization." );
args.AddOption( &refineLvl, "-r", "--refine-level", "Finite element refine level." );
args.AddOption( &localRefineLvl, "-lr", "--local-refine-level", "Finite element local refine level." );
args.Parse();
if ( !args.Good() )
{
args.PrintUsage( cout );
return 1;
}
args.PrintOptions( cout );
cout << static_cond << endl;
// 2. Read the mesh from the given mesh file. We can handle triangular,
// quadrilateral, tetrahedral or hexahedral elements with the same code.
Mesh* mesh = new Mesh( mesh_file, 1, 1 );
int dim = mesh->Dimension();
// 3. Select the order of the finite element discretization space. For NURBS
// meshes, we increase the order by degree elevation.
if ( mesh->NURBSext )
{
mesh->DegreeElevate( order, order );
}
// 4. Mesh refinement
for ( int k = 0; k < refineLvl; k++ )
mesh->UniformRefinement();
for ( int k = 0; k < localRefineLvl; k++ )
{
int ne = mesh->GetNE();
auto eles = mesh->GetElementsArray();
Array<Refinement> refinements;
for ( int i = 0; i < ne; i++ )
{
double* node{ nullptr };
for ( int j = 0; j < eles[i]->GetNVertices(); j++ )
{
const int vi = eles[i]->GetVertices()[j];
node = mesh->GetVertex( vi );
if ( std::abs( node[1] ) < 1e-10 && node[0] + 1e-10 > 0 )
{
refinements.Append( i );
break;
}
}
}
mesh->GeneralRefinement( refinements );
}
ofstream file;
file.open( "refined.vtk" );
mesh->PrintVTK( file );
file.close();
// 5. Define a finite element space on the mesh. Here we use vector finite
// elements, i.e. dim copies of a scalar finite element space. The vector
// dimension is specified by the last argument of the FiniteElementSpace
// constructor. For NURBS meshes, we use the (degree elevated) NURBS space
// associated with the mesh nodes.
FiniteElementCollection* fec;
FiniteElementSpace* fespace;
if ( mesh->NURBSext )
{
fec = NULL;
fespace = mesh->GetNodes()->FESpace();
}
else
{
fec = new H1_FECollection( order, dim );
fespace = new FiniteElementSpace( mesh, fec, dim );
}
cout << "Number of finite element unknowns: " << fespace->GetTrueVSize() << endl << "Assembling: " << flush;
// 6. Determine the list of true (i.e. conforming) essential boundary dofs.
// In this example, the boundary conditions are defined by marking only
// boundary attribute 1 from the mesh as essential and converting it to a
// list of true dofs.
Array<int> ess_tdof_list, ess_bdr( mesh->bdr_attributes.Max() );
ess_bdr = 0;
ess_bdr[0] = 1;
ess_bdr[1] = 1;
fespace->GetEssentialTrueDofs( ess_bdr, ess_tdof_list );
// 7. Set up the linear form b(.) which corresponds to the right-hand side of
// the FEM linear system. In this case, b_i equals the boundary integral
// of f*phi_i where f represents a "pull down" force on the Neumann part
// of the boundary and phi_i are the basis functions in the finite element
// fespace. The force is defined by the VectorArrayCoefficient object f,
// which is a vector of Coefficient objects. The fact that f is non-zero
// on boundary attribute 2 is indicated by the use of piece-wise constants
// coefficient for its last component.
LinearForm* b = new LinearForm( fespace );
cout << "r.h.s. ... " << flush;
b->Assemble();
// 8. Define the solution vector x as a finite element grid function
// corresponding to fespace. Initialize x with initial guess of zero,
// which satisfies the boundary conditions.
GridFunction x( fespace );
x = 0.0;
// 9. Set up the bilinear form a(.,.) on the finite element space
// corresponding to the linear elasticity integrator with piece-wise
// constants coefficient lambda and mu.
Vector Nu( mesh->attributes.Max() );
Nu = .3;
PWConstCoefficient nu_func( Nu );
Vector E( mesh->attributes.Max() );
E = 210e9;
PWConstCoefficient E_func( E );
IsotropicElasticMaterial iem( E_func, nu_func );
BilinearForm* a = new BilinearForm( fespace );
auto intg = plugin::ElasticityIntegrator( iem );
a->AddDomainIntegrator( &intg );
a->Assemble();
Array<int> bdr2( mesh->bdr_attributes.Max() );
bdr2 = 0;
bdr2[1] = 1;
Vector vec( 2 );
vec( 0 ) = .2;
VectorConstantCoefficient vcc( vec );
x.ProjectBdrCoefficient( vcc, bdr2 );
// 10. Assemble the bilinear form and the corresponding linear system,
// applying any necessary transformations such as: eliminating boundary
// conditions, applying conforming constraints for non-conforming AMR,
// static condensation, etc.
cout << "matrix ... " << flush;
if ( static_cond )
{
a->EnableStaticCondensation();
}
a->Assemble();
SparseMatrix A;
Vector B, X;
a->FormLinearSystem( ess_tdof_list, x, *b, A, X, B );
cout << "done." << endl;
cout << "Size of linear system: " << A.Height() << endl;
#ifndef MFEM_USE_SUITESPARSE
// 11. Define a simple symmetric Gauss-Seidel preconditioner and use it to
// solve the system Ax=b with PCG.
GSSmoother M( A );
PCG( A, M, B, X, 1, 500, 1e-8, 0.0 );
#else
// 11. If MFEM was compiled with SuiteSparse, use UMFPACK to solve the system.
UMFPackSolver umf_solver;
umf_solver.Control[UMFPACK_ORDERING] = UMFPACK_ORDERING_METIS;
umf_solver.SetOperator( A );
umf_solver.Mult( B, X );
#endif
// 12. Recover the solution as a finite element grid function.
a->RecoverFEMSolution( X, *b, x );
// 15. Save data in the ParaView format
const char* c = "xyz";
plugin::StressCoefficient stress_c( dim, iem );
stress_c.SetDisplacement( x );
FiniteElementSpace scalar_space( mesh, fec );
ParaViewDataCollection paraview_dc( "test", mesh );
paraview_dc.SetPrefixPath( "ParaView" );
paraview_dc.SetLevelsOfDetail( order );
paraview_dc.SetCycle( 0 );
paraview_dc.SetDataFormat( VTKFormat::BINARY );
paraview_dc.SetHighOrderOutput( true );
paraview_dc.SetTime( 0.0 ); // set the time
paraview_dc.RegisterField( "Displace", &x );
for ( int i = 0; i < dim; i++ )
{
for ( int j = 0; j < dim; j++ )
{
stress_c.SetComponent( i, j );
auto stress = new GridFunction( &scalar_space );
stress->ProjectCoefficient( stress_c );
string x( 1, c[i] );
string y( 1, c[j] );
string name = "S" + x + y;
paraview_dc.RegisterField( name, stress );
}
}
paraview_dc.Save();
if ( fec )
{
delete fespace;
delete fec;
}
delete mesh;
return 0;
}