-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock.cpp
268 lines (225 loc) · 8.39 KB
/
block.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
268
#include "FEMPlugin.h"
#include "Material.h"
#include "NeoHookeanMaterial.h"
#include "PostProc.h"
#include "mfem.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using namespace mfem;
class GeneralResidualMonitor : public IterativeSolverMonitor
{
public:
GeneralResidualMonitor( const std::string& prefix_, int print_lvl ) : prefix( prefix_ )
{
print_level = print_lvl;
}
virtual void MonitorResidual( int it, double norm, const Vector& r, bool final );
private:
const std::string prefix;
int print_level;
mutable double norm0;
};
void GeneralResidualMonitor::MonitorResidual( int it, double norm, const Vector& r, bool final )
{
if ( print_level == 1 || ( print_level == 3 && ( final || it == 0 ) ) )
{
mfem::out << prefix << " iteration " << setw( 2 ) << it << " : ||r|| = " << norm;
if ( it > 0 )
{
mfem::out << ", ||r||/||r_0|| = " << norm / norm0;
}
else
{
norm0 = norm;
}
mfem::out << '\n';
}
}
void ReferenceConfiguration( const Vector& x, Vector& y )
{
// Set the reference, stress free, configuration
y = x;
}
int main( int argc, char* argv[] )
{
// 1. Parse command-line options.
const char* mesh_file = "../block.mesh";
int order = 1;
bool static_cond = false;
bool visualization = 1;
int refineLvl = 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.Parse();
if ( !args.Good() )
{
args.PrintUsage( cout );
return 1;
}
args.PrintOptions( cout );
// 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();
// // 4. Refine the mesh to increase the resolution. In this example we do
// // 'ref_levels' of uniform refinement. We choose 'ref_levels' to be the
// // largest number that gives a final mesh with no more than 5,000
// // elements.
{
for ( int l = 0; l < refineLvl; l++ )
{
mesh->UniformRefinement();
}
}
// mesh->UniformRefinement();
// 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, Ordering::byVDIM );
}
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, temp_list, ess_bdr( mesh->bdr_attributes.Max() );
ess_bdr = 0;
ess_bdr[0] = 1;
fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 2 );
ess_tdof_list.Append( temp_list );
// ess_bdr = 0;
// ess_bdr[1] = 1;
// fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 1 );
// ess_tdof_list.Append( temp_list );
// ess_bdr = 0;
// ess_bdr[2] = 1;
// fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 0 );
// ess_tdof_list.Append( temp_list );
// ess_bdr = 0;
// ess_bdr[3] = 1;
// fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 0 );
// ess_tdof_list.Append( temp_list );
// ess_bdr = 0;
// ess_bdr[3] = 1;
// fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 1 );
// ess_tdof_list.Append( temp_list );
// ess_bdr = 0;
// ess_bdr[5] = 1;
// fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 0 );
// ess_tdof_list.Append( temp_list );
// ess_bdr = 0;
// ess_bdr[5] = 1;
// fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 1 );
// ess_tdof_list.Append( temp_list );
// 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_gf( fespace );
GridFunction x_ref( fespace );
GridFunction x_def( fespace );
VectorFunctionCoefficient refconfig( dim, ReferenceConfiguration );
x_gf.ProjectCoefficient( refconfig );
x_ref.ProjectCoefficient( refconfig );
VectorArrayCoefficient f( dim );
for ( int i = 0; i < dim; i++ )
{
f.Set( i, new ConstantCoefficient( 0.0 ) );
}
// Vector Nu( mesh->attributes.Max() );
// Nu = .0;
// PWConstCoefficient nu_func( Nu );
// Vector E( mesh->attributes.Max() );
// E = 1.2e6;
// PWConstCoefficient E_func( E );
// IsotropicElasticMaterial iem( E_func, nu_func );
// iem.setLargeDeformation();
Vector Mu( mesh->attributes.Max() );
Mu = 1.61148e6;
// Mu = 80.194e6;
PWConstCoefficient mu_func( Mu );
Vector Lambda( mesh->attributes.Max() );
Lambda = 499.92568e6;
// Lambda = 400889.806e6;
PWConstCoefficient lambda_func( Lambda );
NeoHookeanMaterial nh( mu_func, lambda_func, NeoHookeanType::Poly1 );
plugin::Memorize mm( mesh );
auto intg = new plugin::NonlinearElasticityIntegrator( nh, mm );
NonlinearForm* nlf = new NonlinearForm( fespace );
// {
// nlf->AddDomainIntegrator( new HyperelasticNLFIntegrator( new NeoHookeanModel( 1.5e6, 10e9 ) ) );
// }
nlf->AddDomainIntegrator( intg );
nlf->SetEssentialTrueDofs( ess_tdof_list );
// Vector r;
// r.SetSize(nlf->Height());
// nlf->Mult(x_gf, r);
GeneralResidualMonitor newton_monitor( "Newton", 1 );
GeneralResidualMonitor j_monitor( "GMRES", 3 );
// Set up the Jacobian solver
// Set up the Jacobian solver
auto* j_gmres = new KLUSolver();
auto newton_solver = new NewtonSolver();
// Set the newton solve parameters
newton_solver->iterative_mode = true;
newton_solver->SetSolver( *j_gmres );
newton_solver->SetOperator( *nlf );
newton_solver->SetPrintLevel( -1 );
newton_solver->SetMonitor( newton_monitor );
newton_solver->SetRelTol( 1e-7 );
newton_solver->SetAbsTol( 1e-8 );
newton_solver->SetMaxIter( 20 );
nlf->AddBdrFaceIntegrator( new plugin::NonlinearVectorBoundaryLFIntegrator( f ) );
int steps = 15;
// 15. Save data in the ParaView format
ParaViewDataCollection paraview_dc( "Block Compress", 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_def );
paraview_dc.Save();
for ( int i = 1; i <= steps; i++ )
{
Vector push_force( mesh->bdr_attributes.Max() );
push_force = 0.0;
push_force( 5 ) = -3e6 / steps * i;
// push_force( 5 ) = -4e6 * p / steps * i;
f.Set( 2, new PWConstCoefficient( push_force ) );
Vector zero;
newton_solver->Mult( zero, x_gf );
subtract( x_gf, x_ref, x_def );
paraview_dc.SetCycle( i );
paraview_dc.SetTime( i ); // set the time
paraview_dc.Save();
}
// MFEM_VERIFY( newton_solver->GetConverged(), "Newton Solver did not converge." );
if ( fec )
{
delete fespace;
delete fec;
}
delete newton_solver;
delete mesh;
return 0;
}