-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpetchbuckle.cpp
291 lines (254 loc) · 9.62 KB
/
petchbuckle.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "Plugin.h"
#include "mfem.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using namespace mfem;
class GeneralResidualMonitor : public IterativeSolverMonitor
{
public:
GeneralResidualMonitor( MPI_Comm comm, const std::string& prefix_, int print_lvl ) : prefix( prefix_ )
{
#ifndef MFEM_USE_MPI
print_level = print_lvl;
#else
int rank;
MPI_Comm_rank( comm, &rank );
if ( rank == 0 )
{
print_level = print_lvl;
}
else
{
print_level = -1;
}
#endif
}
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. Initialize MPI.
int num_procs, myid;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &num_procs );
MPI_Comm_rank( MPI_COMM_WORLD, &myid );
// 1. Parse command-line options.
const char* mesh_file = "../../data/3DBeamBuckle.msh";
int order = 1;
bool static_cond = false;
bool visualization = 1;
int ser_ref_levels = -1, par_ref_levels = -1;
double f_temp = 100.;
const char* petscrc_file = "../../data/petscSetting";
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( &ser_ref_levels, "-rs", "--refine-serial",
"Number of times to refine the mesh uniformly in serial." );
args.AddOption( &par_ref_levels, "-rp", "--refine-parallel",
"Number of times to refine the mesh uniformly in parallel." );
args.AddOption( &petscrc_file, "-petscopts", "--petscopts", "PetscOptions file to use." );
args.AddOption( &f_temp, "-ft", "--ftemp", "Final temperature." );
args.Parse();
if ( !args.Good() )
{
if ( myid == 0 )
{
args.PrintUsage( cout );
}
MPI_Finalize();
return 1;
}
if ( myid == 0 )
{
args.PrintOptions( cout );
}
MFEMInitializePetsc( NULL, NULL, petscrc_file, NULL );
// 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();
if ( mesh->bdr_attributes.Max() < 2 )
{
if ( myid == 0 )
cerr << "\nInput mesh should have at least "
<< "two boundary attributes! (See schematic in ex2.cpp)\n"
<< endl;
MPI_Finalize();
return 3;
}
// {
// int ref_levels = ser_ref_levels >= 0 ? ser_ref_levels : (int)floor( log( 1000. / mesh->GetNE() ) / log( 2. )
// / dim ); for ( int l = 0; l < ref_levels; l++ )
// {
// mesh->UniformRefinement();
// }
// }
// 6. Define a parallel mesh by a partitioning of the serial mesh. Refine
// this mesh further in parallel to increase the resolution. Once the
// parallel mesh is defined, the serial mesh can be deleted.
ParMesh* pmesh = new ParMesh( MPI_COMM_WORLD, *mesh );
delete mesh;
{
for ( int l = 0; l < par_ref_levels; l++ )
{
pmesh->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;
ParFiniteElementSpace* fespace;
fec = new H1_FECollection( order, dim );
fespace = new ParFiniteElementSpace( pmesh, fec, dim, Ordering::byVDIM );
HYPRE_BigInt size = fespace->GlobalTrueVSize();
if ( myid == 0 )
{
cout << "Number of finite element unknowns: " << size << endl << "Assembling: " << endl;
}
// 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( pmesh->bdr_attributes.Max() ), temp_list;
ess_bdr = 0;
ess_bdr[28] = 1; // left
fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 0 );
ess_tdof_list.Append( temp_list );
ess_bdr = 0;
ess_bdr[29] = 1; // right
fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 0 );
ess_tdof_list.Append( temp_list );
ess_bdr = 0;
ess_bdr[27] = 1; // bottom
fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 1 );
ess_tdof_list.Append( temp_list );
fespace->GetEssentialTrueDofs( ess_bdr, temp_list, 2 );
ess_tdof_list.Append( temp_list );
if ( myid == 0 )
{
printf( "Mesh is %i dimensional.\n", dim );
printf( "Number of mesh attributes: %i\n", pmesh->attributes.Size() );
printf( "Number of boundary attributes: %i\n", pmesh->bdr_attributes.Size() );
printf( "Max of mesh attributes: %i\n", pmesh->attributes.Max() );
printf( "Max of boundary attributes: %i\n", pmesh->bdr_attributes.Max() );
}
// 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.
ParGridFunction x_ref( fespace );
VectorFunctionCoefficient refconfig( dim, ReferenceConfiguration );
x_ref.ProjectCoefficient( refconfig );
Vector Nu( pmesh->attributes.Max() );
Nu = .0;
PWConstCoefficient nu_func( Nu );
Vector E( pmesh->attributes.Max() );
E = 12.8e9;
PWConstCoefficient E_func( E );
Vector CTE( pmesh->attributes.Max() );
CTE = 23.1e-6;
PWConstCoefficient CTE_func( CTE );
IsotropicElasticThermalMaterial ietm( E_func, nu_func, CTE_func );
ietm.setInitialTemp( 0 );
ietm.setFinalTemp( f_temp );
plugin::Memorize mm( pmesh );
auto intg = new plugin::NonlinearElasticityIntegrator( ietm, mm );
intg->setNonlinear( true );
auto* nlf = new ParNonlinearForm( fespace );
nlf->AddDomainIntegrator( intg );
nlf->SetEssentialTrueDofs( ess_tdof_list );
nlf->SetGradientType( Operator::Type::PETSC_MATAIJ );
// nlf->SetAssemblyLevel( AssemblyLevel::NONE );
GeneralResidualMonitor newton_monitor( fespace->GetComm(), "Newton", 1 );
GeneralResidualMonitor j_monitor( fespace->GetComm(), "GMRES", 3 );
// {
// Vector r( fespace->GetTrueVSize() );
// Vector q( fespace->GetTrueVSize() );
// Vector X( fespace->GetTrueVSize() );
// plugin::SetLambdaToIntegrators( nlf, 1. + 0 );
// x_gf.ParallelProject( X );
// nlf->Mult( X, q );
// plugin::SetLambdaToIntegrators( nlf, 0 );
// nlf->Mult( X, r );
// // q.Print();
// mfem::out << "x: " << InnerProduct( fespace->GetComm(), X, X ) << " q: " << InnerProduct( fespace->GetComm(), q, q )
// << " r: " << InnerProduct( fespace->GetComm(), r, r ) << "\n";
// }
// Set up the Jacobian solver
PetscLinearSolver* petsc = new PetscLinearSolver( fespace->GetComm() );
auto newton_solver = new plugin::Crisfield( fespace->GetComm() );
// Set the newton solve parameters
newton_solver->iterative_mode = true;
newton_solver->SetSolver( *petsc );
newton_solver->SetOperator( *nlf );
newton_solver->SetPrintLevel( -1 );
newton_solver->SetMonitor( newton_monitor );
newton_solver->SetRelTol( 1e-6 );
newton_solver->SetAbsTol( 1e-10 );
newton_solver->SetMaxIter( 6 );
newton_solver->SetDelta( .01 );
newton_solver->SetMaxDelta( 15 );
newton_solver->SetMinDelta( 1e-5 );
newton_solver->SetPhi( 1. );
newton_solver->SetMaxStep( 10000 );
// 15. Save data in the ParaView format
Vector X( fespace->GetTrueVSize() );
X = 0.;
ParaViewDataCollection paraview_dc( "buckling1", pmesh );
paraview_dc.SetPrefixPath( "ParaView" );
paraview_dc.SetLevelsOfDetail( order );
paraview_dc.SetCycle( 0 );
paraview_dc.SetFormat( 1 );
paraview_dc.SetDataFormat( VTKFormat::BINARY );
paraview_dc.SetTime( 0.0 ); // set the time
ParGridFunction u( fespace );
u.Distribute( X );
paraview_dc.RegisterField( "Displace", &u );
newton_solver->SetDataCollection( ¶view_dc );
paraview_dc.Save();
Vector zero;
newton_solver->Mult( zero, u );
if ( fec )
{
delete fespace;
delete fec;
}
delete newton_solver;
delete pmesh;
MFEMFinalizePetsc();
MPI_Finalize();
return 0;
}