forked from adamantine-sim/adamantine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adamantine.hh
2236 lines (2032 loc) · 85 KB
/
adamantine.hh
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) 2016 - 2024, the adamantine authors.
*
* This file is subject to the Modified BSD License and may not be distributed
* without copyright and license information. Please refer to the file LICENSE
* for the text and further information on this license.
*/
#ifndef ADAMANTINE_HH
#define ADAMANTINE_HH
#include <DataAssimilator.hh>
#include <ExperimentalData.hh>
#include <Geometry.hh>
#include <MaterialProperty.hh>
#include <MechanicalPhysics.hh>
#include <PointCloud.hh>
#include <PostProcessor.hh>
#include <RayTracing.hh>
#include <ThermalPhysics.hh>
#include <ThermalPhysicsInterface.hh>
#include <Timer.hh>
#include <ensemble_management.hh>
#include <experimental_data_utils.hh>
#include <material_deposition.hh>
#include <types.hh>
#include <utils.hh>
#include <deal.II/base/index_set.h>
#include <deal.II/base/mpi.h>
#include <deal.II/base/symmetric_tensor.h>
#include <deal.II/base/types.h>
#include <deal.II/distributed/cell_data_transfer.templates.h>
#include <deal.II/distributed/solution_transfer.h>
#include <deal.II/grid/filtered_iterator.h>
#include <deal.II/grid/grid_refinement.h>
#include <deal.II/lac/trilinos_sparse_matrix.h>
#include <deal.II/lac/vector_operation.h>
#include <deal.II/numerics/error_estimator.h>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/property_tree/ptree.hpp>
#include <fstream>
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#ifdef ADAMANTINE_WITH_CALIPER
#include <caliper/cali.h>
#endif
#include <cmath>
#include <iostream>
template <int dim, typename MemorySpaceType,
std::enable_if_t<
std::is_same<MemorySpaceType, dealii::MemorySpace::Host>::value,
int> = 0>
void output_pvtu(
adamantine::PostProcessor<dim> &post_processor, unsigned int n_time_step,
double time,
std::unique_ptr<
adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>> const
&thermal_physics,
dealii::LinearAlgebra::distributed::Vector<double, MemorySpaceType>
&temperature,
std::unique_ptr<adamantine::MechanicalPhysics<dim, MemorySpaceType>> const
&mechanical_physics,
dealii::LA::distributed::Vector<double, dealii::MemorySpace::Host>
&displacement,
adamantine::MaterialProperty<dim, MemorySpaceType> const
&material_properties,
std::vector<adamantine::Timer> &timers)
{
#ifdef ADAMANTINE_WITH_CALIPER
CALI_CXX_MARK_FUNCTION;
#endif
timers[adamantine::output].start();
if (thermal_physics)
{
thermal_physics->get_affine_constraints().distribute(temperature);
if (mechanical_physics)
{
mechanical_physics->get_affine_constraints().distribute(displacement);
post_processor.template write_output<typename Kokkos::View<
double **, typename MemorySpaceType::kokkos_space>::array_layout>(
n_time_step, time, temperature, displacement,
mechanical_physics->get_stress_tensor(),
material_properties.get_state(), material_properties.get_dofs_map(),
material_properties.get_dof_handler());
}
else
{
post_processor.template write_thermal_output<typename Kokkos::View<
double **, typename MemorySpaceType::kokkos_space>::array_layout>(
n_time_step, time, temperature, material_properties.get_state(),
material_properties.get_dofs_map(),
material_properties.get_dof_handler());
}
}
else
{
mechanical_physics->get_affine_constraints().distribute(displacement);
post_processor.template write_mechanical_output<typename Kokkos::View<
double **, typename MemorySpaceType::kokkos_space>::array_layout>(
n_time_step, time, displacement,
mechanical_physics->get_stress_tensor(),
material_properties.get_state(), material_properties.get_dofs_map(),
material_properties.get_dof_handler());
}
timers[adamantine::output].stop();
}
template <int dim, typename MemorySpaceType,
std::enable_if_t<std::is_same<MemorySpaceType,
dealii::MemorySpace::Default>::value,
int> = 0>
void output_pvtu(
adamantine::PostProcessor<dim> &post_processor, unsigned int n_time_step,
double time,
std::unique_ptr<
adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>> const
&thermal_physics,
dealii::LinearAlgebra::distributed::Vector<double, MemorySpaceType>
&temperature,
std::unique_ptr<adamantine::MechanicalPhysics<dim, MemorySpaceType>> const
&mechanical_physics,
dealii::LA::distributed::Vector<double, dealii::MemorySpace::Host>
&displacement,
adamantine::MaterialProperty<dim, MemorySpaceType> const
&material_properties,
std::vector<adamantine::Timer> &timers)
{
#ifdef ADAMANTINE_WITH_CALIPER
CALI_CXX_MARK_FUNCTION;
#endif
timers[adamantine::output].start();
auto state = material_properties.get_state();
auto state_host = Kokkos::create_mirror_view_and_copy(
dealii::MemorySpace::Host::kokkos_space{}, state);
if (thermal_physics)
{
dealii::LinearAlgebra::distributed::Vector<double,
dealii::MemorySpace::Host>
temperature_host(temperature.get_partitioner());
temperature_host.import(temperature, dealii::VectorOperation::insert);
thermal_physics->get_affine_constraints().distribute(temperature_host);
if (mechanical_physics)
{
mechanical_physics->get_affine_constraints().distribute(displacement);
post_processor.template write_output<typename Kokkos::View<
double **, typename MemorySpaceType::kokkos_space>::array_layout>(
n_time_step, time, temperature_host, displacement,
mechanical_physics->get_stress_tensor(), state_host,
material_properties.get_dofs_map(),
material_properties.get_dof_handler());
}
else
{
post_processor.template write_thermal_output<typename Kokkos::View<
double **, typename MemorySpaceType::kokkos_space>::array_layout>(
n_time_step, time, temperature_host, state_host,
material_properties.get_dofs_map(),
material_properties.get_dof_handler());
}
}
else
{
mechanical_physics->get_affine_constraints().distribute(displacement);
post_processor.template write_mechanical_output<typename Kokkos::View<
double **, typename MemorySpaceType::kokkos_space>::array_layout>(
n_time_step, time, displacement,
mechanical_physics->get_stress_tensor(), state_host,
material_properties.get_dofs_map(),
material_properties.get_dof_handler());
}
timers[adamantine::output].stop();
}
template <int dim, typename MemorySpaceType,
std::enable_if_t<
std::is_same<MemorySpaceType, dealii::MemorySpace::Host>::value,
int> = 0>
dealii::Vector<float> estimate_error(
dealii::parallel::distributed::Triangulation<dim> const &triangulation,
dealii::DoFHandler<dim> const &dof_handler, int fe_degree,
dealii::LA::distributed::Vector<double, MemorySpaceType> const &solution)
{
dealii::Vector<float> estimated_error_per_cell(
triangulation.n_active_cells());
dealii::KellyErrorEstimator<dim>::estimate(
dof_handler, dealii::QGauss<dim - 1>(fe_degree + 1),
std::map<dealii::types::boundary_id,
const dealii::Function<dim, double> *>(),
solution, estimated_error_per_cell, dealii::ComponentMask(), nullptr, 0,
triangulation.locally_owned_subdomain());
return estimated_error_per_cell;
}
template <int dim, typename MemorySpaceType,
std::enable_if_t<std::is_same<MemorySpaceType,
dealii::MemorySpace::Default>::value,
int> = 0>
dealii::Vector<float> estimate_error(
dealii::parallel::distributed::Triangulation<dim> const &triangulation,
dealii::DoFHandler<dim> const &dof_handler, int fe_degree,
dealii::LA::distributed::Vector<double, MemorySpaceType> const &solution)
{
dealii::LA::distributed::Vector<double, dealii::MemorySpace::Host>
solution_host(solution.get_partitioner());
solution_host.import(solution, dealii::VectorOperation::insert);
dealii::Vector<float> estimated_error_per_cell(
triangulation.n_active_cells());
dealii::KellyErrorEstimator<dim>::estimate(
dof_handler, dealii::QGauss<dim - 1>(fe_degree + 1),
std::map<dealii::types::boundary_id,
const dealii::Function<dim, double> *>(),
solution_host, estimated_error_per_cell, dealii::ComponentMask(), nullptr,
0, triangulation.locally_owned_subdomain());
return estimated_error_per_cell;
}
// inlining this function so we can have in the header
inline void initialize_timers(MPI_Comm const &communicator,
std::vector<adamantine::Timer> &timers)
{
timers.push_back(adamantine::Timer(communicator, "Main"));
timers.push_back(adamantine::Timer(communicator, "Refinement"));
timers.push_back(adamantine::Timer(communicator, "Add Material, Search"));
timers.push_back(adamantine::Timer(communicator, "Add Material, Activate"));
timers.push_back(
adamantine::Timer(communicator, "Data Assimilation, Exp. Data"));
timers.push_back(
adamantine::Timer(communicator, "Data Assimilation, DOF Mapping"));
timers.push_back(
adamantine::Timer(communicator, "Data Assimilation, Cov. Sparsity"));
timers.push_back(
adamantine::Timer(communicator, "Data Assimilation, Exp. Cov."));
timers.push_back(
adamantine::Timer(communicator, "Data Assimilation, Update Ensemble"));
timers.push_back(adamantine::Timer(communicator, "Evolve One Time Step"));
timers.push_back(adamantine::Timer(
communicator, "Evolve One Time Step: evaluate_thermal_physics"));
timers.push_back(adamantine::Timer(
communicator, "Evolve One Time Step: id_minus_tau_J_inverse"));
timers.push_back(adamantine::Timer(
communicator, "Evolve One Time Step: evaluate_material_properties"));
timers.push_back(adamantine::Timer(communicator, "Output"));
}
template <int dim, int fe_degree, typename MemorySpaceType,
typename QuadratureType>
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
initialize(
MPI_Comm const &communicator, boost::property_tree::ptree const &database,
adamantine::Geometry<dim> &geometry,
adamantine::MaterialProperty<dim, MemorySpaceType> &material_properties)
{
return std::make_unique<adamantine::ThermalPhysics<
dim, fe_degree, MemorySpaceType, QuadratureType>>(
communicator, database, geometry, material_properties);
}
template <int dim, int fe_degree, typename MemorySpaceType>
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
initialize_quadrature(
std::string const &quadrature_type, MPI_Comm const &communicator,
boost::property_tree::ptree const &database,
adamantine::Geometry<dim> &geometry,
adamantine::MaterialProperty<dim, MemorySpaceType> &material_properties)
{
if (quadrature_type.compare("gauss") == 0)
return initialize<dim, fe_degree, MemorySpaceType, dealii::QGauss<1>>(
communicator, database, geometry, material_properties);
else
{
adamantine::ASSERT_THROW(quadrature_type.compare("lobatto") == 0,
"quadrature should be Gauss or Lobatto.");
return initialize<dim, fe_degree, MemorySpaceType,
dealii::QGaussLobatto<1>>(communicator, database,
geometry, material_properties);
}
}
template <int dim, typename MemorySpaceType>
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
initialize_thermal_physics(
unsigned int fe_degree, std::string const &quadrature_type,
MPI_Comm const &communicator, boost::property_tree::ptree const &database,
adamantine::Geometry<dim> &geometry,
adamantine::MaterialProperty<dim, MemorySpaceType> &material_properties)
{
switch (fe_degree)
{
case 1:
{
return initialize_quadrature<dim, 1, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 2:
{
return initialize_quadrature<dim, 2, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 3:
{
return initialize_quadrature<dim, 3, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 4:
{
return initialize_quadrature<dim, 4, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 5:
{
return initialize_quadrature<dim, 5, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 6:
{
return initialize_quadrature<dim, 6, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 7:
{
return initialize_quadrature<dim, 7, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 8:
{
return initialize_quadrature<dim, 8, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
case 9:
{
return initialize_quadrature<dim, 9, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
default:
{
adamantine::ASSERT_THROW(fe_degree == 10,
"fe_degree should be between 1 and 10.");
return initialize_quadrature<dim, 10, MemorySpaceType>(
quadrature_type, communicator, database, geometry, material_properties);
}
}
}
template <int dim, typename MemorySpaceType>
void refine_and_transfer(
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
&thermal_physics,
adamantine::MaterialProperty<dim, MemorySpaceType> &material_properties,
dealii::DoFHandler<dim> &dof_handler,
dealii::LA::distributed::Vector<double, MemorySpaceType> &solution)
{
#ifdef ADAMANTINE_WITH_CALIPER
CALI_CXX_MARK_FUNCTION;
#endif
// TODO transfer mechanical data is present, need to be aware that the data
// does not exist on some cells because it's liquid
dealii::parallel::distributed::Triangulation<dim> &triangulation =
dynamic_cast<dealii::parallel::distributed::Triangulation<dim> &>(
const_cast<dealii::Triangulation<dim> &>(
dof_handler.get_triangulation()));
// Update the material state from the ThermalOperator to MaterialProperty
// because, for now, we need to use state from MaterialProperty to perform the
// transfer to the refined mesh.
thermal_physics->set_state_to_material_properties();
// Transfer of the solution
dealii::parallel::distributed::SolutionTransfer<
dim, dealii::LA::distributed::Vector<double, dealii::MemorySpace::Host>>
solution_transfer(dof_handler);
// Transfer material state
unsigned int const direction_data_size = 2;
unsigned int const phase_history_data_size = 1;
unsigned int constexpr n_material_states = adamantine::g_n_material_states;
std::vector<std::vector<double>> data_to_transfer;
std::vector<double> dummy_cell_data(n_material_states + direction_data_size +
phase_history_data_size,
std::numeric_limits<double>::infinity());
auto state_host = Kokkos::create_mirror_view_and_copy(
Kokkos::HostSpace{}, material_properties.get_state());
unsigned int cell_id = 0;
unsigned int activated_cell_id = 0;
for (auto const &cell : dof_handler.active_cell_iterators())
{
if (cell->is_locally_owned())
{
std::vector<double> cell_data(n_material_states + direction_data_size +
phase_history_data_size);
for (unsigned int i = 0; i < n_material_states; ++i)
cell_data[i] = state_host(i, cell_id);
if (cell->active_fe_index() == 0)
{
cell_data[n_material_states] =
thermal_physics->get_deposition_cos(activated_cell_id);
cell_data[n_material_states + 1] =
thermal_physics->get_deposition_sin(activated_cell_id);
if (thermal_physics->get_has_melted(activated_cell_id))
cell_data[n_material_states + direction_data_size] = 1.0;
else
cell_data[n_material_states + direction_data_size] = 0.0;
++activated_cell_id;
}
else
{
cell_data[n_material_states] = std::numeric_limits<double>::infinity();
cell_data[n_material_states + 1] =
std::numeric_limits<double>::infinity();
cell_data[n_material_states + direction_data_size] =
std::numeric_limits<double>::infinity();
}
data_to_transfer.push_back(cell_data);
++cell_id;
}
else
{
data_to_transfer.push_back(dummy_cell_data);
}
}
// Prepare the Triangulation and the diffent data transfer objects for
// refinement
triangulation.prepare_coarsening_and_refinement();
// Prepare for refinement of the solution
dealii::LA::distributed::Vector<double, dealii::MemorySpace::Host>
solution_host;
if constexpr (std::is_same_v<MemorySpaceType, dealii::MemorySpace::Host>)
{
// We need to apply the constraints before the mesh transfer
thermal_physics->get_affine_constraints().distribute(solution);
// We need to update the ghost values before we can do the interpolation on
// the new mesh.
solution.update_ghost_values();
solution_transfer.prepare_for_coarsening_and_refinement(solution);
}
else
{
solution_host.reinit(solution.get_partitioner());
solution_host.import(solution, dealii::VectorOperation::insert);
// We need to apply the constraints before the mesh transfer
thermal_physics->get_affine_constraints().distribute(solution_host);
// We need to update the ghost values before we can do the interpolation on
// the new mesh.
solution_host.update_ghost_values();
solution_transfer.prepare_for_coarsening_and_refinement(solution_host);
}
dealii::parallel::distributed::CellDataTransfer<
dim, dim, std::vector<std::vector<double>>>
cell_data_trans(triangulation);
cell_data_trans.prepare_for_coarsening_and_refinement(data_to_transfer);
#ifdef ADAMANTINE_WITH_CALIPER
CALI_MARK_BEGIN("refine triangulation");
#endif
// Execute the refinement
triangulation.execute_coarsening_and_refinement();
#ifdef ADAMANTINE_WITH_CALIPER
CALI_MARK_END("refine triangulation");
#endif
// Update the AffineConstraints and resize the solution
thermal_physics->setup_dofs();
thermal_physics->initialize_dof_vector(0., solution);
// Update MaterialProperty DoFHandler and resize the state vectors
material_properties.reinit_dofs();
// Interpolate the solution
if constexpr (std::is_same_v<MemorySpaceType, dealii::MemorySpace::Host>)
{
solution_transfer.interpolate(solution);
}
else
{
solution_host.reinit(solution.get_partitioner());
solution_transfer.interpolate(solution_host);
solution.import(solution_host, dealii::VectorOperation::insert);
}
// Unpack the material state and repopulate the material state
std::vector<std::vector<double>> transferred_data(
triangulation.n_active_cells(),
std::vector<double>(n_material_states + direction_data_size +
phase_history_data_size));
cell_data_trans.unpack(transferred_data);
auto state = material_properties.get_state();
state_host = Kokkos::create_mirror_view(state);
unsigned int total_cell_id = 0;
cell_id = 0;
std::vector<double> transferred_cos;
std::vector<double> transferred_sin;
std::vector<bool> has_melted;
for (auto const &cell : dof_handler.active_cell_iterators())
{
if (cell->is_locally_owned())
{
for (unsigned int i = 0; i < n_material_states; ++i)
{
state_host(i, cell_id) = transferred_data[total_cell_id][i];
}
if (cell->active_fe_index() == 0)
{
transferred_cos.push_back(
transferred_data[total_cell_id][n_material_states]);
transferred_sin.push_back(
transferred_data[total_cell_id][n_material_states + 1]);
// Convert from double back to bool
if (transferred_data[total_cell_id]
[n_material_states + direction_data_size] > 0.5)
has_melted.push_back(true);
else
has_melted.push_back(false);
}
++cell_id;
}
++total_cell_id;
}
// Update the deposition cos and sin
thermal_physics->set_material_deposition_orientation(transferred_cos,
transferred_sin);
// Update the melted indicator
thermal_physics->set_has_melted_vector(has_melted);
// Copy the data back to material_property
Kokkos::deep_copy(state, state_host);
// Update the material states in the ThermalOperator
thermal_physics->get_state_from_material_properties();
#if ADAMANTINE_DEBUG
// Check that we are not losing material
cell_id = 0;
for (auto const &cell : dof_handler.active_cell_iterators())
{
if (cell->is_locally_owned())
{
double material_ratio = 0.;
for (unsigned int i = 0; i < n_material_states; ++i)
{
material_ratio += state_host(i, cell_id);
}
ASSERT(std::abs(material_ratio - 1.) < 1e-14, "Material is lost.");
++cell_id;
}
}
#endif
}
template <int dim>
std::vector<typename dealii::parallel::distributed::Triangulation<
dim>::active_cell_iterator>
compute_cells_to_refine(
dealii::parallel::distributed::Triangulation<dim> &triangulation,
double const time, double const next_refinement_time,
unsigned int const n_time_steps,
std::vector<std::shared_ptr<adamantine::HeatSource<dim>>> &heat_sources,
double const current_source_height, double const refinement_beam_cutoff)
{
// Compute the position of the beams between time and next_refinement_time and
// refine the mesh where the source is greater than refinement_beam_cutoff.
// This cut-off is due to the fact that the source is gaussian and thus never
// strictly zero. If the beams intersect, some cells will appear twice in the
// vector. This is not a problem.
std::vector<typename dealii::parallel::distributed::Triangulation<
dim>::active_cell_iterator>
cells_to_refine;
for (unsigned int i = 0; i < n_time_steps; ++i)
{
double const current_time = time + static_cast<double>(i) /
static_cast<double>(n_time_steps) *
(next_refinement_time - time);
for (auto &beam : heat_sources)
{
beam->update_time(current_time);
for (auto cell : dealii::filter_iterators(
triangulation.active_cell_iterators(),
dealii::IteratorFilters::LocallyOwnedCell()))
{
// Check the value at the center of the cell faces. For most cases this
// should be sufficient, but if the beam is small compared to the
// coarsest mesh we may need to add other points to check (e.g.
// quadrature points, vertices).
for (unsigned int f = 0; f < cell->reference_cell().n_faces(); ++f)
{
if (beam->value(cell->face(f)->center(), current_source_height) >
refinement_beam_cutoff)
{
cells_to_refine.push_back(cell);
break;
}
}
}
}
}
return cells_to_refine;
}
template <int dim, int fe_degree, typename MemorySpaceType>
void refine_mesh(
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
&thermal_physics,
adamantine::MaterialProperty<dim, MemorySpaceType> &material_properties,
dealii::LA::distributed::Vector<double, MemorySpaceType> &solution,
std::vector<std::shared_ptr<adamantine::HeatSource<dim>>> &heat_sources,
double const time, double const next_refinement_time,
unsigned int const time_steps_refinement,
boost::property_tree::ptree const &refinement_database)
{
#ifdef ADAMANTINE_WITH_CALIPER
CALI_CXX_MARK_FUNCTION;
#endif
dealii::DoFHandler<dim> &dof_handler = thermal_physics->get_dof_handler();
// Use the Kelly error estimator to refine the mesh. This is done so that the
// part of the domain that were heated stay refined.
// PropertyTreeInput refinement.n_heat_refinements
unsigned int const n_kelly_refinements =
refinement_database.get("n_heat_refinements", 2);
double coarsening_fraction = 0.3;
double refining_fraction = 0.6;
// PropertyTreeInput refinement.heat_cell_ratio
double cells_fraction = refinement_database.get("heat_cell_ratio", 1.);
dealii::parallel::distributed::Triangulation<dim> &triangulation =
dynamic_cast<dealii::parallel::distributed::Triangulation<dim> &>(
const_cast<dealii::Triangulation<dim> &>(
dof_handler.get_triangulation()));
// Number of times the mesh on the beam paths will be refined and maximum
// number of time a cell can be refined.
// PropertyTreeInput refinement.n_beam_refinements
unsigned int const n_beam_refinements =
refinement_database.get("n_beam_refinements", 2);
// PropertyTreeInput refinement.max_level
int max_level = refinement_database.get<int>("max_level");
// PropertyTreeInput refinement.beam_cutoff
const double refinement_beam_cutoff =
refinement_database.get<double>("beam_cutoff", 1.0e-15);
for (unsigned int i = 0; i < n_kelly_refinements; ++i)
{
// Estimate the error. For simplicity, always use dealii::QGauss
dealii::Vector<float> estimated_error_per_cell =
estimate_error(triangulation, dof_handler, fe_degree, solution);
// Flag the cells for refinement.
unsigned int new_n_cells = static_cast<unsigned int>(
cells_fraction *
static_cast<double>(triangulation.n_global_active_cells()));
dealii::GridRefinement::refine_and_coarsen_fixed_fraction(
triangulation, estimated_error_per_cell, refining_fraction,
coarsening_fraction, new_n_cells);
// Don't refine cells that are already as much refined as it is allowed.
for (auto cell :
dealii::filter_iterators(triangulation.active_cell_iterators(),
dealii::IteratorFilters::LocallyOwnedCell()))
if (cell->level() >= max_level)
cell->clear_refine_flag();
// Execute the refinement and transfer the solution onto the new mesh.
refine_and_transfer(thermal_physics, material_properties, dof_handler,
solution);
}
// Refine the mesh along the trajectory of the sources.
double current_source_height =
dynamic_cast<adamantine::ThermalPhysics<dim, fe_degree, MemorySpaceType,
dealii::QGauss<1>> *>(
thermal_physics.get())
? dynamic_cast<adamantine::ThermalPhysics<
dim, fe_degree, MemorySpaceType, dealii::QGauss<1>> *>(
thermal_physics.get())
->get_current_source_height()
: dynamic_cast<adamantine::ThermalPhysics<
dim, fe_degree, MemorySpaceType, dealii::QGaussLobatto<1>> *>(
thermal_physics.get())
->get_current_source_height();
for (unsigned int i = 0; i < n_beam_refinements; ++i)
{
// Compute the cells to be refined.
std::vector<typename dealii::parallel::distributed::Triangulation<
dim>::active_cell_iterator>
cells_to_refine = compute_cells_to_refine(
triangulation, time, next_refinement_time, time_steps_refinement,
heat_sources, current_source_height, refinement_beam_cutoff);
// PropertyTreeInput refinement.coarsen_after_beam
const bool coarsen_after_beam =
refinement_database.get<bool>("coarsen_after_beam", false);
// If coarsening is allowed, set the coarsening flag everywhere
if (coarsen_after_beam)
{
for (auto cell : dealii::filter_iterators(
triangulation.active_cell_iterators(),
dealii::IteratorFilters::LocallyOwnedCell()))
{
if (cell->level() > 0)
cell->set_coarsen_flag();
}
}
// Flag the cells for refinement.
for (auto &cell : cells_to_refine)
{
if (coarsen_after_beam)
cell->clear_coarsen_flag();
if (cell->level() < max_level)
cell->set_refine_flag();
}
// Execute the refinement and transfer the solution onto the new mesh.
refine_and_transfer(thermal_physics, material_properties, dof_handler,
solution);
}
// Recompute the inverse of the mass matrix
thermal_physics->compute_inverse_mass_matrix();
}
template <int dim, typename MemorySpaceType>
void refine_mesh(
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
&thermal_physics,
adamantine::MaterialProperty<dim, MemorySpaceType> &material_properties,
dealii::LA::distributed::Vector<double, MemorySpaceType> &solution,
std::vector<std::shared_ptr<adamantine::HeatSource<dim>>> &heat_sources,
double const time, double const next_refinement_time,
unsigned int const time_steps_refinement,
boost::property_tree::ptree const &refinement_database)
{
if (!thermal_physics)
return;
switch (thermal_physics->get_fe_degree())
{
case 1:
{
refine_mesh<dim, 1>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 2:
{
refine_mesh<dim, 2>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 3:
{
refine_mesh<dim, 3>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 4:
{
refine_mesh<dim, 4>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 5:
{
refine_mesh<dim, 5>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 6:
{
refine_mesh<dim, 6>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 7:
{
refine_mesh<dim, 7>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 8:
{
refine_mesh<dim, 8>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 9:
{
refine_mesh<dim, 9>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
case 10:
{
refine_mesh<dim, 10>(thermal_physics, material_properties, solution,
heat_sources, time, next_refinement_time,
time_steps_refinement, refinement_database);
break;
}
default:
{
adamantine::ASSERT_THROW(false, "fe_degree should be between 1 and 10.");
}
}
}
template <int dim, typename MemorySpaceType>
std::pair<dealii::LinearAlgebra::distributed::Vector<double,
dealii::MemorySpace::Host>,
dealii::LinearAlgebra::distributed::Vector<double,
dealii::MemorySpace::Host>>
run(MPI_Comm const &communicator, boost::property_tree::ptree const &database,
std::vector<adamantine::Timer> &timers)
{
#ifdef ADAMANTINE_WITH_CALIPER
CALI_CXX_MARK_FUNCTION;
#endif
// Create the Geometry
boost::property_tree::ptree geometry_database =
database.get_child("geometry");
adamantine::Geometry<dim> geometry(communicator, geometry_database);
// Create the MaterialProperty
boost::property_tree::ptree material_database =
database.get_child("materials");
adamantine::MaterialProperty<dim, MemorySpaceType> material_properties(
communicator, geometry.get_triangulation(), material_database);
// Extract the physics property tree
boost::property_tree::ptree physics_database = database.get_child("physics");
bool const use_thermal_physics = physics_database.get<bool>("thermal");
[[maybe_unused]] bool const use_mechanical_physics =
physics_database.get<bool>("mechanical");
// Extract the discretization property tree
boost::property_tree::ptree discretization_database =
database.get_child("discretization");
// Extract the post-processor property tree
boost::property_tree::ptree post_processor_database =
database.get_child("post_processor");
// Extract the verbosity
// PropertyTreeInput verbose_output
bool const verbose_output = database.get("verbose_output", false);
// Create ThermalPhysics if necessary
std::unique_ptr<adamantine::ThermalPhysicsInterface<dim, MemorySpaceType>>
thermal_physics;
std::vector<std::shared_ptr<adamantine::HeatSource<dim>>> heat_sources;
if (use_thermal_physics)
{
// PropertyTreeInput discretization.thermal.fe_degree
unsigned int const fe_degree =
discretization_database.get<unsigned int>("thermal.fe_degree");
// PropertyTreeInput discretization.thermal.quadrature
std::string quadrature_type =
discretization_database.get("thermal.quadrature", "gauss");
thermal_physics = initialize_thermal_physics<dim>(
fe_degree, quadrature_type, communicator, database, geometry,
material_properties);
heat_sources = thermal_physics->get_heat_sources();
post_processor_database.put("thermal_output", true);
}
// PropertyTreeInput materials.initial_temperature
double const initial_temperature =
material_database.get("initial_temperature", 300.);
// Get the reference temperature(s) for the substrate and deposited
// material(s)
std::vector<double> material_reference_temps;
// PropertyTreeInput materials.n_materials
unsigned int const n_materials =
material_database.get<unsigned int>("n_materials");
for (unsigned int i = 0; i < n_materials; ++i)
{
// Use the solidus as the reference temperature, in the future we may want
// to use something else
// PropertyTreeInput materials.material_n.solidus
double const reference_temperature = material_database.get<double>(
"material_" + std::to_string(i) + ".solidus");
material_reference_temps.push_back(reference_temperature);
}
material_reference_temps.push_back(initial_temperature);
// Create MechanicalPhysics
std::unique_ptr<adamantine::MechanicalPhysics<dim, MemorySpaceType>>
mechanical_physics;
if (use_mechanical_physics)
{
// PropertyTreeInput discretization.mechanical.fe_degree
unsigned int const fe_degree =
discretization_database.get<unsigned int>("mechanical.fe_degree");
mechanical_physics =
std::make_unique<adamantine::MechanicalPhysics<dim, MemorySpaceType>>(
communicator, fe_degree, geometry, material_properties,
material_reference_temps);
post_processor_database.put("mechanical_output", true);
}
// Create PostProcessor
std::unique_ptr<adamantine::PostProcessor<dim>> post_processor;
bool thermal_output = post_processor_database.get("thermal_output", false);
bool mechanical_output =
post_processor_database.get("mechanical_output", false);
if ((thermal_output) && (mechanical_output))
{
post_processor = std::make_unique<adamantine::PostProcessor<dim>>(
communicator, post_processor_database,
thermal_physics->get_dof_handler(),
mechanical_physics->get_dof_handler());
}
else
{
post_processor = std::make_unique<adamantine::PostProcessor<dim>>(
communicator, post_processor_database,
thermal_output ? thermal_physics->get_dof_handler()
: mechanical_physics->get_dof_handler());
}
// Get the checkpoint and restart subtrees
boost::optional<boost::property_tree::ptree const &>
checkpoint_optional_database = database.get_child_optional("checkpoint");
boost::optional<boost::property_tree::ptree const &>
restart_optional_database = database.get_child_optional("restart");
unsigned int time_steps_checkpoint = std::numeric_limits<unsigned int>::max();
std::string checkpoint_filename;
if (checkpoint_optional_database)
{
auto checkpoint_database = checkpoint_optional_database.get();
// PropertyTreeInput checkpoint.time_steps_between_checkpoint
time_steps_checkpoint =
checkpoint_database.get<unsigned int>("time_steps_between_checkpoint");
// PropertyTreeInput checkpoint.filename_prefix
checkpoint_filename =
checkpoint_database.get<std::string>("filename_prefix");
}
bool restart = false;
std::string restart_filename;
if (restart_optional_database)
{
restart = true;
auto restart_database = restart_optional_database.get();
// PropertyTreeInput restart.filename_prefix
restart_filename = restart_database.get<std::string>("filename_prefix");
}
dealii::LA::distributed::Vector<double, MemorySpaceType> temperature;
dealii::LA::distributed::Vector<double, dealii::MemorySpace::Host>
displacement;
if (use_thermal_physics)
{
if (restart == false)
{
thermal_physics->setup();
thermal_physics->initialize_dof_vector(initial_temperature, temperature);
}
else
{
#ifdef ADAMANTINE_WITH_CALIPER
CALI_MARK_BEGIN("restart from file");
#endif
thermal_physics->load_checkpoint(restart_filename, temperature);
#ifdef ADAMANTINE_WITH_CALIPER
CALI_MARK_END("restart from file");
#endif
}
}