-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbiharmonic.cc
1462 lines (1229 loc) · 59.3 KB
/
biharmonic.cc
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) 2019 by Wolfgang Bangerth and SAATI Co.
*
* ---------------------------------------------------------------------
*/
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/function.h>
#include <deal.II/base/thread_management.h>
#include <deal.II/base/parameter_handler.h>
#include <deal.II/base/timer.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/solver_cg.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/sparse_direct.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/grid_in.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/grid/tria_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/mapping_q.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/numerics/vector_tools.h>
#include <deal.II/numerics/matrix_tools.h>
#include <deal.II/numerics/data_out.h>
// The two most interesting header files will be these two:
#include <deal.II/fe/fe_interface_values.h>
#include <deal.II/meshworker/mesh_loop.h>
// The first of these is responsible for providing the class FEInterfaceValue
// that can be used to evaluate quantities such as the jump or average
// of shape functions (or their gradients) across interfaces between cells.
// This class will be quite useful in evaluating the penalty terms that appear
// in the C0IP formulation.
#include <fstream>
#include <future>
#include <thread>
#include <iostream>
#include <memory>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <complex>
std::string instance_folder;
std::ofstream logger;
namespace MembraneOscillation
{
using namespace dealii;
using ScalarType = std::complex<double>;
// The following namespace defines material parameters. We use SI units.
namespace MaterialParameters
{
double density;
double thickness;
ScalarType tension;
ScalarType stiffness_D;
std::vector<double> frequencies;
}
std::string mesh_file_name;
unsigned int fe_degree = 2;
int n_mesh_refinement_steps = 5;
unsigned int n_threads = 0;
void
declare_parameters (ParameterHandler &prm)
{
prm.declare_entry ("Mesh file name", "./square_mesh.vtk",
Patterns::FileName(),
"The name of the file from which to read the mesh.");
prm.declare_entry ("Thickness", "0.0001",
Patterns::Double(0),
"Thickness of the membrane. Units: [m].");
prm.declare_entry ("Density", "100",
Patterns::Double(0),
"Volumetric density of the membrane material. Units: [kg/m^3].");
prm.declare_entry ("Young's modulus", "200e6",
Patterns::Double(0),
"The magnitude of the Young's modulus. Units: [Pa].");
prm.declare_entry ("Young's modulus loss tangent", "2",
Patterns::Double(0,90),
"The angle used to make the Young's modulus complex-valued. "
"Units: [degrees].");
prm.declare_entry ("Poisson's ratio", "0.3",
Patterns::Double(0,0.5),
"Poisson's ratio. Units: none.");
prm.declare_entry ("Tension", "30",
Patterns::Double(0),
"The tension coefficient T that describes the membrane part of "
"the material behavior. Units: [N/m].");
prm.declare_entry ("Tension loss tangent", "0",
Patterns::Double(0,90),
"The angle used to make the tension complex-valued. "
"Units: [degrees].");
prm.declare_entry ("Frequencies", "linear_spacing(100,10000,100)",
Patterns::Anything(),
"A description of the frequencies to compute. See "
"the readme.md file for a description of the format "
"for this entry.");
prm.declare_entry ("Number of mesh refinement steps", "5",
Patterns::Integer(-100,10),
"The number of global mesh refinement steps applied "
"to the coarse mesh if positive or zero. If negative, "
"then it denotes the number of mesh points per wave length "
"as described in readme.md.");
prm.declare_entry ("Finite element polynomial degree", "2",
Patterns::Integer(1,5),
"The polynomial degree to be used for the finite element.");
prm.declare_entry ("Number of threads", "0",
Patterns::Integer(0),
"The number of threads this program may use at the same time. "
"Threads are used to compute the frequency response for "
"different frequencies at the same time since these are "
"independent computations. A value of zero means that the "
"program may use as many threads as it pleases, whereas a "
"positive number limits how many threads (and consequently "
"CPU cores) the program will use.");
}
void
read_parameters (ParameterHandler &prm)
{
// First read parameter values from the input file 'biharmonic.prm'
prm.parse_input (instance_folder + "/biharmonic.prm");
using namespace MaterialParameters;
// First get the independent parameters from the input file:
double E, E_loss_tangent, T, T_loss_tangent, poissons_ratio;
thickness = prm.get_double ("Thickness");
density = prm.get_double ("Density");
E = prm.get_double ("Young's modulus");
E_loss_tangent = prm.get_double ("Young's modulus loss tangent");
poissons_ratio = prm.get_double ("Poisson's ratio");
T = prm.get_double ("Tension");
T_loss_tangent = prm.get_double ("Tension loss tangent");
mesh_file_name = prm.get ("Mesh file name");
// Read and parse the entry that determines which frequencies to compute.
// Recall that the format is one of the following:
// - linear_spacing(min,max,n_steps)
// - exp_spacing(min,max,n_steps)
// - list(...)
const std::string frequency_descriptor = prm.get ("Frequencies");
if (frequency_descriptor.find ("linear_spacing") == 0)
{
// Get the rest of the string, and eat any space at the start and end
const std::string parenthesized_expr
= Utilities::trim (frequency_descriptor.substr
(std::string("linear_spacing").size(),
std::string::npos));
AssertThrow (parenthesized_expr.size() >= 2
&&
parenthesized_expr.front() == '('
&&
parenthesized_expr.back() == ')',
ExcMessage ("Wrong format for 'linear_spacing'."));
// Then get the interior part, again trim spaces, and split at
// commas
const std::vector<std::string> min_max_steps
= Utilities::split_string_list
(Utilities::trim (parenthesized_expr.substr
(1,
parenthesized_expr.size() - 2)),
',');
AssertThrow (min_max_steps.size() == 3,
ExcMessage ("Wrong format for 'linear_spacing'."));
const double min_omega = Utilities::string_to_double(min_max_steps[0])
* 2 * numbers::PI;
const double max_omega = Utilities::string_to_double(min_max_steps[1])
* 2 * numbers::PI;
const unsigned int n_frequencies = Utilities::string_to_int(min_max_steps[2]);
const double delta_omega = (max_omega - min_omega)
/ (n_frequencies-1)
* (1.-1e-12);
for (double omega = min_omega;
omega <= max_omega;
omega += delta_omega)
MaterialParameters::frequencies.push_back (omega);
}
else if (frequency_descriptor.find ("exp_spacing") == 0)
{
// Get the rest of the string, and eat any space at the start and end
const std::string parenthesized_expr
= Utilities::trim (frequency_descriptor.substr
(std::string("exp_spacing").size(),
std::string::npos));
AssertThrow (parenthesized_expr.size() >= 2
&&
parenthesized_expr.front() == '('
&&
parenthesized_expr.back() == ')',
ExcMessage ("Wrong format for 'exp_spacing'."));
// Then get the interior part, again trim spaces, and split at
// commas
const std::vector<std::string> min_max_steps
= Utilities::split_string_list
(Utilities::trim (parenthesized_expr.substr
(1,
parenthesized_expr.size() - 2)),
',');
AssertThrow (min_max_steps.size() == 3,
ExcMessage ("Wrong format for 'exp_spacing'."));
const double log_min_omega = std::log(Utilities::string_to_double(min_max_steps[0])
* 2 * numbers::PI);
const double log_max_omega = std::log(Utilities::string_to_double(min_max_steps[1])
* 2 * numbers::PI);
const unsigned int n_frequencies = Utilities::string_to_int(min_max_steps[2]);
const double delta_log_omega = (log_max_omega - log_min_omega)
/ (n_frequencies - 1)
* (1.-1e-12);
for (double log_omega = log_min_omega;
log_omega <= log_max_omega;
log_omega += delta_log_omega)
MaterialParameters::frequencies.push_back (std::exp(log_omega));
}
else if (frequency_descriptor.find ("list") == 0)
{
// Get the rest of the string, and eat any space at the start and end
const std::string parenthesized_expr
= Utilities::trim (frequency_descriptor.substr
(std::string("list").size(),
std::string::npos));
AssertThrow (parenthesized_expr.size() >= 2
&&
parenthesized_expr.front() == '('
&&
parenthesized_expr.back() == ')',
ExcMessage ("Wrong format for 'list' frequency spacing."));
// Then get the interior part, again trim spaces, and split at
// commas
MaterialParameters::frequencies =
Utilities::string_to_double
(Utilities::split_string_list
(Utilities::trim (parenthesized_expr.substr
(1,
parenthesized_expr.size() - 2)),
','));
AssertThrow (MaterialParameters::frequencies.size() >= 1,
ExcMessage ("Wrong format for 'list' frequency spacing."));
// Because MaterialParameters::frequencies stores angular
// frequencies, we need to multiply by 2*pi
for (auto &f : MaterialParameters::frequencies)
f *= 2 * numbers::PI;
}
else
AssertThrow (false,
ExcMessage ("The format for the description of the frequencies to "
"be solved for, namely <"
+ frequency_descriptor + ">, did not match any of "
"the recognized formats."));
// Then compute the dependent ones. Note that we interpret the angle in degrees.
const ScalarType youngs_modulus
= E * std::exp(std::complex<double>(0,2*numbers::PI*E_loss_tangent/360));
tension
= T * std::exp(std::complex<double>(0,2*numbers::PI*T_loss_tangent/360));
stiffness_D
= (youngs_modulus *
ScalarType(thickness * thickness * thickness
/ 12 / (1 - poissons_ratio * poissons_ratio)));
fe_degree = prm.get_integer ("Finite element polynomial degree");
n_mesh_refinement_steps = prm.get_integer ("Number of mesh refinement steps");
Assert(fe_degree >= 2,
ExcMessage("The C0IP formulation for the biharmonic problem "
"only works if one uses elements of polynomial "
"degree at least 2."));
n_threads = prm.get_integer ("Number of threads");
}
// A data structure that is used to collect the results of the computations
// for one frequency. The main class fills this for a given frequency
// in various places of its member functions, and at the end puts it into
// a global map.
struct OutputData
{
ScalarType normalized_amplitude_integral;
double normalized_maximum_amplitude;
std::string visualization_file_name;
};
// A variable that will collect the data (value) for all frequencies
// omega (key). Since we will access it from different threads, we also
// need a mutex to guard access to it.
std::map<double,OutputData> results;
std::mutex results_mutex;
TimerOutput timer_output = TimerOutput (logger, TimerOutput::summary,
TimerOutput::wall_times);
// Check whether an external program has left a signal that
// indicates that the current program run should terminate without
// computing any further frequency responses. This is done by
// placing the word "STOP" into the file "termination_signal" in the
// current directory.
//
// Once detected, we delete the file again and terminate the
// program.
bool check_for_termination_signal()
{
static bool termination_requested = false;
static std::mutex mutex;
std::lock_guard<std::mutex> lock_guard (mutex);
if (termination_requested == true)
return true;
// Try and see whether we can open the file at all. If we can't,
// then no termination signal has been sent. If so, return 'true',
// but before that set a flag that ensures we don't have to do the
// expensive test with the file in any further calls. (We'll try
// to abort the program below, but this may block for a bit
// because we need to wait for the lock that guards access to the
// output file.)
std::ifstream in(instance_folder + "/termination_signal");
if (!in)
{
termination_requested = false;
return false;
}
// OK, the file exists, but does it contain the right content?
std::string line;
std::getline(in, line);
if (line == "STOP")
{
termination_requested = true;
// Close the file handle and remove the file.
in.close();
std::remove ((instance_folder + "/termination_signal").c_str());
// Now wait for the lock that guards access to the output file
// and if we have it, we know that nothing else is writing to
// the file at the moment and we can safely abort the program.
std::lock_guard<std::mutex> results_lock(results_mutex);
logger << "INFO *** Terminating program upon request." << std::endl;
std::exit (1);
return true;
}
// The file exists, but it has the wrong content (or no content so
// far). This means no termination. In the best of all cases, we
// will have caught the driver program having created but not
// written to the file. The next time we check, we might find the
// file in the correct state.
return false;
}
template <int dim>
class RightHandSide : public Function<dim>
{
public:
static_assert(dim == 2, "Only dim==2 is implemented");
RightHandSide (const double omega)
: omega (omega)
{}
virtual double value(const Point<dim> &/*p*/,
const unsigned int /*component*/ = 0) const override
{
return 1;
}
private:
double omega;
};
// @sect3{The main class}
//
// The following is the principal class of this tutorial program. It has
// the structure of many of the other tutorial programs and there should
// really be nothing particularly surprising about its contents or
// the constructor that follows it.
template <int dim>
class BiharmonicProblem
{
public:
BiharmonicProblem(const double omega);
void run();
private:
void make_grid();
void setup_system();
void assemble_system();
void solve();
void postprocess();
void output_results();
// The frequency that this instance of the class is supposed to solve for.
const double omega;
Triangulation<dim> triangulation;
MappingQ<dim> mapping;
FE_Q<dim> fe;
DoFHandler<dim> dof_handler;
std::map<types::global_dof_index,ScalarType> boundary_values;
SparsityPattern sparsity_pattern;
SparseMatrix<ScalarType> system_matrix;
Vector<ScalarType> solution;
Vector<ScalarType> system_rhs;
OutputData output_data;
};
template <int dim>
BiharmonicProblem<dim>::BiharmonicProblem(const double omega)
: omega (omega)
, mapping(1)
, fe(MembraneOscillation::fe_degree)
, dof_handler(triangulation)
{}
// Next up are the functions that create the initial mesh (a once refined
// unit square) and set up the constraints, vectors, and matrices on
// each mesh. Again, both of these are essentially unchanged from many
// previous tutorial programs.
template <int dim>
void BiharmonicProblem<dim>::make_grid()
{
std::unique_ptr<TimerOutput::Scope> timer_section = (n_threads==1 ? std::make_unique<TimerOutput::Scope>(timer_output, "Make grid") : nullptr);
GridIn<dim> grid_in;
grid_in.attach_triangulation (triangulation);
std::ifstream input (instance_folder + "/" + mesh_file_name);
grid_in.read_vtk (input);
// Now implement the heuristic for mesh refinement described in
// readme.md: If positive, just do a number of global refinement
// steps. If negative, interpret it as the number of mesh points
// per wave length.
if (n_mesh_refinement_steps >= 0)
triangulation.refine_global(n_mesh_refinement_steps);
else
{
const int N = -n_mesh_refinement_steps;
const double lambda_1 = 2 * numbers::PI *
std::sqrt (std::real(MaterialParameters::tension)
/
(MaterialParameters::density
*
MaterialParameters::thickness))
/
omega;
const double lambda_2 = 2 * numbers::PI *
std::pow (std::real(MaterialParameters::stiffness_D)
/
(MaterialParameters::density
*
MaterialParameters::thickness),
0.25)
/
std::sqrt(omega);
const double lambda = std::max (lambda_1, lambda_2);
const double diameter = GridTools::diameter(triangulation);
const double delta_x = std::min(lambda, diameter) / N * fe_degree;
while (GridTools::maximal_cell_diameter(triangulation)
>= delta_x)
triangulation.refine_global();
}
}
template <int dim>
void BiharmonicProblem<dim>::setup_system()
{
std::unique_ptr<TimerOutput::Scope> timer_section = (n_threads==1 ? std::make_unique<TimerOutput::Scope>(timer_output, "Set up system") : nullptr);
dof_handler.distribute_dofs(fe);
boundary_values.clear();
VectorTools::interpolate_boundary_values(dof_handler,
0,
Functions::ZeroFunction<dim,ScalarType>(),
boundary_values);
DynamicSparsityPattern c_sparsity(dof_handler.n_dofs());
{
std::vector<types::global_dof_index> local_dof_indices (fe.dofs_per_cell);
std::vector<types::global_dof_index> neighbor_dof_indices (fe.dofs_per_cell);
for (const auto &cell : dof_handler.active_cell_iterators())
{
cell->get_dof_indices (local_dof_indices);
for (auto i : local_dof_indices)
for (auto j : local_dof_indices)
c_sparsity.add (i,j);
for (unsigned int f=0; f<GeometryInfo<dim>::faces_per_cell; ++f)
if (cell->at_boundary(f) == false)
{
cell->neighbor(f)->get_dof_indices (neighbor_dof_indices);
for (auto i : local_dof_indices)
for (auto j : neighbor_dof_indices)
c_sparsity.add (i,j);
}
}
c_sparsity.compress();
}
sparsity_pattern.copy_from(c_sparsity);
system_matrix.reinit(sparsity_pattern);
solution.reinit(dof_handler.n_dofs());
system_rhs.reinit(dof_handler.n_dofs());
}
// @sect{Assembling the linear system}
//
// The following pieces of code are more interesting. They all relate to the
// assembly of the linear system. While assemling the cell-interior terms
// is not of great difficulty -- that works in essence like the assembly
// of the corresponding terms of the Laplace equation, and you have seen
// how this works in step-4 or step-6, for example -- the difficulty
// is with the penalty terms in the formulation. These require the evaluation
// of gradients of shape functions at interfaces of cells. At the least,
// one would therefore need to use two FEFaceValues objects, but if one of the
// two sides is adaptively refined, then one actually needs an FEFaceValues
// and one FESubfaceValues objects; one also needs to keep track which
// shape functions live where, and finally we need to ensure that every
// face is visited only once. All of this is a substantial overhead to the
// logic we really want to implement (namely the penalty terms in the
// bilinear form). As a consequence, we will make use of the
// FEInterfaceValues class -- a helper class in deal.II that allows us
// to abstract away the two FEFaceValues or FESubfaceValues objects and
// directly access what we really care about: jumps, averages, etc.
//
// But this doesn't yet solve our problem of having to keep track of
// which faces we have already visited when we loop over all cells and
// all of their faces. To make this process simpler, we use the
// MeshWorker::mesh_loop() function that provides a simple interface
// for this task: Based on the ideas outlined in the WorkStream
// namespace documentation, MeshWorker::mesh_loop() requires three
// functions that do work on cells, interior faces, and boundary
// faces; these functions work on scratch objects for intermediate
// results, and then copy the result of their computations into
// copy data objects from where a copier function copies them into
// the global matrix and right hand side objects.
//
// The following structures then provide the scratch and copy objects
// that are necessary for this approach. You may look up the WorkStream
// namespace as well as the
// @ref threads "Parallel computing with multiple processors"
// module for more information on how they typically work.
template <int dim>
struct ScratchData
{
ScratchData(const Mapping<dim> & mapping,
const FiniteElement<dim> &fe,
const unsigned int quadrature_degree,
const UpdateFlags update_flags = update_values |
update_gradients |
update_quadrature_points |
update_JxW_values,
const UpdateFlags interface_update_flags =
update_values | update_gradients | update_quadrature_points |
update_JxW_values | update_normal_vectors)
: fe_values(mapping, fe, QGauss<dim>(quadrature_degree), update_flags)
, fe_interface_values(mapping,
fe,
QGauss<dim - 1>(quadrature_degree),
interface_update_flags)
{}
ScratchData(const ScratchData<dim> &scratch_data)
: fe_values(scratch_data.fe_values.get_mapping(),
scratch_data.fe_values.get_fe(),
scratch_data.fe_values.get_quadrature(),
scratch_data.fe_values.get_update_flags())
, fe_interface_values(scratch_data.fe_values.get_mapping(),
scratch_data.fe_values.get_fe(),
scratch_data.fe_interface_values.get_quadrature(),
scratch_data.fe_interface_values.get_update_flags())
{}
FEValues<dim> fe_values;
FEInterfaceValues<dim> fe_interface_values;
};
struct CopyData
{
CopyData(const unsigned int dofs_per_cell)
: cell_matrix(dofs_per_cell, dofs_per_cell)
, cell_rhs(dofs_per_cell)
, local_dof_indices(dofs_per_cell)
{}
CopyData(const CopyData &) = default;
struct FaceData
{
FullMatrix<ScalarType> cell_matrix;
std::vector<types::global_dof_index> joint_dof_indices;
};
FullMatrix<ScalarType> cell_matrix;
Vector<ScalarType> cell_rhs;
std::vector<types::global_dof_index> local_dof_indices;
std::vector<FaceData> face_data;
};
// The more interesting part is where we actually assemble the linear system.
// Fundamentally, this function has five parts:
// - The definition of the `cell_worker` "lambda function", a small
// function that is defined within the surrounding `assemble_system()`
// function and that will be responsible for computing the local
// integrals on an individual cell; it will work on a copy of the
// `ScratchData` class and put its results into the corresponding
// `CopyData` object.
// - The definition of the `face_worker` lambda function that does
// the integration of all terms that live on the interfaces between
// cells.
// - The definition of the `boundary_worker` function that does the
// same but for cell faces located on the boundary of the domain.
// - The definition of the `copier` function that is responsible
// for copying all of the data the previous three functions have
// put into copy objects for a single cell, into the global matrix
// and right hand side.
//
// The fifth part is the one where we bring all of this together.
//
// Let us go through each of these pieces necessary for the assembly
// in turns.
template <int dim>
void BiharmonicProblem<dim>::assemble_system()
{
std::unique_ptr<TimerOutput::Scope> timer_section = (n_threads==1 ? std::make_unique<TimerOutput::Scope>(timer_output, "Assemble linear system") : nullptr);
using Iterator = typename DoFHandler<dim>::active_cell_iterator;
// The first piece is the `cell_worker` that does the assembly
// on the cell interiors. It is a (lambda) function that takes
// a cell (input), a scratch object, and a copy object (output)
// as arguments. It looks like the assembly functions of many
// other of the tutorial programs, or at least the body of the
// loop over all cells.
//
// The terms we integrate here are the cell contribution
// @f{align*}{
// A^K_{ij} = \int_K \nabla^2\varphi_i(x) : \nabla^2\varphi_j(x) dx
// @f}
// to the global matrix, and
// @f{align*}{
// f^K_i = \int_K varphi_i(x) f(x) dx
// @f}
// to the right hand side vector.
//
// We use the same technique as used in the assembly of step-22
// to accelerate the function: Instead of calling
// `fe_values.shape_hessian(i, qpoint)` in the innermost loop,
// we instead create a variable `hessian_i` that evaluates this
// value once in the loop over `i` and re-use the so-evaluated
// value in the loop over `j`. For symmetry, we do the same with a
// variable `hessian_j`, although it is indeed only used once and
// we could have left the call to `fe_values.shape_hessian(j,qpoint)`
// in the instruction that computes the scalar product between
// the two terms.
auto cell_worker = [&](const Iterator & cell,
ScratchData<dim> &scratch_data,
CopyData & copy_data) {
std::unique_ptr<TimerOutput::Scope> timer_section = (n_threads==1 ? std::make_unique<TimerOutput::Scope>(timer_output, "Assemble linear system - cell") : nullptr);
copy_data.cell_matrix = 0;
copy_data.cell_rhs = 0;
scratch_data.fe_values.reinit(cell);
cell->get_dof_indices(copy_data.local_dof_indices);
const FEValues<dim> &fe_values = scratch_data.fe_values;
const RightHandSide<dim> right_hand_side (omega);
const unsigned int dofs_per_cell =
scratch_data.fe_values.get_fe().dofs_per_cell;
for (unsigned int qpoint = 0; qpoint < fe_values.n_quadrature_points;
++qpoint)
{
for (unsigned int i = 0; i < dofs_per_cell; ++i)
{
const Tensor<2,dim> hessian_i = fe_values.shape_hessian(i, qpoint);
const Tensor<1,dim> grad_i = fe_values.shape_grad(i, qpoint);
const double value_i = fe_values.shape_value(i, qpoint);
for (unsigned int j = 0; j < dofs_per_cell; ++j)
{
const Tensor<2,dim> hessian_j = fe_values.shape_hessian(j, qpoint);
const Tensor<1,dim> grad_j = fe_values.shape_grad(j, qpoint);
const double value_j = fe_values.shape_value(j, qpoint);
copy_data.cell_matrix(i, j) +=
(MaterialParameters::stiffness_D *
scalar_product(
hessian_i, // nabla^2 phi_i(x)
hessian_j) // nabla^2 phi_j(x)
+
MaterialParameters::tension *
grad_i *
grad_j
-
omega *
omega *
MaterialParameters::thickness *
MaterialParameters::density *
value_i *
value_j
)
* fe_values.JxW(qpoint); // dx
}
copy_data.cell_rhs(i) +=
fe_values.shape_value(i, qpoint) * // phi_i(x)
right_hand_side.value(
fe_values.quadrature_point(qpoint)) * // f(x)
fe_values.JxW(qpoint); // dx
}
}
};
// The next building block is the one that assembles penalty terms on each
// of the interior faces of the mesh. As described in the documentation of
// MeshWorker::mesh_loop(), this function receives arguments that denote
// a cell and its neighboring cell, as well as (for each of the two
// cells) the face (and potentially sub-face) we have to integrate
// over. Again, we also get a scratch object, and a copy object
// for putting the results in.
//
// The function has three parts itself. At the top, we initialize
// the FEInterfaceValues object and create a new `CopyData::FaceData`
// object to store our input in. This gets pushed to the end of the
// `copy_data.face_data` variable. We need to do this because
// the number of faces (or subfaces) over which we integrate for a
// given cell differs from cell to cell, and the sizes of these
// matrices also differ, depending on what degrees of freedom
// are adjacent to the face or subface. As discussed in the documentation
// of MeshWorker::mesh_loop(), the copy object is reset every time a new
// cell is visited, so that what we push to the end of
// `copy_data.face_data()` is really all that the later `copier` function
// gets to see when it copies the contributions of each cell to the global
// matrix and right hand side objects.
auto face_worker = [&](const Iterator & cell,
const unsigned int &f,
const unsigned int &sf,
const Iterator & ncell,
const unsigned int &nf,
const unsigned int &nsf,
ScratchData<dim> & scratch_data,
CopyData & copy_data) {
std::unique_ptr<TimerOutput::Scope> timer_section = (n_threads==1 ? std::make_unique<TimerOutput::Scope>(timer_output, "Assemble linear system - face") : nullptr);
FEInterfaceValues<dim> &fe_interface_values =
scratch_data.fe_interface_values;
fe_interface_values.reinit(cell, f, sf, ncell, nf, nsf);
copy_data.face_data.emplace_back();
CopyData::FaceData ©_data_face = copy_data.face_data.back();
copy_data_face.joint_dof_indices =
fe_interface_values.get_interface_dof_indices();
const unsigned int n_interface_dofs =
fe_interface_values.n_current_interface_dofs();
copy_data_face.cell_matrix.reinit(n_interface_dofs, n_interface_dofs);
// The second part deals with determining what the penalty
// parameter should be. The simplest formula for this parameter $\gamma$
// is $\frac{p(p+1)}{h_K}$ where $p$ is the polynomial degree of the
// finite element used and $h_K$ is the size of cell $K$. But this
// is not quite so straightforward: If one uses highly stretched cells,
// then a more involved theory says that $h$ should be replaced be the
// diameter of cell $K$ normal to the direction of the edge in question.
// It turns out that there is a function in deal.II for that. Secondly,
// $h_K$ may be different when viewed from the two different sides of
// a face.
//
// To stay on the safe side, we take the maximum of the two values.
// We will note that it is possible that this computation has to be
// further adjusted if one were to use hanging nodes resulting from
// adaptive mesh refinement.
const unsigned int p = fe.degree;
const double gamma_over_h =
std::max((1.0 * p * (p + 1) /
cell->extent_in_direction(
GeometryInfo<dim>::unit_normal_direction[f])),
(1.0 * p * (p + 1) /
ncell->extent_in_direction(
GeometryInfo<dim>::unit_normal_direction[nf])));
// Finally, and as usual, we loop over the quadrature points
// and indices `i` and `j` to add up the contributions of this
// face or sub-face. These are then stored in the `copy_data.face_data`
// object created above. As for the cell worker, we pull the evalation
// of averages and jumps out of the loops if possible, introducing
// local variables that store these results. The assembly then only
// needs to use these local variables in the innermost loop.
for (unsigned int qpoint = 0;
qpoint < fe_interface_values.n_quadrature_points;
++qpoint)
{
const auto &n = fe_interface_values.normal(qpoint);
for (unsigned int i = 0; i < n_interface_dofs; ++i)
{
const double av_hessian_i_dot_n_dot_n
= (fe_interface_values.average_of_shape_hessians(i, qpoint) * n * n);
const double jump_grad_i_dot_n
= (fe_interface_values.jump_in_shape_gradients(i, qpoint) * n);
for (unsigned int j = 0; j < n_interface_dofs; ++j)
{
const double av_hessian_j_dot_n_dot_n
= (fe_interface_values.average_of_shape_hessians(j, qpoint) * n * n);
const double jump_grad_j_dot_n
= (fe_interface_values.jump_in_shape_gradients(j, qpoint) * n);
copy_data_face.cell_matrix(i, j) +=
MaterialParameters::stiffness_D *
(-
av_hessian_i_dot_n_dot_n // - {grad^2 v n n }
* jump_grad_j_dot_n // [grad u n]
-
av_hessian_j_dot_n_dot_n // - {grad^2 u n n }
* jump_grad_i_dot_n // [grad v n]
+
// gamma/h [grad u n ][grad v n]:
gamma_over_h
* jump_grad_i_dot_n
* jump_grad_j_dot_n
)
*
fe_interface_values.JxW(qpoint); // dx
}
}
}
};
// The third piece is to do the same kind of assembly for faces that
// are at the boundary. The idea is the same as above, of course,
// with only the difference that there are now penalty terms that
// also go into the right hand side.
//
// As before, the first part of the function simply sets up some
// helper objects:
auto boundary_worker = [&](const Iterator & cell,
const unsigned int &face_no,
ScratchData<dim> & scratch_data,
CopyData & copy_data) {
std::unique_ptr<TimerOutput::Scope> timer_section = (n_threads==1 ? std::make_unique<TimerOutput::Scope>(timer_output, "Assemble linear system - boundary") : nullptr);
FEInterfaceValues<dim> &fe_interface_values = scratch_data.fe_interface_values;
fe_interface_values.reinit(cell, face_no);
const auto &q_points = fe_interface_values.get_quadrature_points();
copy_data.face_data.emplace_back();
CopyData::FaceData ©_data_face = copy_data.face_data.back();
const unsigned int n_dofs = fe_interface_values.n_current_interface_dofs();
copy_data_face.joint_dof_indices = fe_interface_values.get_interface_dof_indices();
copy_data_face.cell_matrix.reinit(n_dofs, n_dofs);
const std::vector<double> & JxW = fe_interface_values.get_JxW_values();
const std::vector<Tensor<1, dim>> &normals = fe_interface_values.get_normal_vectors();
// Positively, because we now only deal with one cell adjacent to the
// face (as we are on the boundary), the computation of the penalty
// factor $\gamma$ is substantially simpler:
const unsigned int p = fe.degree;
const double gamma_over_h =
(1.0 * p * (p + 1) /
cell->extent_in_direction(
GeometryInfo<dim>::unit_normal_direction[face_no]));
// The third piece is the assembly of terms. This is now slightly more
// involved since these contains both terms for the matrix and for
// the right hand side. The latter requires us to evaluate the
//
for (unsigned int qpoint = 0; qpoint < q_points.size(); ++qpoint)
{
const auto &n = normals[qpoint];
for (unsigned int i = 0; i < n_dofs; ++i)
{
const double av_hessian_i_dot_n_dot_n
= (fe_interface_values.average_of_shape_hessians(i, qpoint) * n * n);
const double jump_grad_i_dot_n
= (fe_interface_values.jump_in_shape_gradients(i, qpoint) * n);
for (unsigned int j = 0; j < n_dofs; ++j)
{
const double av_hessian_j_dot_n_dot_n
= (fe_interface_values.average_of_shape_hessians(j, qpoint) * n * n);
const double jump_grad_j_dot_n
= (fe_interface_values.jump_in_shape_gradients(j, qpoint) * n);
copy_data_face.cell_matrix(i, j) +=
MaterialParameters::stiffness_D *
(-av_hessian_i_dot_n_dot_n // - {grad^2 v n n }
* jump_grad_j_dot_n // * [grad u n]
//
-av_hessian_j_dot_n_dot_n // - {grad^2 u n n }
* jump_grad_i_dot_n // * [grad v n]
//
+
gamma_over_h * // + gamma_over_h
jump_grad_i_dot_n // * [grad v n]
* jump_grad_j_dot_n // * [grad u n]
) *
JxW[qpoint]; // dx
}