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
// use fenris_solid::assembly::MaterialEllipticOperator;
// use fenris_solid::materials::{LameParameters, LinearElasticMaterial, StVKMaterial, YoungPoisson};
// use fenris_solid::ElasticMaterialModel;
// use fenris_solid::ElasticityModel;
// TODO: Re-enable/rewrite tests here as appropriate when possible (most tests rely on some
// solid mechanics stuff)
// #[derive(Debug, Copy, Clone)]
// struct MockIdentityMaterial;
//
// impl<T> ElasticMaterialModel<T, U2> for MockIdentityMaterial
// where
// T: Real,
// {
// fn compute_strain_energy_density(&self, _deformation_gradient: &Matrix2<T>) -> T {
// unimplemented!()
// }
//
// fn compute_stress_tensor(&self, _deformation_gradient: &Matrix2<T>) -> Matrix2<T> {
// Matrix2::identity()
// }
//
// fn contract_stress_tensor_with(
// &self,
// _deformation_gradient: &Matrix2<T>,
// _a: &OVector<T, U2>,
// _b: &OVector<T, U2>,
// ) -> Matrix2<T> {
// Matrix2::zero()
// }
// }
//
// #[derive(Debug, Copy, Clone)]
// struct MockSimpleMaterial;
//
// #[allow(non_snake_case)]
// impl<T> ElasticMaterialModel<T, U2> for MockSimpleMaterial
// where
// T: Real,
// {
// fn compute_strain_energy_density(&self, _deformation_gradient: &Matrix2<T>) -> T {
// unimplemented!()
// }
//
// fn compute_stress_tensor(&self, F: &Matrix2<T>) -> Matrix2<T> {
// F - Matrix2::identity()
// }
//
// fn contract_stress_tensor_with(
// &self,
// _F: &Matrix2<T>,
// a: &OVector<T, U2>,
// b: &OVector<T, U2>,
// ) -> Matrix2<T> {
// Matrix2::identity() * a.dot(&b)
// }
// }
// #[test]
// fn quad4d2_constant_displacement_gives_zero_elastic_forces_for_reference_element() {
// let lame = fenris_solid::materials::LameParameters {
// mu: 2.0,
// lambda: 3.0,
// };
// let material = fenris_solid::materials::LinearElasticMaterial::from(lame);
//
// let u = 3.0 * OMatrix::<f64, U2, Dynamic>::repeat(4, 1.0);
//
// let quadrature = quad_quadrature_strength_5_f64();
// let quad = Quad4d2Element::from(reference_quad());
//
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut f_e = DMatrix::zeros(2, 4);
// assemble_generalized_element_elliptic_term(MatrixSliceMut::from(&mut f_e),
// &quad,
// &elliptic_operator,
// &MatrixSlice::from(&u),
// &quadrature);
// assert!(f_e.norm() < 1e-14);
// }
// #[test]
// fn quad4d2_constant_displacement_gives_zero_elastic_forces_for_arbitrary_quad() {
// let lame = fenris_solid::materials::LameParameters {
// mu: 2.0,
// lambda: 3.0,
// };
// let material = fenris_solid::materials::LinearElasticMaterial::from(lame);
// let u = 3.0 * OMatrix::<f64, U2, Dynamic>::repeat(4, 1.0);
//
// let quadrature = quad_quadrature_strength_5_f64();
// let quad = Quad4d2Element::from_vertices([
// Point2::new(-2.0, -3.0),
// Point2::new(1.0, -1.0),
// Point2::new(2.0, 4.0),
// Point2::new(-1.0, 3.0),
// ]);
//
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut f_e = DMatrix::zeros(2, 4);
// assemble_generalized_element_elliptic_term(MatrixSliceMut::from(&mut f_e),
// &quad,
// &elliptic_operator,
// &MatrixSlice::from(&u),
// &quadrature);
// assert!(f_e.norm() < 1e-14);
// }
// #[test]
// fn analytic_comparison_of_element_elastic_force_for_reference_element() {
// let u = 3.0 * OMatrix::<f64, U2, Dynamic>::repeat(4, 1.0);
// let quadrature = quad_quadrature_strength_5_f64();
// let material = MockIdentityMaterial;
// let quad = Quad4d2Element::from(reference_quad());
//
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut f_e = DMatrix::zeros(2, 4);
// assemble_generalized_element_elliptic_term(MatrixSliceMut::from(&mut f_e),
// &quad,
// &elliptic_operator,
// &MatrixSlice::from(&u),
// &quadrature);
// #[rustfmt::skip]
// let expected = Matrix2x4::new(1.0, -1.0, -1.0, 1.0,
// 1.0, 1.0, -1.0, -1.0);
// let diff = f_e - expected;
// assert!(diff.norm() < 1e-14);
// }
// TODO: Test elastic forces for arbitrary element
// #[test]
// fn analytic_comparison_of_element_stiffness_matrix_for_reference_element() {
// let u = 3.0 * OMatrix::<f64, U2, Dynamic>::repeat(4, 1.0);
// let material = MockSimpleMaterial;
// let quadrature = quad_quadrature_strength_5_f64();
// let quad = Quad4d2Element::from(reference_quad());
//
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut a = DMatrix::zeros(8, 8);
// assemble_generalized_element_stiffness(DMatrixSliceMut::from(&mut a),
// &quad,
// &elliptic_operator,
// MatrixSlice::from(&u),
// &quadrature);
//
// // For the given mock material, the contraction yields tr(B) I,
// // and so the integral over the element K reads
// // A^K_IJ = int_K tr(B_IJ) * I dX
// // with tr(B_IJ) = grad phi_I dot grad phi_J
// // in other words, the 2x2 matrix A^K_IJ corresponds to
// // the value of the *scalar* Laplacian stiffness matrix for basis
// // functions IJ multiplied by the 2x2 identity matrix.
// #[rustfmt::skip]
// let expected4x4 = Matrix4::new( 2.0/3.0, -1.0/6.0, -1.0/3.0, -1.0/6.0,
// -1.0/6.0, 2.0/3.0, -1.0/6.0, -1.0/3.0,
// -1.0/3.0, -1.0/6.0, 2.0/3.0, -1.0/6.0,
// -1.0/6.0, -1.0/3.0, -1.0/6.0, 2.0/3.0);
// let mut expected8x8: MatrixN<f64, U8> = MatrixN::zero();
// expected8x8
// .slice_with_steps_mut((0, 0), (4, 4), (1, 1))
// .copy_from(&expected4x4);
// expected8x8
// .slice_with_steps_mut((1, 1), (4, 4), (1, 1))
// .copy_from(&expected4x4);
//
// let diff = a - expected8x8;
// assert!(diff.norm() <= 1e-6);
// }
// #[test]
// #[allow(non_snake_case)]
// fn quad4d2_mass_matrix_vector_product_with_ones() {
// // It can be shown that, assuming the transformation from the reference
// // element to each individual element K is a linear transformation,
// // then
// // (M * 1)_Ii = rho_0 * sum_{K in S_I} |det J_K|
// // where S_I = { K | intersection of K and support of basis function I is non-empty},
// // rho_0 is the rest density and J_K is the Jacobian of the (linear)
// // transformation from the reference element to each element K.
// //
// // If furthermore all cells have the same size, we must only compute
// // |det J| once and scale it by the number of elements the node appears in.
//
// let resolutions = [1, 2, 3, 4, 8, 9, 11];
// let quadrature = quad_quadrature_strength_5_f64();
//
// for resolution in &resolutions {
// let mesh = create_unit_square_uniform_quad_mesh_2d(*resolution);
// let ndof = 2 * mesh.vertices().len();
// let model = Quad4Model::from_mesh_and_quadrature(mesh, quadrature.clone());
// let rho_0 = 3.0;
//
// let mass_matrix = model.assemble_mass(rho_0).build_dense();
//
// // Cells all have the same size
// let cell_size = 1.0 / f64::from_usize(*resolution).unwrap();
// let abs_det_J = cell_size * cell_size / 4.0;
// let num_nodes = model.vertices().len();
//
// let mut node_counts = vec![0u32; num_nodes];
// for connectivity in model.connectivity() {
// for node_index in &connectivity.0 {
// node_counts[*node_index] += 1;
// }
// }
//
// let expected_values = node_counts
// .iter()
// .map(|i| rho_0 * abs_det_J * f64::from(*i))
// .flat_map(|v| once(v).chain(once(v)));
// let expected = DVector::from_iterator(ndof, expected_values);
//
// let result = mass_matrix * DVector::repeat(ndof, 1.0);
// let diff = result - expected;
//
// assert!(diff.norm() < ndof as f64 * 1e-12);
// }
// }
// #[test]
// #[allow(non_snake_case)]
// fn tet4_mass_matrix_vector_product_with_ones() {
// // See the comment above for the explanation for this test
// let resolutions = [1, 2, 3, 4, 8];
// let quadrature = tet_quadrature_strength_5();
//
// for resolution in &resolutions {
// let mesh = create_rectangular_uniform_hex_mesh(1.0, 1, 1, 1, *resolution);
// let mesh = Tet4Mesh::try_from(&PolyMesh3d::from(&mesh).triangulate().unwrap()).unwrap();
//
// let model = Tet4Model::from_mesh_and_quadrature(mesh, quadrature.clone());
// let ndof = model.ndof();
// let rho_0 = 3.0;
//
// let mass_matrix = model.assemble_mass(rho_0).build_dense();
//
// // Cells all have the same size
// let cell_size = 1.0 / f64::from_usize(*resolution).unwrap();
// let abs_det_J = cell_size * cell_size * cell_size / (6.0 * 4.0);
// let num_nodes = model.vertices().len();
//
// let mut node_counts = vec![0u32; num_nodes];
// for connectivity in model.connectivity() {
// for node_index in &connectivity.0 {
// node_counts[*node_index] += 1;
// }
// }
//
// let expected_values = node_counts
// .iter()
// .map(|i| rho_0 * abs_det_J * f64::from(*i))
// .flat_map(|v| once(v).chain(once(v)).chain(once(v)));
// let expected = DVector::from_iterator(ndof, expected_values);
//
// let result = mass_matrix * DVector::repeat(ndof, 1.0);
// println!("result: {}", result);
// println!("expected: {}", expected);
// let diff = result - expected;
// assert!(diff.norm() < ndof as f64 * 1e-12);
// }
// }
// /// Creates an instance of a VectorFunction that corresponds to the elastic pseudo forces F(u)
// /// given displacements u.
// fn create_single_element_elastic_force_vector_function<'a, Connectivity>(
// element: &'a Connectivity::Element,
// indices: &'a Connectivity,
// material: impl ElasticMaterialModel<f64, Connectivity::GeometryDim> + 'a,
// quadrature: impl Quadrature<f64, Connectivity::GeometryDim> + 'a,
// ) -> impl VectorFunction<f64> + 'a
// where
// Connectivity: ElementConnectivity<
// f64,
// ReferenceDim = <Connectivity as ElementConnectivity<f64>>::GeometryDim,
// >,
// Connectivity::GeometryDim: DimName
// + DimMin<Connectivity::GeometryDim, Output = Connectivity::GeometryDim>,
// DefaultAllocator: ElementConnectivityAllocator<f64, Connectivity>,
// {
// let d = Connectivity::GeometryDim::dim();
// let vector_space_dim = d * element.num_nodes();
// let mut f_element = OMatrix::<f64, Connectivity::GeometryDim, Dynamic>::zeros(element.num_nodes());
// VectorFunctionBuilder::with_dimension(vector_space_dim).with_function(move |f, u| {
// let u_element: OMatrix<f64, Connectivity::GeometryDim, Dynamic> = indices.element_variables(u);
// let elliptic_operator = MaterialEllipticOperator(&material);
// assemble_generalized_element_elliptic_term(
// MatrixSliceMut::from(&mut f_element),
// element,
// &elliptic_operator,
// &MatrixSlice::from(&u_element),
// &quadrature,
// );
// f.copy_from_slice(f_element.as_slice());
// })
// }
// #[test]
// fn element_stiffness_matrix_is_negative_derivative_of_forces_for_linear_material_arbitrary_displacement(
// ) {
// let u = DVector::from_vec(vec![3.0, -2.0, 1.0, -4.0, 13.0, -2.0, 13.0, 15.0]);
// let lame = LameParameters {
// mu: 2.0,
// lambda: 3.0,
// };
// let material = LinearElasticMaterial::from(lame);
//
// let h = 1e-6;
// let quadrature = quad_quadrature_strength_5_f64();
// let quad = Quad4d2Element::from_vertices([
// Point2::new(0.5, 0.25),
// Point2::new(1.25, 0.5),
// Point2::new(1.5, 1.0),
// Point2::new(0.25, 1.5),
// ]);
//
// let quad_indices = Quad4d2Connectivity([0, 1, 2, 3]);
//
// let u_element: OMatrix<_, U2, Dynamic> = quad_indices.element_variables(&u);
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut a = DMatrix::zeros(8, 8);
// assemble_generalized_element_stiffness(DMatrixSliceMut::from(&mut a),
// &quad,
// &elliptic_operator,
// MatrixSlice::from(&u_element),
// &quadrature);
//
// let func = create_single_element_elastic_force_vector_function(
// &quad,
// &quad_indices,
// &material,
// &quadrature,
// );
//
// let a_approx = -approximate_jacobian(func, &u, &h);
//
// let diff = a - a_approx;
// assert!(diff.norm() < 1e-6);
// }
//
// #[test]
// fn tet4_element_stiffness_matrix_is_negative_derivative_of_forces_for_linear_material_arbitrary_displacement(
// ) {
// let u = DVector::from_vec(vec![
// 0.1, -0.2, 0.1, -0.0, 0.2, -0.1, 0.0, 0.05, 0.1, 0.2, 0.0, -0.2,
// ]);
// let lame = LameParameters {
// mu: 2.0,
// lambda: 3.0,
// };
// let material = LinearElasticMaterial::from(lame);
//
// let h = 1e-6;
// let quadrature = tet_quadrature_strength_5();
// let tet = Tet4Element::from_vertices([
// Point3::new(-1.0, -0.5, -1.0),
// Point3::new(1.0, -0.5, 0.0),
// Point3::new(0.0, 1.0, -1.0),
// Point3::new(0.0, 0.0, 0.5),
// ]);
// let tet_conn = Tet4Connectivity([0, 1, 2, 3]);
//
// let u_element: OMatrix<_, U3, Dynamic> = tet_conn.element_variables(&u);
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut a = DMatrix::zeros(12, 12);
// assemble_generalized_element_stiffness(DMatrixSliceMut::from(&mut a),
// &tet,
// &elliptic_operator,
// MatrixSlice::from(&u_element),
// &quadrature);
//
// let func = create_single_element_elastic_force_vector_function(
// &tet,
// &tet_conn,
// &material,
// &quadrature,
// );
//
// let a_approx = -approximate_jacobian(func, &u, &h);
//
// let diff = &a - &a_approx;
// assert!(diff.norm() < 1e-6);
// }
//
// #[test]
// fn element_stiffness_matrix_is_negative_derivative_of_forces_for_stvk_material_arbitrary_displacement(
// ) {
// let u = DVector::from_vec(vec![3.0, -2.0, 1.0, -4.0, 13.0, -2.0, 13.0, 15.0]);
// let lame = LameParameters {
// mu: 2.0,
// lambda: 3.0,
// };
// let material = StVKMaterial::from(lame);
//
// let h = 1e-6;
// let quadrature = quad_quadrature_strength_5_f64();
// let quad = Quad4d2Element::from_vertices([
// Point2::new(0.5, 0.25),
// Point2::new(1.25, 0.5),
// Point2::new(1.5, 1.0),
// Point2::new(0.25, 1.5),
// ]);
//
// let quad_indices = Quad4d2Connectivity([0, 1, 2, 3]);
//
// let u_element: OMatrix<_, U2, _> = quad_indices.element_variables(&u);
// let elliptic_operator = MaterialEllipticOperator(&material);
//
// let mut a = DMatrix::zeros(8, 8);
// assemble_generalized_element_stiffness(DMatrixSliceMut::from(&mut a),
// &quad,
// &elliptic_operator,
// MatrixSlice::from(&u_element),
// &quadrature);
//
// let func = create_single_element_elastic_force_vector_function(
// &quad,
// &quad_indices,
// &material,
// &quadrature,
// );
//
// let a_approx = -approximate_jacobian(func, &u, &h);
//
// let diff = a - a_approx;
// assert!(diff.norm() < 1e-5);
// }
//
// #[test]
// fn element_stiffness_matrix_is_negative_derivative_of_forces_for_stvk_material_problematic_element()
// {
// // This is an example where the eigenvalues of the element matrices turned out to be
// // strongly negative
//
// let u = DVector::from_vec(vec![
// 0.0,
// 0.0,
// -0.033613342105786675,
// -0.21919651627727949,
// -0.26977755029543005,
// -0.19110852394892108,
// 0.0,
// 0.0,
// ]);
// let lame = YoungPoisson {
// young: 1e8,
// poisson: 0.2,
// };
// let material = StVKMaterial::from(lame);
//
// let h = 1e-6;
// let quadrature = quad_quadrature_strength_5_f64();
// let quad = Quad4d2Element::from_vertices([
// Point2::new(0.0, -1.0),
// Point2::new(1.0, -1.0),
// Point2::new(1.0, 0.0),
// Point2::new(0.0, 0.0),
// ]);
//
// let quad_indices = Quad4d2Connectivity([0, 1, 2, 3]);
//
// let u_element: OMatrix<_, U2, _> = quad_indices.element_variables(&u);
// let elliptic_operator = MaterialEllipticOperator(&material);
// let mut a = DMatrix::zeros(8, 8);
// assemble_generalized_element_stiffness(DMatrixSliceMut::from(&mut a),
// &quad,
// &elliptic_operator,
// MatrixSlice::from(&u_element),
// &quadrature);
//
// let func = create_single_element_elastic_force_vector_function(
// &quad,
// &quad_indices,
// &material,
// &quadrature,
// );
//
// let a_approx = -approximate_jacobian(func, &u, &h);
//
// let diff = &a - &a_approx;
//
// assert!(diff.norm() / (a.norm() + a_approx.norm()) < 1e-5);
//
// // TODO: Report issue with this matrix to nalgebra as example of failing eigenvalue decomposition
// }