/
TestIncompressibleNonlinearElasticitySolver.hpp
1627 lines (1353 loc) · 66.5 KB
/
TestIncompressibleNonlinearElasticitySolver.hpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2005-2024, University of Oxford.
All rights reserved.
University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.
This file is part of Chaste.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Oxford nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TESTINCOMPRESSIBLENONLINEARELASTICITYSOLVER_HPP_
#define TESTINCOMPRESSIBLENONLINEARELASTICITYSOLVER_HPP_
#include <cxxtest/TestSuite.h>
#include "UblasCustomFunctions.hpp"
#include "IncompressibleNonlinearElasticitySolver.hpp"
#include "ExponentialMaterialLaw.hpp"
#include "MooneyRivlinMaterialLaw.hpp"
#include "NashHunterPoleZeroLaw.hpp"
#include "NonlinearElasticityTools.hpp"
#include "CompressibleMooneyRivlinMaterialLaw.hpp"
#include "NumericFileComparison.hpp"
#include "VtkNonlinearElasticitySolutionWriter.hpp"
#include "PetscSetupAndFinalize.hpp"
double MATERIAL_PARAM = 1.0;
double ALPHA = 0.2;
// Body force corresponding to the deformation
// x = X+0.5*alpha*X^2, y=Y/(1+alpha*X), with p=2c
//
// TO TEST THE TIME-DEPENDENCE, THIS REQUIRES THE
// CURRENT TIME TO BE SET TO 1.0
//
c_vector<double,2> MyBodyForce(c_vector<double,2>& rX, double t)
{
assert(rX(0)>=0 && rX(0)<=1 && rX(1)>=0 && rX(1)<=1);
c_vector<double,2> body_force;
double lam = 1+ALPHA*rX(0);
body_force(0) = -2*MATERIAL_PARAM * ALPHA;
body_force(1) = -2*MATERIAL_PARAM * 2*ALPHA*ALPHA*rX(1)/(lam*lam*lam);
// Make sure the time has been passed through to here correctly.
// This function requires t=1 for the test to pass
body_force(0) += (t-1)*5723485;
return body_force;
}
// Surface traction on three sides of a cube, corresponding to
// x = X+0.5*alpha*X^2, y=Y/(1+alpha*X), with p=2c
//
// TO TEST THE TIME-DEPENDENCE, THIS REQUIRES THE
// CURRENT TIME TO BE SET TO 1.0
//
c_vector<double,2> MyTraction(c_vector<double,2>& location, double t)
{
c_vector<double,2> traction = zero_vector<double>(2);
double lam = 1+ALPHA*location(0);
if (fabs(location(0)-1.0) <= DBL_EPSILON) //Right edge
{
traction(0) = 2*MATERIAL_PARAM * (lam - 1.0/lam);
traction(1) = -2*MATERIAL_PARAM * location(1)*ALPHA/(lam*lam);
}
else if (fabs(location(1)) <= DBL_EPSILON) //Bottom edge
{
traction(0) = 2*MATERIAL_PARAM * location(1)*ALPHA/(lam*lam);
traction(1) = -2*MATERIAL_PARAM * (-lam + 1.0/lam);
}
else if (fabs(location(1) - 1.0) <= DBL_EPSILON)//Top edge
{
traction(0) = -2*MATERIAL_PARAM * location(1)*ALPHA/(lam*lam);
traction(1) = 2*MATERIAL_PARAM * (-lam + 1.0/lam);
}
else
{
NEVER_REACHED;
}
// Make sure the time has been passed through to here correctly.
// This function requires t=1 for the test to pass
traction(0) += (t-1.0)*543548;
return traction;
}
// see TestSolveWithPressureBcsOnDeformedSurface()
double MyPressureFunction(double t)
{
// the hardcoded value 1.32317 comes from outputting
// double pressure = (2*c1*(pow(lambda,-1) - lambda*lambda*lambda))/lambda;
return 1.32317 + 100*(t-1.0);
}
class TestIncompressibleNonlinearElasticitySolver : public CxxTest::TestSuite
{
public:
// This is purely for coverage of assembling a 3D system...
void TestAssembleSystem3D()
{
QuadraticMesh<3> mesh;
TrianglesMeshReader<3,3> mesh_reader1("mesh/test/data/3D_Single_tetrahedron_element_quadratic",2,1,false);
mesh.ConstructFromMeshReader(mesh_reader1);
ExponentialMaterialLaw<3> law(2.0, 3.0);
std::vector<unsigned> fixed_nodes;
fixed_nodes.push_back(0);
SolidMechanicsProblemDefinition<3> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
IncompressibleNonlinearElasticitySolver<3> solver(mesh,
problem_defn,
"");
solver.AssembleSystem(true, true);
// cover exception
CompressibleMooneyRivlinMaterialLaw<3> comp_law(2.0,1.0);
problem_defn.SetMaterialLaw(COMPRESSIBLE,&comp_law);
TS_ASSERT_THROWS_THIS(IncompressibleNonlinearElasticitySolver<3> another_solver(mesh,problem_defn,""), "SolidMechanicsProblemDefinition object contains compressible material laws");
}
void TestAssembleSystem()
{
QuadraticMesh<2> mesh(0.5, 1.0, 1.0);
ExponentialMaterialLaw<2> law(2.0, 3.0);
std::vector<unsigned> fixed_nodes;
fixed_nodes.push_back(0);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
problem_defn.SetZeroDisplacementNodes(fixed_nodes);
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"");
///////////////////////////////////////////////////////////////////
// test whether residual vector is currently zero (as
// current solution should have been initialised to u=0, p=p0
///////////////////////////////////////////////////////////////////
solver.AssembleSystem(true, true);
ReplicatableVector rhs_vec0(solver.mResidualVector);
TS_ASSERT_EQUALS( rhs_vec0.GetSize(), 3U*25U);
for (unsigned i=0; i<rhs_vec0.GetSize(); i++)
{
TS_ASSERT_DELTA(rhs_vec0[i], 0.0, 1e-12);
}
///////////////////////////////////////////////////////////////////
// Include pressure-on-deformed-body boundary conditions, which
// add contributions to residual and jacobian
///////////////////////////////////////////////////////////////////
std::vector<BoundaryElement<1,2>*> boundary_elems;
double pressure = 3.0;
for (TetrahedralMesh<2,2>::BoundaryElementIterator iter
= mesh.GetBoundaryElementIteratorBegin();
iter != mesh.GetBoundaryElementIteratorEnd();
++iter)
{
BoundaryElement<1,2>* p_element = *iter;
boundary_elems.push_back(p_element);
}
problem_defn.SetApplyNormalPressureOnDeformedSurface(boundary_elems, pressure);
solver.mLastDampingValue = 1.0;
solver.AssembleSystem(true, true); // See comments in AbstractNonlinearElasticitySolver::ShouldAssembleMatrixTermForPressureOnDeformedBc
ReplicatableVector rhs_vec(solver.mResidualVector);
///////////////////////////////////////////////////////////////////
// compute numerical Jacobian and compare with analytic jacobian
// (about u=0, p=p0)
///////////////////////////////////////////////////////////////////
unsigned num_dofs = rhs_vec.GetSize();
double h = 1e-6;
int lo, hi;
MatGetOwnershipRange(solver.mrJacobianMatrix, &lo, &hi);
for (unsigned j=0; j<num_dofs; j++)
{
solver.rGetCurrentSolution().clear();
solver.FormInitialGuess();
solver.rGetCurrentSolution()[j] += h;
solver.AssembleSystem(true, false);
ReplicatableVector perturbed_rhs( solver.mResidualVector );
for (unsigned i=0; i<num_dofs; i++)
{
if ((lo<=(int)i) && ((int)i<hi))
{
double analytic_matrix_val = PetscMatTools::GetElement(solver.mrJacobianMatrix,i,j);
double numerical_matrix_val = (perturbed_rhs[i] - rhs_vec[i])/h;
if ((fabs(analytic_matrix_val)>1e-6) && (fabs(numerical_matrix_val)>1e-6))
{
// relative error
TS_ASSERT_DELTA( (analytic_matrix_val-numerical_matrix_val)/analytic_matrix_val, 0.0, 1e-2);
}
else
{
// absolute error
TS_ASSERT_DELTA(analytic_matrix_val, numerical_matrix_val, 1e-2);
}
}
}
}
PetscTools::Barrier();
//////////////////////////////////////////////////////////
// compare numerical and analytic jacobians again, this
// time using a non-zero displacement, u=lambda x, v = mu y
// (lambda not equal to 1/nu), p = x*y
//////////////////////////////////////////////////////////
double lambda = 1.2;
double mu = 1.0/1.3;
solver.rGetCurrentSolution().clear();
solver.FormInitialGuess();
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
solver.rGetCurrentSolution()[3*i] = (lambda-1)*mesh.GetNode(i)->rGetLocation()[0];
solver.rGetCurrentSolution()[3*i+1] = (mu-1)*mesh.GetNode(i)->rGetLocation()[1];
// should really be only setting this on vertices..
solver.rGetCurrentSolution()[3*i+2] = mesh.GetNode(i)->rGetLocation()[0]*mesh.GetNode(i)->rGetLocation()[1];
}
solver.AssembleSystem(true, true);
ReplicatableVector rhs_vec2(solver.mResidualVector);
h=1e-8; // needs to be smaller for this one
// check identity block
for (unsigned i=mesh.GetNumVertices(); i<mesh.GetNumNodes(); i++)
{
assert(mesh.GetNode(i)->IsInternal());
unsigned row = 3*i+2;
if ((lo<=(int)row) && ((int)row<hi))
{
for (unsigned col=0; col<num_dofs; col++)
{
double val = PetscMatTools::GetElement(solver.mrJacobianMatrix,row,col);
TS_ASSERT_DELTA(val, (double)(row==col), 1e-12);
}
}
}
// compare with numerical jacobian
for (unsigned j=0; j<num_dofs; j++)
{
solver.rGetCurrentSolution()[j] += h;
solver.AssembleSystem(true, false);
ReplicatableVector perturbed_rhs( solver.mResidualVector );
for (unsigned i=0; i<num_dofs; i++)
{
if ((lo<=(int)i) && ((int)i<hi))
{
double analytic_matrix_val = PetscMatTools::GetElement(solver.mrJacobianMatrix,i,j);
double numerical_matrix_val = (perturbed_rhs[i] - rhs_vec2[i])/h;
if ((fabs(analytic_matrix_val)>1e-6) && (fabs(numerical_matrix_val)>1e-6))
{
// relative error
TS_ASSERT_DELTA( (analytic_matrix_val-numerical_matrix_val)/analytic_matrix_val, 0.0, 1e-2);
}
else
{
// absolute error
TS_ASSERT_DELTA(analytic_matrix_val, numerical_matrix_val, 1e-2);
}
}
}
solver.rGetCurrentSolution()[j] -= h;
}
}
void TestComputeResidualAndGetNormWithBadDeformation()
{
// There are 10 nodes (including internals) which means that the (non)linear system
// can't be distributed over more than 10 processes. This may cause deadlock in
// VecNorm when calculating the norm of the residual at the end of this test.
if (PetscTools::GetNumProcs() > 10u)
{
TS_TRACE("This test is designed for fewer processes!");
return;
}
QuadraticMesh<3> mesh;
TrianglesMeshReader<3,3> mesh_reader1("mesh/test/data/3D_Single_tetrahedron_element_quadratic",2,1,false);
mesh.ConstructFromMeshReader(mesh_reader1);
NashHunterPoleZeroLaw<3> law;
std::vector<unsigned> fixed_nodes;
fixed_nodes.push_back(0);
SolidMechanicsProblemDefinition<3> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
problem_defn.SetZeroDisplacementNodes(fixed_nodes);
IncompressibleNonlinearElasticitySolver<3> solver(mesh,
problem_defn,
"");
// compute the residual norm - should be zero as no force or tractions
TS_ASSERT_DELTA( solver.ComputeResidualAndGetNorm(false), 0.0, 1e-7);
// the change the current solution (=displacement) to correspond to a small stretch
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
for (unsigned j=0; j<3; j++)
{
solver.rGetCurrentSolution()[3*i+j] = 0.01*mesh.GetNode(i)->rGetLocation()[j];
}
}
// compute the residual norm - check computes fine
TS_ASSERT_LESS_THAN( 0.0, solver.ComputeResidualAndGetNorm(false));
// the change the current solution (=displacement) to correspond to a large stretch
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
for (unsigned j=0; j<3; j++)
{
solver.rGetCurrentSolution()[3*i+j] = mesh.GetNode(i)->rGetLocation()[j];
}
}
// compute the residual norm - material law should complain - exception thrown...
TS_ASSERT_THROWS_CONTAINS( solver.ComputeResidualAndGetNorm(false), "strain unacceptably large");
// ..unless we set the allowException parameter to be true, in which case we should get
// infinity returned.
TS_ASSERT_EQUALS( solver.ComputeResidualAndGetNorm(true), DBL_MAX);
}
// A test where the solution should be zero displacement
// It mainly tests that the initial guess was set up correctly to
// the final correct solution, ie u=0, p=zero_strain_pressure (!=0)
void TestWithZeroDisplacement()
{
QuadraticMesh<2> mesh;
TrianglesMeshReader<2,2> mesh_reader("mesh/test/data/square_128_elements_quadratic",2,1,false);
mesh.ConstructFromMeshReader(mesh_reader);
double c1 = 3.0;
MooneyRivlinMaterialLaw<2> mooney_rivlin_law(c1);
std::vector<unsigned> fixed_nodes
= NonlinearElasticityTools<2>::GetNodesByComponentValue(mesh,0,0);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&mooney_rivlin_law);
problem_defn.SetZeroDisplacementNodes(fixed_nodes);
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"");
// for coverage
TS_ASSERT_THROWS_THIS(solver.SetWriteOutput(true),
"Can\'t write output if no output directory was given in constructor");
solver.SetWriteOutput(false);
solver.Solve();
TS_ASSERT_EQUALS(solver.GetNumNewtonIterations(), 0u);
TS_ASSERT_THROWS_CONTAINS(solver.CreateCmguiOutput(), "No output directory was given");
TS_ASSERT_THROWS_CONTAINS(solver.CreateVtkOutput("Displacement"), "No output directory was given");
// get deformed position
std::vector<c_vector<double,2> >& r_deformed_position
= solver.rGetDeformedPosition();
for (unsigned i=0; i<r_deformed_position.size(); i++)
{
TS_ASSERT_DELTA(mesh.GetNode(i)->rGetLocation()[0], r_deformed_position[i](0), 1e-8);
TS_ASSERT_DELTA(mesh.GetNode(i)->rGetLocation()[1], r_deformed_position[i](1), 1e-8);
}
// check the final pressure
std::vector<double>& r_pressures = solver.rGetPressures();
TS_ASSERT_EQUALS(r_pressures.size(), mesh.GetNumNodes());
for (unsigned i=0; i<r_pressures.size(); i++)
{
TS_ASSERT_DELTA(r_pressures[i], 2*c1, 1e-6);
}
}
void TestSettingUpHeterogeneousProblem()
{
// two element quad mesh on the square
QuadraticMesh<2> mesh(1.0, 1.0, 1.0);
MooneyRivlinMaterialLaw<2> law_1(5.0);
MooneyRivlinMaterialLaw<2> law_2(1.0);
std::vector<AbstractMaterialLaw<2>*> laws;
laws.push_back(&law_1);
laws.push_back(&law_2);
std::vector<unsigned> fixed_nodes
= NonlinearElasticityTools<2>::GetNodesByComponentValue(mesh,0,0);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,laws);
problem_defn.SetZeroDisplacementNodes(fixed_nodes);
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"");
// Recall variables in the solution vector are ordered
// [U0 V0 P0 U1 V1 P1 .. Un Vn Pn]
// with P_i a dummy pressure variable if node i is not a vertex
// pressure for node 0 at (0,0) in element 1 (c1=1.0 above)
TS_ASSERT_DELTA(mesh.GetNode(0)->rGetLocation()[0], 0.0, 1e-6);
TS_ASSERT_DELTA(mesh.GetNode(0)->rGetLocation()[1], 0.0, 1e-6);
TS_ASSERT_DELTA(solver.rGetCurrentSolution()[2], 2.0, 1e-6); //0*3+2
// pressure for node 3 at (1,1) in element 0 (c1=5.0 above)
TS_ASSERT_DELTA(mesh.GetNode(3)->rGetLocation()[0], 1.0, 1e-6);
TS_ASSERT_DELTA(mesh.GetNode(3)->rGetLocation()[1], 1.0, 1e-6);
TS_ASSERT_DELTA(solver.rGetCurrentSolution()[11], 10.0, 1e-6); //3*3+2
}
void TestSolve()
{
QuadraticMesh<2> mesh;
TrianglesMeshReader<2,2> mesh_reader("mesh/test/data/square_128_elements_quadratic",2,1,false);
mesh.ConstructFromMeshReader(mesh_reader);
MooneyRivlinMaterialLaw<2> law(1.0);
c_vector<double,2> body_force;
body_force(0) = 3.0;
body_force(1) = 0.0;
std::vector<unsigned> fixed_nodes
= NonlinearElasticityTools<2>::GetNodesByComponentValue(mesh,0,0);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
problem_defn.SetZeroDisplacementNodes(fixed_nodes);
problem_defn.SetBodyForce(body_force);
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"simple_nonlin_elas");
solver.Solve();
TS_ASSERT_EQUALS(solver.GetNumNewtonIterations(), 4u); // 'hardcoded' answer, protects against Jacobian getting messed up
std::vector<c_vector<double,2> >& r_solution = solver.rGetDeformedPosition();
double xend = 1.17199;
double yend = 0.01001;
///////////////////////////////////////////////////////////
// compare the solution at the corners with the values
// obtained using the dealii finite elasticity solver
//
// Results have been visually checked to see they agree
// (they do, virtually or completely overlapping.
///////////////////////////////////////////////////////////
// bottom lhs corner should still be at (0,0)
assert( fabs(mesh.GetNode(0)->rGetLocation()[0] - 0) < 1e-9 );
assert( fabs(mesh.GetNode(0)->rGetLocation()[1] - 0) < 1e-9 );
TS_ASSERT_DELTA( r_solution[0](0), 0.0, 1e-6 );
TS_ASSERT_DELTA( r_solution[0](1), 0.0, 1e-6 );
// top lhs corner should still be at (0,1)
assert( fabs(mesh.GetNode(3)->rGetLocation()[0] - 0) < 1e-9 );
assert( fabs(mesh.GetNode(3)->rGetLocation()[1] - 1) < 1e-9 );
TS_ASSERT_DELTA( r_solution[3](0), 0.0, 1e-6 );
TS_ASSERT_DELTA( r_solution[3](1), 1.0, 1e-6 );
// DEALII value for bottom rhs corner is (1.17199,0.01001)
assert( fabs(mesh.GetNode(1)->rGetLocation()[0] - 1) < 1e-9 );
assert( fabs(mesh.GetNode(1)->rGetLocation()[1] - 0) < 1e-9 );
TS_ASSERT_DELTA( r_solution[1](0), xend, 1e-3 );
TS_ASSERT_DELTA( r_solution[1](1), yend, 1e-3 );
// DEALII value for top rhs corner is (1.17199,0.98999)
assert( fabs(mesh.GetNode(2)->rGetLocation()[0] - 1) < 1e-9 );
assert( fabs(mesh.GetNode(2)->rGetLocation()[1] - 1) < 1e-9 );
TS_ASSERT_DELTA( r_solution[2](0), xend, 1e-3 );
TS_ASSERT_DELTA( r_solution[2](1), 1-yend, 1e-3 );
MechanicsEventHandler::Headings();
MechanicsEventHandler::Report();
}
/**
* Solve a problem with non-zero dirichlet boundary conditions
* and non-zero tractions. THIS TEST COMPARES AGAINST AN EXACT SOLUTION.
*
* Choosing the deformation x=X/lambda, y=lambda*Y, with a
* Mooney-Rivlin material, then
* F = [1/lam 0; 0 lam], T = [2*c1-p*lam^2, 0; 0, 2*c1-p/lam^2],
* sigma = [2*c1/lam^2-p, 0; 0, 2*c1*lam^2-p].
* Choosing p=2*c1*lam^2, then sigma = [2*c1/lam^2-p 0; 0 0].
* The surface tractions are then
* TOP and BOTTOM SURFACE: 0
* RHS: s = SN = J*invF*sigma*N = [lam 0; 0 1/lam]*sigma*[1,0]
* = [2*c1(1/lam-lam^3), 0]
*
* So, we have to specify displacement boundary conditions (y=lam*Y) on
* the LHS (X=0), and traction bcs (s=the above) on the RHS (X=1), and can
* compare the computed displacement and pressure against the true solution.
*
*/
void TestSolveWithNonZeroBoundaryConditions()
{
double lambda = 0.85;
double c1 = 1.0;
unsigned num_elem = 5;
QuadraticMesh<2> mesh(1.0/num_elem, 1.0, 1.0);
// TrianglesMeshReader<2,2> reader("mesh/test/data/square_128_elements_quadratic_reordered",2,1,false);
// mesh.ConstructFromMeshReader(reader);
MooneyRivlinMaterialLaw<2> law(c1);
std::vector<unsigned> fixed_nodes;
std::vector<c_vector<double,2> > locations;
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
if (fabs(mesh.GetNode(i)->rGetLocation()[0]) < 1e-6)
{
fixed_nodes.push_back(i);
c_vector<double,2> new_position;
new_position(0) = 0;
new_position(1) = lambda*mesh.GetNode(i)->rGetLocation()[1];
locations.push_back(new_position);
}
}
std::vector<BoundaryElement<1,2>*> boundary_elems;
std::vector<c_vector<double,2> > tractions;
c_vector<double,2> traction;
traction(0) = 2*c1*(pow(lambda,-1) - lambda*lambda*lambda);
traction(1) = 0;
for (TetrahedralMesh<2,2>::BoundaryElementIterator iter
= mesh.GetBoundaryElementIteratorBegin();
iter != mesh.GetBoundaryElementIteratorEnd();
++iter)
{
if (fabs((*iter)->CalculateCentroid()[0] - 1.0)<1e-4)
{
BoundaryElement<1,2>* p_element = *iter;
boundary_elems.push_back(p_element);
tractions.push_back(traction);
}
}
assert(boundary_elems.size()==num_elem);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
problem_defn.SetFixedNodes(fixed_nodes, locations);
problem_defn.SetTractionBoundaryConditions(boundary_elems, tractions);
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"nonlin_elas_non_zero_bcs");
/////////////////////////////////////////////////////////////////
// Provide the exact solution as the initial guess and check
// residual is (nearly) exactly zero (as the solution is in
// the FEM space, the FEM solution, assuming the nonlinear
// system was solved exactly, is the exact solution
/////////////////////////////////////////////////////////////////
std::vector<double> old_current_soln = solver.rGetCurrentSolution();
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
double exact_x = (1.0/lambda)*mesh.GetNode(i)->rGetLocation()[0];
double exact_y = lambda*mesh.GetNode(i)->rGetLocation()[1];
solver.rGetCurrentSolution()[3*i] = exact_x - mesh.GetNode(i)->rGetLocation()[0];
solver.rGetCurrentSolution()[3*i+1] = exact_y - mesh.GetNode(i)->rGetLocation()[1];
if (mesh.GetNode(i)->IsInternal())
{
solver.rGetCurrentSolution()[3*i+2] = 0.0;
}
else
{
solver.rGetCurrentSolution()[3*i+2] = 2*c1*lambda*lambda;
}
}
/* HOW_TO_TAG Continuum mechanics
* Get or output stresses during a solve
*/
// get the solver to save the stresses on each element (averaged over quad point stresses)
solver.SetComputeAverageStressPerElementDuringSolve();
solver.Solve();
TS_ASSERT_EQUALS(solver.GetNumNewtonIterations(), 0u); // initial guess was solution
// test stresses. The 1st PK stress should satisfy S = [s(0) 0 ; 0 0], where s is the
// applied traction. This has to be multiplied by F^{-T} to get the 2nd PK stress.
assert(solver.mAverageStressesPerElement.size()==mesh.GetNumElements()); //Will fail when we move to DistributedQuadraticMesh
for (unsigned i=0; i<mesh.GetNumElements(); i++)
{
if (mesh.CalculateDesignatedOwnershipOfElement(i))
{
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(0,0), lambda*traction(0), 1e-8);
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(1,0), 0.0, 1e-8);
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(0,1), 0.0, 1e-8);
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(1,1), 0.0, 1e-8);
}
}
///////////////////////////////////////////////////////////////////////////
// Now solve properly
///////////////////////////////////////////////////////////////////////////
solver.rGetCurrentSolution() = old_current_soln;
// coverage
solver.SetKspAbsoluteTolerance(1e-10);
solver.Solve();
// write the stresses
solver.WriteCurrentAverageElementStresses("solution");
TS_ASSERT_EQUALS(solver.GetNumNewtonIterations(), 3u); // 'hardcoded' answer, protects against Jacobian getting messed up
std::vector<c_vector<double,2> >& r_solution = solver.rGetDeformedPosition();
for (unsigned i=0; i<fixed_nodes.size(); i++)
{
unsigned index = fixed_nodes[i];
TS_ASSERT_DELTA(r_solution[index](0), locations[i](0), 1e-8);
TS_ASSERT_DELTA(r_solution[index](1), locations[i](1), 1e-8);
}
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
double exact_x = (1.0/lambda)*mesh.GetNode(i)->rGetLocation()[0];
double exact_y = lambda*mesh.GetNode(i)->rGetLocation()[1];
TS_ASSERT_DELTA( r_solution[i](0), exact_x, 1e-5 );
TS_ASSERT_DELTA( r_solution[i](1), exact_y, 1e-5 );
}
std::vector<double>& r_pressures = solver.rGetPressures();
TS_ASSERT_EQUALS(r_pressures.size(), mesh.GetNumNodes());
for (unsigned i=0; i<r_pressures.size(); i++)
{
TS_ASSERT_DELTA(r_pressures[i], 2*c1*lambda*lambda, 1e-5);
}
for (unsigned i=0; i<mesh.GetNumElements(); i++)
{
if (mesh.CalculateDesignatedOwnershipOfElement(i))
{
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(0,0), lambda*traction(0), 1e-3);
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(1,0), 0.0, 1e-3);
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(0,1), 0.0, 1e-3);
TS_ASSERT_DELTA(solver.GetAverageStressPerElement(i)(1,1), 0.0, 1e-3);
}
}
// check the written stresses
std::string test_output_directory = OutputFileHandler::GetChasteTestOutputDirectory();
NumericFileComparison comparison(test_output_directory + "/nonlin_elas_non_zero_bcs/solution.stress", "continuum_mechanics/test/data/exact.stress");
TS_ASSERT(comparison.CompareFiles(2e-4));
MechanicsEventHandler::Headings();
MechanicsEventHandler::Report();
}
/**
* Test with functional (rather than constant) body force and surface traction, against a known
* solution. Since a non-zero body force is used here and a known solution, this is the MOST
* IMPORTANT TEST.
*
* See equations and finite element implementations document for more info.
*
* Choose x=X+0.5*alpha*X*X, y=Y/(1+alpha*X), p=2c, then F has determinant 1, and S can be shown to
* be, where lam = 1 +alpha X (ie dx/dX)
* S = 2c[lam-1/lam, -Y*alpha*lam^{-2}; -Y*alpha*lam^{-2}, 1/lam - lam]
* in which case the required body force and surface traction can be computed to be
*
* b = 2c/density [ -alpha, -2*Y*alpha^2 * lam^{-3} ]
* s = 2c[lam-1/lam, -Y*alpha/(lam^2)] on X=1
* s = 2c[0, lam - 1/lam] on Y=0
* s = 2c[Y*alpha/lam^2, 1/lam - lam] on Y=1
*
*/
void TestAgainstExactSolution()
{
for (unsigned run=0; run<2; run++)
{
MechanicsEventHandler::Reset();
unsigned num_elem = 5;
QuadraticMesh<2> mesh(1.0/num_elem, 1.0, 1.0);
MooneyRivlinMaterialLaw<2> law(MATERIAL_PARAM);
std::vector<unsigned> fixed_nodes
= NonlinearElasticityTools<2>::GetNodesByComponentValue(mesh,0,0);
std::vector<BoundaryElement<1,2>*> boundary_elems;
for (TetrahedralMesh<2,2>::BoundaryElementIterator iter
= mesh.GetBoundaryElementIteratorBegin();
iter != mesh.GetBoundaryElementIteratorEnd();
++iter)
{
// get all boundary elems except those on X=0
if (fabs((*iter)->CalculateCentroid()[0])>1e-6)
{
BoundaryElement<1,2>* p_element = *iter;
boundary_elems.push_back(p_element);
}
}
assert(boundary_elems.size()==3*num_elem);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
problem_defn.SetZeroDisplacementNodes(fixed_nodes);
problem_defn.SetBodyForce(MyBodyForce);
problem_defn.SetTractionBoundaryConditions(boundary_elems, MyTraction);
if (run==1)
{
problem_defn.SetVerboseDuringSolve(); // coverage
problem_defn.SetSolveUsingSnes();
}
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"nonlin_elas_functional_data");
// this test requires the time to be set to t=1 to pass (see comment
// in and MyBodyForce() and MyTraction()
solver.SetCurrentTime(1.0);
// cover the option of writing output for each iteration
solver.SetWriteOutputEachNewtonIteration();
solver.Solve();
if (run==0)
{
// matrix might have (small) errors introduced if this fails
TS_ASSERT_EQUALS(solver.GetNumNewtonIterations(), 3u); // 'hardcoded' answer, protects against Jacobian getting messed up
}
// check CreateCmguiOutput() - call and check output files were written.
solver.CreateCmguiOutput();
// Create .vtu file
VtkNonlinearElasticitySolutionWriter<2> vtk_writer(solver);
vtk_writer.Write();
std::vector<c_vector<double,2> >& r_solution = solver.rGetDeformedPosition();
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
double X = mesh.GetNode(i)->rGetLocation()[0];
double Y = mesh.GetNode(i)->rGetLocation()[1];
double exact_x = X + 0.5*ALPHA*X*X;
double exact_y = Y/(1+ALPHA*X);
TS_ASSERT_DELTA(r_solution[i](0), exact_x, 1e-4);
TS_ASSERT_DELTA(r_solution[i](1), exact_y, 1e-4);
}
std::vector<double>& r_pressures = solver.rGetPressures();
for (unsigned i=0; i<r_pressures.size(); i++)
{
TS_ASSERT_DELTA( r_pressures[i]/(2*MATERIAL_PARAM), 1.0, 1e-3);
}
MechanicsEventHandler::Headings();
MechanicsEventHandler::Report();
if (run==0)
{
// Check output files were created: the standard output files initial.nodes and solution.nodes, the extra newton iteration
// output files created as SetWriteOutputEachNewtonIteration() was called above, and the cmgui files created as
// CreateCmguiOutput() was called above.
FileFinder init_file("nonlin_elas_functional_data/initial.nodes", RelativeTo::ChasteTestOutput);
TS_ASSERT(init_file.Exists());
FileFinder nodes_file("nonlin_elas_functional_data/solution.nodes", RelativeTo::ChasteTestOutput);
TS_ASSERT(nodes_file.Exists());
FileFinder newton_file("nonlin_elas_functional_data/newton_iteration_3.nodes", RelativeTo::ChasteTestOutput);
TS_ASSERT(newton_file.Exists());
FileFinder exelem_file("nonlin_elas_functional_data/cmgui/solution_0.exelem", RelativeTo::ChasteTestOutput);
TS_ASSERT(exelem_file.Exists());
FileFinder exnode0_file("nonlin_elas_functional_data/cmgui/solution_0.exnode", RelativeTo::ChasteTestOutput);
TS_ASSERT(exnode0_file.Exists());
FileFinder exnode1_file("nonlin_elas_functional_data/cmgui/solution_1.exnode", RelativeTo::ChasteTestOutput);
TS_ASSERT(exnode1_file.Exists());
#ifdef CHASTE_VTK
//Check the VTK file exists
FileFinder vtk_file("nonlin_elas_functional_data/vtk/solution.vtu", RelativeTo::ChasteTestOutput);
TS_ASSERT(vtk_file.Exists());
#endif
solver.rGetCurrentSolution().clear();
solver.rGetCurrentSolution().resize(solver.mNumDofs, 0.0);
solver.SetKspAbsoluteTolerance(1); // way too high
TS_ASSERT_THROWS_CONTAINS(solver.Solve(), "KSP Absolute tolerance was too high");
}
}
}
/*
* Test the functionality for specifying that a pressure should act in the normal direction on the
* DEFORMED SURFACE.
*
* The deformation is based on that in TestSolveWithNonZeroBoundaryConditions (x=X/lambda,
* y=Y*lam; see comments for this test), but the exact solution here is this rotated by
* 45 degrees anticlockwise. We choose dirichlet boundary conditions on the X=0 surface to
* match this, and a pressure on the opposite surface (similar to the traction provided in
* TestSolveWithNonZeroBoundaryConditions, except we don't provide the direction, the code
* needs to work this out), and it is scaled by 1.0/lambda as it acts on a smaller surface
* than would on the undeformed surface.
*/
void TestSolveWithPressureBcsOnDeformedSurface()
{
std::vector<double> soln_first_run;
for (unsigned run=0; run<2; run++)
{
double lambda = 0.85;
double c1 = 1.0;
unsigned num_elem = 10;
QuadraticMesh<2> mesh(1.0/num_elem, 1.0, 1.0);
MooneyRivlinMaterialLaw<2> law(c1);
std::vector<unsigned> fixed_nodes;
std::vector<c_vector<double,2> > locations;
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
if (fabs(mesh.GetNode(i)->rGetLocation()[0]) < 1e-6)
{
fixed_nodes.push_back(i);
c_vector<double,2> new_position;
new_position(0) = (lambda/sqrt(2.0)) * mesh.GetNode(i)->rGetLocation()[1];
new_position(1) = (lambda/sqrt(2.0)) * mesh.GetNode(i)->rGetLocation()[1];
locations.push_back(new_position);
}
}
std::vector<BoundaryElement<1,2>*> boundary_elems;
double pressure = (2*c1*(pow(lambda,-1) - lambda*lambda*lambda))/lambda;
for (TetrahedralMesh<2,2>::BoundaryElementIterator iter
= mesh.GetBoundaryElementIteratorBegin();
iter != mesh.GetBoundaryElementIteratorEnd();
++iter)
{
if (fabs((*iter)->CalculateCentroid()[0] - 1.0)<1e-4)
{
BoundaryElement<1,2>* p_element = *iter;
boundary_elems.push_back(p_element);
}
}
assert(boundary_elems.size()==num_elem);
SolidMechanicsProblemDefinition<2> problem_defn(mesh);
problem_defn.SetMaterialLaw(INCOMPRESSIBLE,&law);
problem_defn.SetFixedNodes(fixed_nodes, locations);
// the two variants of SetApplyNormalPressureOnDeformedSurface(). MyPressureFunction
// will return the same value as that in the variable pressure IF the current time
// is set to 1.0.
if (run==0)
{
problem_defn.SetApplyNormalPressureOnDeformedSurface(boundary_elems, pressure);
}
else
{
problem_defn.SetApplyNormalPressureOnDeformedSurface(boundary_elems, MyPressureFunction);
}
IncompressibleNonlinearElasticitySolver<2> solver(mesh,
problem_defn,
"nonlin_elas_pressure_on_deformed");
if (run==1)
{
solver.SetCurrentTime(1.0);
// To speed up this test, we provide the correct answer as the initial guess for
// the second run. Note: the second run may still take an iteration or two, as the
// final newton solve tolerance may be different (eg if relative tolerance)
solver.rGetCurrentSolution() = soln_first_run;
}
solver.Solve();
if (run==0)
{
soln_first_run = solver.rGetCurrentSolution();
}
std::vector<c_vector<double,2> >& r_solution = solver.rGetDeformedPosition();
for (unsigned i=0; i<mesh.GetNumNodes(); i++)
{
double exact_x_before_rotation = (1.0/lambda)*mesh.GetNode(i)->rGetLocation()[0];
double exact_y_before_rotation = lambda*mesh.GetNode(i)->rGetLocation()[1];
double exact_x = (1.0/sqrt(2.0))*( exact_x_before_rotation + exact_y_before_rotation);
double exact_y = (1.0/sqrt(2.0))*(-exact_x_before_rotation + exact_y_before_rotation);
TS_ASSERT_DELTA( r_solution[i](0), exact_x, 1e-3 );
TS_ASSERT_DELTA( r_solution[i](1), exact_y, 1e-3 );
}
// check the final pressure
std::vector<double>& r_pressures = solver.rGetPressures();
TS_ASSERT_EQUALS(r_pressures.size(), mesh.GetNumNodes());
for (unsigned i=0; i<r_pressures.size(); i++)
{
TS_ASSERT_DELTA(r_pressures[i], 2*c1*lambda*lambda, 5e-2 );
}
}
}
/*
* Repeat of previous test but with SNES solver from PETSc.
*
* Test the functionality for specifying that a pressure should act in the normal direction on the
* DEFORMED SURFACE.
*
* The deformation is based on that in TestSolveWithNonZeroBoundaryConditions (x=X/lambda,
* y=Y*lam; see comments for this test), but the exact solution here is this rotated by
* 45 degrees anticlockwise. We choose dirichlet boundary conditions on the X=0 surface to
* match this, and a pressure on the opposite surface (similar to the traction provided in
* TestSolveWithNonZeroBoundaryConditions, except we don't provide the direction, the code
* needs to work this out), and it is scaled by 1.0/lambda as it acts on a smaller surface
* than would on the undeformed surface.
*/
void TestSolveWithPressureBcsOnDeformedSurfaceSnes()
{
std::vector<double> soln_first_run;
for (unsigned run=0; run<2; run++)
{
double lambda = 0.85;