-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathcrystal_preferred_orientation.cc
1288 lines (1099 loc) · 67.8 KB
/
crystal_preferred_orientation.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) 2022 - 2024 by the authors of the ASPECT code.
This file is part of ASPECT.
ASPECT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
ASPECT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ASPECT; see the file LICENSE. If not see
<http://www.gnu.org/licenses/>.
*/
#include <aspect/particle/property/crystal_preferred_orientation.h>
#include <aspect/geometry_model/interface.h>
#include <aspect/utilities.h>
#include <world_builder/grains.h>
#include <world_builder/world.h>
namespace aspect
{
namespace Particle
{
namespace Property
{
template <int dim>
void
CrystalPreferredOrientation<dim>::initialize ()
{
CitationInfo::add("CPO");
const unsigned int my_rank = Utilities::MPI::this_mpi_process(MPI_COMM_WORLD);
this->random_number_generator.seed(random_number_seed+my_rank);
// Don't assert when called by the unit tester.
if (this->simulator_is_past_initialization())
{
AssertThrow(this->introspection().compositional_name_exists("water"),
ExcMessage("Particle property CPO only works if"
"there is a compositional field called water."));
water_index = this->introspection().compositional_index_for_name("water");
}
}
template <int dim>
void
CrystalPreferredOrientation<dim>::compute_random_rotation_matrix(Tensor<2,3> &rotation_matrix) const
{
// This function is based on an article in Graphic Gems III, written by James Arvo, Cornell University (p 116-120).
// The original code can be found on http://www.realtimerendering.com/resources/GraphicsGems/gemsiii/rand_rotation.c
// and is licenced according to this website with the following licence:
//
// "The Graphics Gems code is copyright-protected. In other words, you cannot claim the text of the code as your own and
// resell it. Using the code is permitted in any program, product, or library, non-commercial or commercial. Giving credit
// is not required, though is a nice gesture. The code comes as-is, and if there are any flaws or problems with any Gems
// code, nobody involved with Gems - authors, editors, publishers, or webmasters - are to be held responsible. Basically,
// don't be a jerk, and remember that anything free comes with no guarantee.""
//
// The book states in the preface the following: "As in the first two volumes, all of the C and C++ code in this book is in
// the public domain, and is yours to study, modify, and use."
// first generate three random numbers between 0 and 1 and multiply them with 2 PI or 2 for z. Note that these are not the same as phi_1, theta and phi_2.
boost::random::uniform_real_distribution<double> uniform_distribution(0,1);
double one = uniform_distribution(this->random_number_generator);
double two = uniform_distribution(this->random_number_generator);
double three = uniform_distribution(this->random_number_generator);
double theta = 2.0 * M_PI * one; // Rotation about the pole (Z)
double phi = 2.0 * M_PI * two; // For direction of pole deflection.
double z = 2.0* three; //For magnitude of pole deflection.
// Compute a vector V used for distributing points over the sphere
// via the reflection I - V Transpose(V). This formulation of V
// will guarantee that if x[1] and x[2] are uniformly distributed,
// the reflected points will be uniform on the sphere. Note that V
// has length sqrt(2) to eliminate the 2 in the Householder matrix.
double r = std::sqrt( z );
double Vx = std::sin( phi ) * r;
double Vy = std::cos( phi ) * r;
double Vz = std::sqrt( 2.f - z );
// Compute the row vector S = Transpose(V) * R, where R is a simple
// rotation by theta about the z-axis. No need to compute Sz since
// it's just Vz.
double st = std::sin( theta );
double ct = std::cos( theta );
double Sx = Vx * ct - Vy * st;
double Sy = Vx * st + Vy * ct;
// Construct the rotation matrix ( V Transpose(V) - I ) R, which
// is equivalent to V S - R.
rotation_matrix[0][0] = Vx * Sx - ct;
rotation_matrix[0][1] = Vx * Sy - st;
rotation_matrix[0][2] = Vx * Vz;
rotation_matrix[1][0] = Vy * Sx + st;
rotation_matrix[1][1] = Vy * Sy - ct;
rotation_matrix[1][2] = Vy * Vz;
rotation_matrix[2][0] = Vz * Sx;
rotation_matrix[2][1] = Vz * Sy;
rotation_matrix[2][2] = 1.0 - z; // This equals Vz * Vz - 1.0
}
template <int dim>
void
CrystalPreferredOrientation<dim>::initialize_one_particle_property(const Point<dim> &position,
std::vector<double> &data) const
{
// the layout of the data vector per particle is the following:
// 1. M mineral times
// 1.1 olivine deformation type -> 1 double, at location
// => data_position + 0 + mineral_i * (n_grains * 10 + 2)
// 2.1. Mineral volume fraction -> 1 double, at location
// => data_position + 1 + mineral_i *(n_grains * 10 + 2)
// 2.2. N grains times:
// 2.1. volume fraction grain -> 1 double, at location:
// => data_position + 2 + i_grain * 10 + mineral_i *(n_grains * 10 + 2), or
// => data_position + 2 + i_grain * (2 * Tensor<2,3>::n_independent_components+ 2) + mineral_i * (n_grains * 10 + 2)
// 2.2. rotation matrix grain -> 9 (Tensor<2,dim>::n_independent_components) doubles, starts at:
// => data_position + 3 + i_grain * 10 + mineral_i * (n_grains * 10 + 2), or
// => data_position + 3 + i_grain * (2 * Tensor<2,3>::n_independent_components+ 2) + mineral_i * (n_grains * 10 + 2)
//
// Note that we store exactly the same number of grains of all minerals (e.g. olivine and enstatite
// grains), although their volume fractions may not be the same. We need a minimum amount
// of grains per particle to perform reliable statistics on it. This minimum is the same for all phases.
// and enstatite.
//
// Furthermore, for this plugin the following dims are always 3. When using 2d an infinitely thin 3d domain is assumed.
//
// The rotation matrix is a direction cosine matrix, representing the orientation of the grain in the domain.
// The fabric is determined later in the computations, so initialize it to -1.
std::vector<double> deformation_type(n_minerals, -1.0);
std::vector<std::vector<double >>volume_fractions_grains(n_minerals);
std::vector<std::vector<Tensor<2,3>>> rotation_matrices_grains(n_minerals);
for (unsigned int mineral_i = 0; mineral_i < n_minerals; ++mineral_i)
{
volume_fractions_grains[mineral_i].resize(n_grains);
rotation_matrices_grains[mineral_i].resize(n_grains);
// This will be set by the initial grain subsection.
if (initial_grains_model == CPOInitialGrainsModel::world_builder)
{
#ifdef ASPECT_WITH_WORLD_BUILDER
WorldBuilder::grains wb_grains = this->get_world_builder().grains(Utilities::convert_point_to_array(position),
-this->get_geometry_model().height_above_reference_surface(position),
mineral_i,
n_grains);
double sum_volume_fractions = 0;
for (unsigned int grain_i = 0; grain_i < n_grains ; ++grain_i)
{
sum_volume_fractions += wb_grains.sizes[grain_i];
volume_fractions_grains[mineral_i][grain_i] = wb_grains.sizes[grain_i];
// we are receiving a array<array<double,3>,3> from the world builder,
// which needs to be copied in the correct way into a tensor<2,3>.
for (unsigned int component_i = 0; component_i < 3 ; ++component_i)
{
for (unsigned int component_j = 0; component_j < 3 ; ++component_j)
{
Assert(!std::isnan(wb_grains.rotation_matrices[grain_i][component_i][component_j]), ExcMessage("Error: not a number."));
rotation_matrices_grains[mineral_i][grain_i][component_i][component_j] = wb_grains.rotation_matrices[grain_i][component_i][component_j];
}
}
}
AssertThrow(sum_volume_fractions != 0, ExcMessage("Sum of volumes is equal to zero, which is not supposed to happen. "
"Make sure that all parts of the domain which contain particles are covered by the world builder."));
#else
AssertThrow(false,
ExcMessage("The world builder was requested but not provided. Make sure that aspect is "
"compiled with the World Builder and that you provide a world builder file in the input."));
#endif
}
else
{
// set volume fraction
const double initial_volume_fraction = 1.0/n_grains;
for (unsigned int grain_i = 0; grain_i < n_grains ; ++grain_i)
{
// set volume fraction
volume_fractions_grains[mineral_i][grain_i] = initial_volume_fraction;
// set a uniform random rotation_matrix per grain
this->compute_random_rotation_matrix(rotation_matrices_grains[mineral_i][grain_i]);
}
}
}
for (unsigned int mineral_i = 0; mineral_i < n_minerals; ++mineral_i)
{
data.emplace_back(deformation_type[mineral_i]);
data.emplace_back(volume_fractions_minerals[mineral_i]);
for (unsigned int grain_i = 0; grain_i < n_grains ; ++grain_i)
{
data.emplace_back(volume_fractions_grains[mineral_i][grain_i]);
for (unsigned int i = 0; i < Tensor<2,3>::n_independent_components ; ++i)
{
const dealii::TableIndices<2> index = Tensor<2,3>::unrolled_to_component_indices(i);
data.emplace_back(rotation_matrices_grains[mineral_i][grain_i][index]);
}
}
}
}
template <int dim>
void
CrystalPreferredOrientation<dim>::update_particle_properties(const ParticleUpdateInputs<dim> &inputs,
typename ParticleHandler<dim>::particle_iterator_range &particles) const
{
const unsigned int data_position = this->data_position;
std::vector<double> compositions(this->n_compositional_fields());
unsigned int p = 0;
for (auto &particle: particles)
{
// STEP 1: Load data and preprocess it.
// need access to the pressure, viscosity,
// get velocity
Tensor<1,dim> velocity;
for (unsigned int i = 0; i < dim; ++i)
velocity[i] = inputs.solution[p][this->introspection().component_indices.velocities[i]];
// get velocity gradient tensor.
Tensor<2,dim> velocity_gradient;
for (unsigned int i = 0; i < dim; ++i)
velocity_gradient[i] = inputs.gradients[p][this->introspection().component_indices.velocities[i]];
// Calculate strain rate from velocity gradients
const SymmetricTensor<2,dim> strain_rate = symmetrize (velocity_gradient);
const SymmetricTensor<2,dim> deviatoric_strain_rate
= (this->get_material_model().is_compressible()
?
strain_rate - 1./3 * trace(strain_rate) * unit_symmetric_tensor<dim>()
:
strain_rate);
const double pressure = inputs.solution[p][this->introspection().component_indices.pressure];
const double temperature = inputs.solution[p][this->introspection().component_indices.temperature];
const double water_content = inputs.solution[p][this->introspection().component_indices.compositional_fields[water_index]];
// get the composition of the particle
for (unsigned int i = 0; i < this->n_compositional_fields(); ++i)
{
const unsigned int solution_component = this->introspection().component_indices.compositional_fields[i];
compositions[i] = inputs.solution[p][solution_component];
}
const double dt = this->get_timestep();
// even in 2d we need 3d strain-rates and velocity gradient tensors. So we make them 3d by
// adding an extra dimension which is zero.
SymmetricTensor<2,3> strain_rate_3d;
strain_rate_3d[0][0] = strain_rate[0][0];
strain_rate_3d[0][1] = strain_rate[0][1];
//sym: strain_rate_3d[1][0] = strain_rate[1][0];
strain_rate_3d[1][1] = strain_rate[1][1];
if (dim == 3)
{
strain_rate_3d[0][2] = strain_rate[0][2];
strain_rate_3d[1][2] = strain_rate[1][2];
//sym: strain_rate_3d[2][0] = strain_rate[0][2];
//sym: strain_rate_3d[2][1] = strain_rate[1][2];
strain_rate_3d[2][2] = strain_rate[2][2];
}
Tensor<2,3> velocity_gradient_3d;
velocity_gradient_3d[0][0] = velocity_gradient[0][0];
velocity_gradient_3d[0][1] = velocity_gradient[0][1];
velocity_gradient_3d[1][0] = velocity_gradient[1][0];
velocity_gradient_3d[1][1] = velocity_gradient[1][1];
if (dim == 3)
{
velocity_gradient_3d[0][2] = velocity_gradient[0][2];
velocity_gradient_3d[1][2] = velocity_gradient[1][2];
velocity_gradient_3d[2][0] = velocity_gradient[2][0];
velocity_gradient_3d[2][1] = velocity_gradient[2][1];
velocity_gradient_3d[2][2] = velocity_gradient[2][2];
}
ArrayView<double> data = particle.get_properties();
const typename DoFHandler<dim>::active_cell_iterator cell(*particle.get_surrounding_cell(),&(this->get_dof_handler()));
for (unsigned int mineral_i = 0; mineral_i < n_minerals; ++mineral_i)
{
/**
* Now we have loaded all the data and can do the actual computation.
* The computation consists of two parts. The first part is computing
* the derivatives for the directions and grain sizes. Then those
* derivatives are used to advect the particle properties.
*/
double sum_volume_mineral = 0;
std::pair<std::vector<double>, std::vector<Tensor<2,3>>>
derivatives_grains = this->compute_derivatives(data_position,
data,
mineral_i,
strain_rate_3d,
velocity_gradient_3d,
particle.get_location(),
cell,
temperature,
pressure,
velocity,
compositions,
strain_rate,
deviatoric_strain_rate,
water_content);
switch (advection_method)
{
case AdvectionMethod::forward_euler:
sum_volume_mineral = this->advect_forward_euler(data_position,
data,
mineral_i,
dt,
derivatives_grains);
break;
case AdvectionMethod::backward_euler:
sum_volume_mineral = this->advect_backward_euler(data_position,
data,
mineral_i,
dt,
derivatives_grains);
break;
}
// normalize the volume fractions back to a total of 1 for each mineral
const double inv_sum_volume_mineral = 1.0/sum_volume_mineral;
Assert(std::isfinite(inv_sum_volume_mineral),
ExcMessage("inv_sum_volume_mineral is not finite. sum_volume_enstatite = "
+ std::to_string(sum_volume_mineral)));
for (unsigned int grain_i = 0; grain_i < n_grains; ++grain_i)
{
const double volume_fraction_grains = get_volume_fractions_grains(data_position,data,mineral_i,grain_i)*inv_sum_volume_mineral;
set_volume_fractions_grains(data_position,data,mineral_i,grain_i,volume_fraction_grains);
Assert(isfinite(get_volume_fractions_grains(data_position,data,mineral_i,grain_i)),
ExcMessage("volume_fractions_grains[mineral_i]" + std::to_string(grain_i) + "] is not finite: "
+ std::to_string(get_volume_fractions_grains(data_position,data,mineral_i,grain_i)) + ", inv_sum_volume_mineral = "
+ std::to_string(inv_sum_volume_mineral) + "."));
/**
* Correct direction rotation matrices numerical error (orthnormality) after integration
* Follows same method as in matlab version from Thissen (see https://github.com/cthissen/Drex-MATLAB/)
* of finding the nearest orthonormal matrix using the SVD
*/
Tensor<2,3> rotation_matrix = get_rotation_matrix_grains(data_position,data,mineral_i,grain_i);
for (size_t i = 0; i < 3; ++i)
{
for (size_t j = 0; j < 3; ++j)
{
Assert(!std::isnan(rotation_matrix[i][j]), ExcMessage("rotation_matrix is nan before orthogonalization."));
}
}
rotation_matrix = dealii::project_onto_orthogonal_tensors(rotation_matrix);
for (size_t i = 0; i < 3; ++i)
for (size_t j = 0; j < 3; ++j)
{
// I don't think this should happen with the projection, but D-Rex
// does not do the orthogonal projection, but just clamps the values
// to 1 and -1.
Assert(std::fabs(rotation_matrix[i][j]) <= 1.0,
ExcMessage("The rotation_matrix has a entry larger than 1."));
Assert(!std::isnan(rotation_matrix[i][j]),
ExcMessage("rotation_matrix is nan after orthoganalization: "
+ std::to_string(rotation_matrix[i][j])));
Assert(std::abs(rotation_matrix[i][j]) <= 1.0,
ExcMessage("3. rotation_matrix[" + std::to_string(i) + "][" + std::to_string(j) +
"] is larger than one: "
+ std::to_string(rotation_matrix[i][j]) + " (" + std::to_string(rotation_matrix[i][j]-1.0) + "). rotation_matrix = \n"
+ std::to_string(rotation_matrix[0][0]) + " " + std::to_string(rotation_matrix[0][1]) + " " + std::to_string(rotation_matrix[0][2]) + "\n"
+ std::to_string(rotation_matrix[1][0]) + " " + std::to_string(rotation_matrix[1][1]) + " " + std::to_string(rotation_matrix[1][2]) + "\n"
+ std::to_string(rotation_matrix[2][0]) + " " + std::to_string(rotation_matrix[2][1]) + " " + std::to_string(rotation_matrix[2][2])));
}
}
}
++p;
}
}
template <int dim>
UpdateTimeFlags
CrystalPreferredOrientation<dim>::need_update() const
{
return update_time_step;
}
template <int dim>
InitializationModeForLateParticles
CrystalPreferredOrientation<dim>::late_initialization_mode () const
{
return InitializationModeForLateParticles::interpolate;
}
template <int dim>
UpdateFlags
CrystalPreferredOrientation<dim>::get_update_flags (const unsigned int component) const
{
if (this->introspection().component_masks.velocities[component] == true)
return update_values | update_gradients;
return update_values;
}
template <int dim>
std::vector<std::pair<std::string, unsigned int>>
CrystalPreferredOrientation<dim>::get_property_information() const
{
std::vector<std::pair<std::string,unsigned int>> property_information;
property_information.reserve(n_minerals * n_grains * (1+Tensor<2,3>::n_independent_components));
for (unsigned int mineral_i = 0; mineral_i < n_minerals; ++mineral_i)
{
property_information.emplace_back("cpo mineral " + std::to_string(mineral_i) + " type",1);
property_information.emplace_back("cpo mineral " + std::to_string(mineral_i) + " volume fraction",1);
for (unsigned int grain_i = 0; grain_i < n_grains; ++grain_i)
{
property_information.emplace_back("cpo mineral " + std::to_string(mineral_i) + " grain " + std::to_string(grain_i) + " volume fraction",1);
for (unsigned int index = 0; index < Tensor<2,3>::n_independent_components; ++index)
{
property_information.emplace_back("cpo mineral " + std::to_string(mineral_i) + " grain " + std::to_string(grain_i) + " rotation_matrix " + std::to_string(index),1);
}
}
}
return property_information;
}
template <int dim>
double
CrystalPreferredOrientation<dim>::advect_forward_euler(const unsigned int cpo_index,
const ArrayView<double> &data,
const unsigned int mineral_i,
const double dt,
const std::pair<std::vector<double>, std::vector<Tensor<2,3>>> &derivatives) const
{
double sum_volume_fractions = 0;
Tensor<2,3> rotation_matrix;
for (unsigned int grain_i = 0; grain_i < n_grains; ++grain_i)
{
// Do the volume fraction of the grain
Assert(std::isfinite(get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i)),ExcMessage("volume_fractions[grain_i] is not finite before it is set."));
double volume_fraction_grains = get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i);
volume_fraction_grains = volume_fraction_grains + dt * volume_fraction_grains * derivatives.first[grain_i];
set_volume_fractions_grains(cpo_index,data,mineral_i,grain_i, volume_fraction_grains);
Assert(std::isfinite(get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i)),ExcMessage("volume_fractions[grain_i] is not finite. grain_i = "
+ std::to_string(grain_i) + ", volume_fractions[grain_i] = " + std::to_string(get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i))
+ ", derivatives.first[grain_i] = " + std::to_string(derivatives.first[grain_i])));
sum_volume_fractions += get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i);
// Do the rotation matrix for this grain
rotation_matrix = get_rotation_matrix_grains(cpo_index,data,mineral_i,grain_i);
rotation_matrix += dt * rotation_matrix * derivatives.second[grain_i];
set_rotation_matrix_grains(cpo_index,data,mineral_i,grain_i,rotation_matrix);
}
Assert(sum_volume_fractions != 0, ExcMessage("The sum of all grain volume fractions of a mineral is equal to zero. This should not happen."));
return sum_volume_fractions;
}
template <int dim>
double
CrystalPreferredOrientation<dim>::advect_backward_euler(const unsigned int cpo_index,
const ArrayView<double> &data,
const unsigned int mineral_i,
const double dt,
const std::pair<std::vector<double>, std::vector<Tensor<2,3>>> &derivatives) const
{
double sum_volume_fractions = 0;
Tensor<2,3> cosine_ref;
for (unsigned int grain_i = 0; grain_i < n_grains; ++grain_i)
{
// Do the volume fraction of the grain
double vf_old = get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i);
double vf_new = get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i);
Assert(std::isfinite(vf_new),ExcMessage("vf_new is not finite before it is set."));
for (size_t iteration = 0; iteration < property_advection_max_iterations; ++iteration)
{
Assert(std::isfinite(vf_new),ExcMessage("vf_new is not finite before it is set. grain_i = "
+ std::to_string(grain_i) + ", volume_fractions[grain_i] = " + std::to_string(get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i))
+ ", derivatives.first[grain_i] = " + std::to_string(derivatives.first[grain_i])));
vf_new = get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i) + dt * vf_new * derivatives.first[grain_i];
Assert(std::isfinite(get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i)),ExcMessage("volume_fractions[grain_i] is not finite. grain_i = "
+ std::to_string(grain_i) + ", volume_fractions[grain_i] = " + std::to_string(get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i))
+ ", derivatives.first[grain_i] = " + std::to_string(derivatives.first[grain_i])));
if (std::fabs(vf_new-vf_old) < property_advection_tolerance)
{
break;
}
vf_old = vf_new;
}
set_volume_fractions_grains(cpo_index,data,mineral_i,grain_i,vf_new);
sum_volume_fractions += vf_new;
// Do the rotation matrix for this grain
cosine_ref = get_rotation_matrix_grains(cpo_index,data,mineral_i,grain_i);
Tensor<2,3> cosine_old = cosine_ref;
Tensor<2,3> cosine_new = cosine_ref;
for (size_t iteration = 0; iteration < property_advection_max_iterations; ++iteration)
{
cosine_new = cosine_ref + dt * cosine_new * derivatives.second[grain_i];
if ((cosine_new-cosine_old).norm() < property_advection_tolerance)
{
break;
}
cosine_old = cosine_new;
}
set_rotation_matrix_grains(cpo_index,data,mineral_i,grain_i,cosine_new);
}
Assert(sum_volume_fractions != 0, ExcMessage("The sum of all grain volume fractions of a mineral is equal to zero. This should not happen."));
return sum_volume_fractions;
}
template <int dim>
std::pair<std::vector<double>, std::vector<Tensor<2,3>>>
CrystalPreferredOrientation<dim>::compute_derivatives(const unsigned int cpo_index,
const ArrayView<double> &data,
const unsigned int mineral_i,
const SymmetricTensor<2,3> &strain_rate_3d,
const Tensor<2,3> &velocity_gradient_tensor,
const Point<dim> &position,
const typename DoFHandler<dim>::active_cell_iterator &cell,
const double temperature,
const double pressure,
const Tensor<1,dim> &velocity,
const std::vector<double> &compositions,
const SymmetricTensor<2,dim> &strain_rate,
const SymmetricTensor<2,dim> &deviatoric_strain_rate,
const double water_content) const
{
std::pair<std::vector<double>, std::vector<Tensor<2,3>>> derivatives;
switch (cpo_derivative_algorithm)
{
case CPODerivativeAlgorithm::spin_tensor:
{
return compute_derivatives_spin_tensor(velocity_gradient_tensor);
break;
}
case CPODerivativeAlgorithm::drex_2004:
{
const DeformationType deformation_type = determine_deformation_type(deformation_type_selector[mineral_i],
position,
cell,
temperature,
pressure,
velocity,
compositions,
strain_rate,
deviatoric_strain_rate,
water_content);
set_deformation_type(cpo_index,data,mineral_i,deformation_type);
const std::array<double,4> ref_resolved_shear_stress = reference_resolved_shear_stress_from_deformation_type(deformation_type);
return compute_derivatives_drex_2004(cpo_index,
data,
mineral_i,
strain_rate_3d,
velocity_gradient_tensor,
ref_resolved_shear_stress);
break;
}
default:
AssertThrow(false, ExcMessage("Internal error."));
break;
}
AssertThrow(false, ExcMessage("Internal error."));
return derivatives;
}
template <int dim>
std::pair<std::vector<double>, std::vector<Tensor<2,3>>>
CrystalPreferredOrientation<dim>::compute_derivatives_spin_tensor(const Tensor<2,3> &velocity_gradient_tensor) const
{
// dA/dt = W * A, where W is the spin tensor and A is the rotation matrix
// The spin tensor is defined as W = 0.5 * ( L - L^T ), where L is the velocity gradient tensor.
const Tensor<2,3> spin_tensor = -0.5 *(velocity_gradient_tensor - dealii::transpose(velocity_gradient_tensor));
return std::pair<std::vector<double>, std::vector<Tensor<2,3>>>(std::vector<double>(n_grains,0.0), std::vector<Tensor<2,3>>(n_grains, spin_tensor));
}
template <int dim>
std::pair<std::vector<double>, std::vector<Tensor<2,3>>>
CrystalPreferredOrientation<dim>::compute_derivatives_drex_2004(const unsigned int cpo_index,
const ArrayView<double> &data,
const unsigned int mineral_i,
const SymmetricTensor<2,3> &strain_rate_3d,
const Tensor<2,3> &velocity_gradient_tensor,
const std::array<double,4> ref_resolved_shear_stress,
const bool prevent_nondimensionalization) const
{
// This if statement is only there for the unit test. In normal situations it should always be set to false,
// because the nondimensionalization should always be done (in this exact way), unless you really know what
// you are doing.
double nondimensionalization_value = 1.0;
if (!prevent_nondimensionalization)
{
const std::array< double, 3 > eigenvalues = dealii::eigenvalues(strain_rate_3d);
nondimensionalization_value = std::max(std::abs(eigenvalues[0]),std::abs(eigenvalues[2]));
Assert(!std::isnan(nondimensionalization_value), ExcMessage("The second invariant of the strain rate is not a number."));
}
// Make the strain-rate and velocity gradient tensor non-dimensional
// by dividing it through the second invariant
const Tensor<2,3> strain_rate_nondimensional = nondimensionalization_value != 0 ? strain_rate_3d/nondimensionalization_value : strain_rate_3d;
const Tensor<2,3> velocity_gradient_tensor_nondimensional = nondimensionalization_value != 0 ? velocity_gradient_tensor/nondimensionalization_value : velocity_gradient_tensor;
// create output variables
std::vector<double> deriv_volume_fractions(n_grains);
std::vector<Tensor<2,3>> deriv_a_cosine_matrices(n_grains);
// create shortcuts
const std::array<double, 4> &tau = ref_resolved_shear_stress;
std::vector<double> strain_energy(n_grains);
double mean_strain_energy = 0;
for (unsigned int grain_i = 0; grain_i < n_grains; ++grain_i)
{
// Compute the Schmidt tensor for this grain (nu), s is the slip system.
// We first compute beta_s,nu (equation 5, Kaminski & Ribe, 2001)
// Then we use the beta to calculate the Schmidt tensor G_{ij} (Eq. 5, Kaminski & Ribe, 2001)
Tensor<2,3> G;
Tensor<1,3> w;
Tensor<1,4> beta({1.0, 1.0, 1.0, 1.0});
std::array<Tensor<1,3>,4> slip_normal_reference {{Tensor<1,3>({0,1,0}),Tensor<1,3>({0,0,1}),Tensor<1,3>({0,1,0}),Tensor<1,3>({1,0,0})}};
std::array<Tensor<1,3>,4> slip_direction_reference {{Tensor<1,3>({1,0,0}),Tensor<1,3>({1,0,0}),Tensor<1,3>({0,0,1}),Tensor<1,3>({0,0,1})}};
// these are variables we only need for olivine, but we need them for both
// within this if block and the next ones
// Ordered vector where the first entry is the max/weakest and the last entry is the inactive slip system.
std::array<unsigned int,4> indices {};
// compute G and beta
Tensor<1,4> bigI;
const Tensor<2,3> rotation_matrix = get_rotation_matrix_grains(cpo_index,data,mineral_i,grain_i);
const Tensor<2,3> rotation_matrix_transposed = transpose(rotation_matrix);
for (unsigned int slip_system_i = 0; slip_system_i < 4; ++slip_system_i)
{
const Tensor<1,3> slip_normal_global = rotation_matrix_transposed*slip_normal_reference[slip_system_i];
const Tensor<1,3> slip_direction_global = rotation_matrix_transposed*slip_direction_reference[slip_system_i];
const Tensor<2,3> slip_cross_product = outer_product(slip_direction_global,slip_normal_global);
bigI[slip_system_i] = scalar_product(slip_cross_product,strain_rate_nondimensional);
}
if (bigI.norm() < 1e-10)
{
// In this case there is no shear, only (possibly) a rotation. So \gamma_y and/or G should be zero.
// Which is the default value, so do nothing.
}
else
{
// compute the element wise absolute value of the element wise
// division of BigI by tau (tau = ref_resolved_shear_stress).
std::array<double,4> q_abs;
for (unsigned int i = 0; i < 4; ++i)
{
q_abs[i] = std::abs(bigI[i] / tau[i]);
}
// here we find the indices starting at the largest value and ending at the smallest value
// and assign them to special variables. Because all the variables are absolute values,
// we can set them to a negative value to ignore them. This should be faster then deleting
// the element, which would require allocation. (not tested)
for (unsigned int slip_system_i = 0; slip_system_i < 4; ++slip_system_i)
{
indices[slip_system_i] = std::distance(q_abs.begin(),std::max_element(q_abs.begin(), q_abs.end()));
q_abs[indices[slip_system_i]] = -1;
}
// compute the ordered beta vector, which is the relative slip rates of the active slip systems.
// Test whether the max element is not equal to zero.
Assert(bigI[indices[0]] != 0.0, ExcMessage("Internal error: bigI is zero."));
beta[indices[0]] = 1.0; // max q_abs, weak system (most deformation) "s=1"
const double ratio = tau[indices[0]]/bigI[indices[0]];
for (unsigned int slip_system_i = 1; slip_system_i < 4-1; ++slip_system_i)
{
beta[indices[slip_system_i]] = std::pow(std::abs(ratio * (bigI[indices[slip_system_i]]/tau[indices[slip_system_i]])), stress_exponent);
}
beta[indices.back()] = 0.0;
// Now compute the crystal rate of deformation tensor. equation 4 of Kaminski&Ribe 2001
// rotation_matrix_transposed = inverse of rotation matrix
// (see Engler et al., 2024 book: Intro to Texture analysis chp 2.3.2 The Rotation Matrix)
// this transform the crystal reference frame to specimen reference frame
for (unsigned int slip_system_i = 0; slip_system_i < 4; ++slip_system_i)
{
const Tensor<1,3> slip_normal_global = rotation_matrix_transposed*slip_normal_reference[slip_system_i];
const Tensor<1,3> slip_direction_global = rotation_matrix_transposed*slip_direction_reference[slip_system_i];
const Tensor<2,3> slip_cross_product = outer_product(slip_direction_global,slip_normal_global);
G += 2.0 * beta[slip_system_i] * slip_cross_product;
}
}
// Now calculate the analytic solution to the deformation minimization problem
// compute gamma (equation 7, Kaminiski & Ribe, 2001)
// Top is the numerator and bottom is the denominator in equation 7.
double top = 0;
double bottom = 0;
for (unsigned int i = 0; i < 3; ++i)
{
// Following the actual Drex implementation we use i+2, which differs
// from the EPSL paper, which says gamma_nu depends on i+1
const unsigned int i_offset = (i==0) ? (i+2) : (i-1);
top = top - (velocity_gradient_tensor_nondimensional[i][i_offset]-velocity_gradient_tensor_nondimensional[i_offset][i])*(G[i][i_offset]-G[i_offset][i]);
bottom = bottom - (G[i][i_offset]-G[i_offset][i])*(G[i][i_offset]-G[i_offset][i]);
for (unsigned int j = 0; j < 3; ++j)
{
top = top + 2.0 * G[i][j]*velocity_gradient_tensor_nondimensional[i][j];
bottom = bottom + 2.0* G[i][j] * G[i][j];
}
}
// see comment on if all BigI are zero. In that case gamma should be zero.
const double gamma = (bottom != 0.0) ? top/bottom : 0.0;
// compute w (equation 8, Kaminiski & Ribe, 2001)
// w is the Rotation rate vector of the crystallographic axes of grain
w[0] = 0.5*(velocity_gradient_tensor_nondimensional[2][1]-velocity_gradient_tensor_nondimensional[1][2]) - 0.5*(G[2][1]-G[1][2])*gamma;
w[1] = 0.5*(velocity_gradient_tensor_nondimensional[0][2]-velocity_gradient_tensor_nondimensional[2][0]) - 0.5*(G[0][2]-G[2][0])*gamma;
w[2] = 0.5*(velocity_gradient_tensor_nondimensional[1][0]-velocity_gradient_tensor_nondimensional[0][1]) - 0.5*(G[1][0]-G[0][1])*gamma;
// Compute strain energy for this grain (abbreviated Estr)
// For olivine: DREX only sums over 1-3. But Christopher Thissen's matlab
// code (https://github.com/cthissen/Drex-MATLAB) corrected
// this and writes each term using the indices created when calculating bigI.
// Note tau = RRSS = (tau_m^s/tau_o), this why we get tau^(p-n)
for (unsigned int slip_system_i = 0; slip_system_i < 4; ++slip_system_i)
{
const double rhos = std::pow(tau[indices[slip_system_i]],exponent_p-stress_exponent) *
std::pow(std::abs(gamma*beta[indices[slip_system_i]]),exponent_p/stress_exponent);
strain_energy[grain_i] += rhos * std::exp(-nucleation_efficiency * rhos * rhos);
Assert(isfinite(strain_energy[grain_i]), ExcMessage("strain_energy[" + std::to_string(grain_i) + "] is not finite: " + std::to_string(strain_energy[grain_i])
+ ", rhos (" + std::to_string(slip_system_i) + ") = " + std::to_string(rhos)
+ ", nucleation_efficiency = " + std::to_string(nucleation_efficiency) + "."));
}
// compute the derivative of the rotation matrix: \frac{\partial a_{ij}}{\partial t}
// (Eq. 9, Kaminski & Ribe 2001)
deriv_a_cosine_matrices[grain_i] = 0;
const double volume_fraction_grain = get_volume_fractions_grains(cpo_index,data,mineral_i,grain_i);
if (volume_fraction_grain >= threshold_GBS/n_grains)
{
deriv_a_cosine_matrices[grain_i] = Utilities::Tensors::levi_civita<3>() * w * nondimensionalization_value;
// volume averaged strain energy
mean_strain_energy += volume_fraction_grain * strain_energy[grain_i];
Assert(isfinite(mean_strain_energy), ExcMessage("mean_strain_energy when adding grain " + std::to_string(grain_i) + " is not finite: " + std::to_string(mean_strain_energy)
+ ", volume_fraction_grain = " + std::to_string(volume_fraction_grain) + "."));
}
else
{
strain_energy[grain_i] = 0;
}
}
// Change of volume fraction of grains by grain boundary migration
for (unsigned int grain_i = 0; grain_i < n_grains; ++grain_i)
{
// Different than D-Rex. Here we actually only compute the derivative and do not multiply it with the volume_fractions. We do that when we advect.
deriv_volume_fractions[grain_i] = get_volume_fraction_mineral(cpo_index,data,mineral_i) * mobility * (mean_strain_energy - strain_energy[grain_i]) * nondimensionalization_value;
Assert(isfinite(deriv_volume_fractions[grain_i]),
ExcMessage("deriv_volume_fractions[" + std::to_string(grain_i) + "] is not finite: "
+ std::to_string(deriv_volume_fractions[grain_i])));
}
return std::pair<std::vector<double>, std::vector<Tensor<2,3>>>(deriv_volume_fractions, deriv_a_cosine_matrices);
}
template <int dim>
DeformationType
CrystalPreferredOrientation<dim>::determine_deformation_type(const DeformationTypeSelector deformation_type_selector,
const Point<dim> &position,
const typename DoFHandler<dim>::active_cell_iterator &cell,
const double temperature,
const double pressure,
const Tensor<1,dim> &velocity,
const std::vector<double> &compositions,
const SymmetricTensor<2,dim> &strain_rate,
const SymmetricTensor<2,dim> &deviatoric_strain_rate,
const double water_content) const
{
// Now compute what type of deformation takes place.
switch (deformation_type_selector)
{
case DeformationTypeSelector::passive:
return DeformationType::passive;
case DeformationTypeSelector::olivine_a_fabric:
return DeformationType::olivine_a_fabric;
case DeformationTypeSelector::olivine_b_fabric:
return DeformationType::olivine_b_fabric;
case DeformationTypeSelector::olivine_c_fabric:
return DeformationType::olivine_c_fabric;
case DeformationTypeSelector::olivine_d_fabric:
return DeformationType::olivine_d_fabric;
case DeformationTypeSelector::olivine_e_fabric:
return DeformationType::olivine_e_fabric;
case DeformationTypeSelector::enstatite:
return DeformationType::enstatite;
case DeformationTypeSelector::olivine_karato_2008:
// construct the material model inputs and outputs
// Since this function is only evaluating one particle,
// we use 1 for the amount of quadrature points.
MaterialModel::MaterialModelInputs<dim> material_model_inputs(1,this->n_compositional_fields());
material_model_inputs.position[0] = position;
material_model_inputs.temperature[0] = temperature;
material_model_inputs.pressure[0] = pressure;
material_model_inputs.velocity[0] = velocity;
material_model_inputs.composition[0] = compositions;
material_model_inputs.strain_rate[0] = strain_rate;
material_model_inputs.current_cell = cell;
MaterialModel::MaterialModelOutputs<dim> material_model_outputs(1,this->n_compositional_fields());
this->get_material_model().evaluate(material_model_inputs, material_model_outputs);
double eta = material_model_outputs.viscosities[0];
const SymmetricTensor<2,dim> stress = 2*eta*deviatoric_strain_rate +
pressure * unit_symmetric_tensor<dim>();
const std::array< double, dim > eigenvalues = dealii::eigenvalues(stress);
double differential_stress = eigenvalues[0]-eigenvalues[dim-1];
return determine_deformation_type_karato_2008(differential_stress, water_content);
}
AssertThrow(false, ExcMessage("Internal error. Deformation type not implemented."));
return DeformationType::passive;
}
template <int dim>
DeformationType
CrystalPreferredOrientation<dim>::determine_deformation_type_karato_2008(const double stress, const double water_content) const
{
constexpr double MPa = 1e6;
constexpr double ec_line_slope = -500./1050.;
if (stress > (380. - 0.05 * water_content)*MPa)
{
if (stress > (625. - 2.5 * water_content)*MPa)
{
return DeformationType::olivine_b_fabric;
}
else
{
return DeformationType::olivine_d_fabric;
}
}
else
{
if (stress < (625.0 -2.5 * water_content)*MPa)
{
return DeformationType::olivine_a_fabric;
}
else
{
if (stress < (500.0 + ec_line_slope*-100. + ec_line_slope * water_content)*MPa)
{
return DeformationType::olivine_e_fabric;
}
else
{
return DeformationType::olivine_c_fabric;
}
}
}
}
template <int dim>
std::array<double,4>
CrystalPreferredOrientation<dim>::reference_resolved_shear_stress_from_deformation_type(DeformationType deformation_type,
double max_value) const
{
std::array<double,4> ref_resolved_shear_stress;
switch (deformation_type)
{
// from Kaminski and Ribe, GJI 2004 and
// Becker et al., 2007 (http://www-udc.ig.utexas.edu/external/becker/preprints/bke07.pdf)
case DeformationType::olivine_a_fabric :
ref_resolved_shear_stress[0] = 1;
ref_resolved_shear_stress[1] = 2;
ref_resolved_shear_stress[2] = 3;
ref_resolved_shear_stress[3] = max_value;
break;
// from Kaminski and Ribe, GJI 2004 and
// Becker et al., 2007 (http://www-udc.ig.utexas.edu/external/becker/preprints/bke07.pdf)
case DeformationType::olivine_b_fabric :
ref_resolved_shear_stress[0] = 3;
ref_resolved_shear_stress[1] = 2;
ref_resolved_shear_stress[2] = 1;
ref_resolved_shear_stress[3] = max_value;
break;
// from Kaminski and Ribe, GJI 2004 and
// Becker et al., 2007 (http://www-udc.ig.utexas.edu/external/becker/preprints/bke07.pdf)
case DeformationType::olivine_c_fabric :
ref_resolved_shear_stress[0] = 3;
ref_resolved_shear_stress[1] = max_value;
ref_resolved_shear_stress[2] = 2;
ref_resolved_shear_stress[3] = 1;
break;
// from Kaminski and Ribe, GRL 2002 and
// Becker et al., 2007 (http://www-udc.ig.utexas.edu/external/becker/preprints/bke07.pdf)
case DeformationType::olivine_d_fabric :
ref_resolved_shear_stress[0] = 1;
ref_resolved_shear_stress[1] = 1;
ref_resolved_shear_stress[2] = 3;
ref_resolved_shear_stress[3] = max_value;
break;
// Kaminski, Ribe and Browaeys, GJI, 2004 (same as in the matlab code) and
// Becker et al., 2007 (http://www-udc.ig.utexas.edu/external/becker/preprints/bke07.pdf)
case DeformationType::olivine_e_fabric :
ref_resolved_shear_stress[0] = 2;
ref_resolved_shear_stress[1] = 1;
ref_resolved_shear_stress[2] = max_value;
ref_resolved_shear_stress[3] = 3;
break;
// from Kaminski and Ribe, GJI 2004.
// Todo: this one is not used in practice, since there is an optimization in
// the code. So maybe remove it in the future.
case DeformationType::enstatite :
ref_resolved_shear_stress[0] = max_value;
ref_resolved_shear_stress[1] = max_value;
ref_resolved_shear_stress[2] = max_value;
ref_resolved_shear_stress[3] = 1;
break;
default: