OGS
PhaseFieldProcess.cpp
Go to the documentation of this file.
1
11#include "PhaseFieldProcess.h"
12
13#include <cassert>
14
17#include "PhaseFieldFEM.h"
18#include "ProcessLib/Process.h"
21
22namespace ProcessLib
23{
24namespace PhaseField
25{
26template <int DisplacementDim>
28 std::string name,
29 MeshLib::Mesh& mesh,
30 std::unique_ptr<ProcessLib::AbstractJacobianAssembler>&& jacobian_assembler,
31 std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
32 unsigned const integration_order,
33 std::vector<std::vector<std::reference_wrapper<ProcessVariable>>>&&
34 process_variables,
36 SecondaryVariableCollection&& secondary_variables,
37 bool const use_monolithic_scheme)
38 : Process(std::move(name), mesh, std::move(jacobian_assembler), parameters,
39 integration_order, std::move(process_variables),
40 std::move(secondary_variables), use_monolithic_scheme),
41 _process_data(std::move(process_data))
42{
43 if (use_monolithic_scheme)
44 {
46 "Monolithic scheme is not implemented for the PhaseField process.");
47 }
48
50 mesh, "NodalForces", MeshLib::MeshItemType::Node, DisplacementDim);
51
52 _integration_point_writer.emplace_back(
53 std::make_unique<MeshLib::IntegrationPointWriter>(
54 "sigma_ip",
55 static_cast<int>(mesh.getDimension() == 2 ? 4 : 6) /*n components*/,
56 integration_order, _local_assemblers,
58}
59
60template <int DisplacementDim>
62{
63 return false;
64}
65
66template <int DisplacementDim>
69 const int process_id) const
70{
71 // For the M process (deformation) in the staggered scheme.
72 if (process_id == 0)
73 {
74 auto const& l = *_local_to_global_index_map;
75 return {l.dofSizeWithoutGhosts(), l.dofSizeWithoutGhosts(),
76 &l.getGhostIndices(), &this->_sparsity_pattern};
77 }
78
79 // For staggered scheme and phase field process.
80 auto const& l = *_local_to_global_index_map_single_component;
81 return {l.dofSizeWithoutGhosts(), l.dofSizeWithoutGhosts(),
82 &l.getGhostIndices(), &_sparsity_pattern_with_single_component};
83}
84
85template <int DisplacementDim>
88{
89 // For the M process (deformation) in the staggered scheme.
90 if (process_id == 0)
91 {
92 return *_local_to_global_index_map;
93 }
94
95 // For the equation of phasefield
96 return *_local_to_global_index_map_single_component;
97}
98
99template <int DisplacementDim>
101{
102 // For displacement equation.
103 const int mechanics_process_id = 0;
104 constructDofTableOfSpecifiedProcessStaggeredScheme(mechanics_process_id);
105
106 // TODO move the two data members somewhere else.
107 // for extrapolation of secondary variables of stress or strain
108 std::vector<MeshLib::MeshSubset> all_mesh_subsets_single_component{
109 *_mesh_subset_all_nodes};
110 _local_to_global_index_map_single_component =
111 std::make_unique<NumLib::LocalToGlobalIndexMap>(
112 std::move(all_mesh_subsets_single_component),
113 // by location order is needed for output
115
116 assert(_local_to_global_index_map_single_component);
117
118 // For phase field equation.
119 _sparsity_pattern_with_single_component = NumLib::computeSparsityPattern(
120 *_local_to_global_index_map_single_component, _mesh);
121}
122
123template <int DisplacementDim>
125 NumLib::LocalToGlobalIndexMap const& dof_table,
126 MeshLib::Mesh const& mesh,
127 unsigned const integration_order)
128{
130 DisplacementDim, PhaseFieldLocalAssembler>(
131 mesh.getElements(), dof_table, _local_assemblers,
132 NumLib::IntegrationOrder{integration_order}, mesh.isAxiallySymmetric(),
133 _process_data);
134
135 _secondary_variables.addSecondaryVariable(
136 "sigma",
138 DisplacementDim>::RowsAtCompileTime,
139 getExtrapolator(), _local_assemblers,
140 &LocalAssemblerInterface::getIntPtSigma));
141
142 _secondary_variables.addSecondaryVariable(
143 "epsilon",
145 DisplacementDim>::RowsAtCompileTime,
146 getExtrapolator(), _local_assemblers,
147 &LocalAssemblerInterface::getIntPtEpsilon));
148
149 _secondary_variables.addSecondaryVariable(
150 "sigma_tensile",
152 DisplacementDim>::RowsAtCompileTime,
153 getExtrapolator(), _local_assemblers,
154 &LocalAssemblerInterface::getIntPtSigmaTensile));
155
156 _secondary_variables.addSecondaryVariable(
157 "sigma_compressive",
159 DisplacementDim>::RowsAtCompileTime,
160 getExtrapolator(), _local_assemblers,
161 &LocalAssemblerInterface::getIntPtSigmaCompressive));
162
163 _secondary_variables.addSecondaryVariable(
164 "eps_tensile",
166 DisplacementDim>::RowsAtCompileTime,
167 getExtrapolator(), _local_assemblers,
168 &LocalAssemblerInterface::getIntPtEpsilonTensile));
169
170 setIPDataInitialConditions(_integration_point_writer, mesh.getProperties(),
171 _local_assemblers);
172
173 // Initialize local assemblers after all variables have been set.
175 &LocalAssemblerInterface::initialize, _local_assemblers,
176 *_local_to_global_index_map);
177}
178
179template <int DisplacementDim>
181 std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media)
182{
183 // Staggered scheme:
184 // for the equations of deformation.
185 const int mechanical_process_id = 0;
186 initializeProcessBoundaryConditionsAndSourceTerms(
187 *_local_to_global_index_map, mechanical_process_id, media);
188 // for the phase field
189 const int phasefield_process_id = 1;
190 initializeProcessBoundaryConditionsAndSourceTerms(
191 *_local_to_global_index_map_single_component, phasefield_process_id,
192 media);
193}
194
195template <int DisplacementDim>
197 const double t, double const dt, std::vector<GlobalVector*> const& x,
198 std::vector<GlobalVector*> const& x_prev, int const process_id,
200{
201 DBUG("Assemble PhaseFieldProcess.");
202
203 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
204
205 // For the staggered scheme
206 if (process_id == 1)
207 {
208 DBUG(
209 "Assemble the equations of phase field in "
210 "PhaseFieldProcess for the staggered scheme.");
211 }
212 else
213 {
214 DBUG(
215 "Assemble the equations of deformation in "
216 "PhaseFieldProcess for the staggered scheme.");
217 }
218 dof_tables.emplace_back(_local_to_global_index_map_single_component.get());
219 dof_tables.emplace_back(_local_to_global_index_map.get());
220
221 // Call global assembler for each local assembly item.
223 _global_assembler, &VectorMatrixAssembler::assemble, _local_assemblers,
224 getActiveElementIDs(), dof_tables, t, dt, x, x_prev, process_id, &M, &K,
225 &b);
226}
227
228template <int DisplacementDim>
230 const double t, double const dt, std::vector<GlobalVector*> const& x,
231 std::vector<GlobalVector*> const& x_prev, int const process_id,
232 GlobalVector& b, GlobalMatrix& Jac)
233{
234 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
235
236 // For the staggered scheme
237 if (process_id == 1)
238 {
239 DBUG(
240 "Assemble the Jacobian equations of phase field in "
241 "PhaseFieldProcess for the staggered scheme.");
242 }
243 else
244 {
245 DBUG(
246 "Assemble the Jacobian equations of deformation in "
247 "PhaseFieldProcess for the staggered scheme.");
248 }
249 dof_tables.emplace_back(_local_to_global_index_map.get());
250 dof_tables.emplace_back(_local_to_global_index_map_single_component.get());
251
252 // Call global assembler for each local assembly item.
255 _local_assemblers, getActiveElementIDs(), dof_tables, t, dt, x, x_prev,
256 process_id, &b, &Jac);
257
258 if (process_id == 0)
259 {
260 b.copyValues(*_nodal_forces);
261 std::transform(_nodal_forces->begin(), _nodal_forces->end(),
262 _nodal_forces->begin(), [](double val) { return -val; });
263 }
264}
265
266template <int DisplacementDim>
268 std::vector<GlobalVector*> const& x, double const t, double const dt,
269 const int process_id)
270{
271 DBUG("PreTimestep PhaseFieldProcess {:d}.", process_id);
272
273 _process_data.injected_volume = t;
274
275 _x_previous_timestep =
277
279 &LocalAssemblerInterface::preTimestep, _local_assemblers,
280 getActiveElementIDs(), getDOFTable(process_id), *x[process_id], t, dt);
281}
282
283template <int DisplacementDim>
285 std::vector<GlobalVector*> const& x,
286 std::vector<GlobalVector*> const& /*x_prev*/, const double t,
287 const double /*delta_t*/, int const process_id)
288{
289 if (isPhaseFieldProcess(process_id))
290 {
291 DBUG("PostTimestep PhaseFieldProcess.");
292
293 _process_data.elastic_energy = 0.0;
294 _process_data.surface_energy = 0.0;
295 _process_data.pressure_work = 0.0;
296
297 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
298
299 dof_tables.emplace_back(_local_to_global_index_map.get());
300 dof_tables.emplace_back(
301 _local_to_global_index_map_single_component.get());
302
304 &LocalAssemblerInterface::computeEnergy, _local_assemblers,
305 getActiveElementIDs(), dof_tables, x, t,
306 _process_data.elastic_energy, _process_data.surface_energy,
307 _process_data.pressure_work);
308
309#ifdef USE_PETSC
310 double const elastic_energy = _process_data.elastic_energy;
311 MPI_Allreduce(&elastic_energy, &_process_data.elastic_energy, 1,
312 MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD);
313 double const surface_energy = _process_data.surface_energy;
314 MPI_Allreduce(&surface_energy, &_process_data.surface_energy, 1,
315 MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD);
316 double const pressure_work = _process_data.pressure_work;
317 MPI_Allreduce(&pressure_work, &_process_data.pressure_work, 1,
318 MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD);
319#endif
320
321 INFO(
322 "Elastic energy: {:g} Surface energy: {:g} Pressure work: {:g} at "
323 "time: {:g} ",
324 _process_data.elastic_energy, _process_data.surface_energy,
325 _process_data.pressure_work, t);
326 if (_process_data.propagating_pressurized_crack)
327 {
328 INFO("Pressure: {:g} at time: {:g} ", _process_data.pressure, t);
329 }
330 }
331}
332
333template <int DisplacementDim>
335 std::vector<GlobalVector*> const& x,
336 std::vector<GlobalVector*> const& /*x_prev*/, const double t,
337 double const /*dt*/, const int process_id)
338{
339 _process_data.crack_volume = 0.0;
340
341 if (isPhaseFieldProcess(process_id))
342 {
343 if (_process_data.propagating_pressurized_crack)
344 {
345 auto& u = *x[0];
346 MathLib::LinAlg::scale(const_cast<GlobalVector&>(u),
347 1 / _process_data.pressure);
348 }
349 return;
350 }
351
352 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
353
354 dof_tables.emplace_back(_local_to_global_index_map.get());
355 dof_tables.emplace_back(_local_to_global_index_map_single_component.get());
356
357 DBUG("PostNonLinearSolver crack volume computation.");
358
360 &LocalAssemblerInterface::computeCrackIntegral, _local_assemblers,
361 getActiveElementIDs(), dof_tables, x, t, _process_data.crack_volume);
362
363#ifdef USE_PETSC
364 double const crack_volume = _process_data.crack_volume;
365 MPI_Allreduce(&crack_volume, &_process_data.crack_volume, 1, MPI_DOUBLE,
366 MPI_SUM, PETSC_COMM_WORLD);
367#endif
368
369 INFO("Integral of crack: {:g}", _process_data.crack_volume);
370
371 if (_process_data.propagating_pressurized_crack)
372 {
373 _process_data.pressure_old = _process_data.pressure;
374 _process_data.pressure =
375 _process_data.injected_volume / _process_data.crack_volume;
376 _process_data.pressure_error =
377 std::abs(_process_data.pressure_old - _process_data.pressure) /
378 _process_data.pressure;
379 INFO("Internal pressure: {:g} and Pressure error: {:.4e}",
380 _process_data.pressure, _process_data.pressure_error);
381
382 auto& u = *x[0];
383 MathLib::LinAlg::scale(const_cast<GlobalVector&>(u),
384 _process_data.pressure);
385 }
386}
387
388template <int DisplacementDim>
390 GlobalVector& lower, GlobalVector& upper, int const /*process_id*/)
391{
392 lower.setZero();
393 MathLib::LinAlg::setLocalAccessibleVector(*_x_previous_timestep);
394 MathLib::LinAlg::copy(*_x_previous_timestep, upper);
395
396 GlobalIndexType const x_begin = _x_previous_timestep->getRangeBegin();
397 GlobalIndexType const x_end = _x_previous_timestep->getRangeEnd();
398
399 for (GlobalIndexType i = x_begin; i < x_end; i++)
400 {
401 if ((*_x_previous_timestep)[i] > _process_data.irreversible_threshold)
402 {
403 upper.set(i, 1.0);
404 }
405 }
406}
407
408template <int DisplacementDim>
410 int const process_id) const
411{
412 return process_id == 1;
413}
414
415template class PhaseFieldProcess<2>;
416template class PhaseFieldProcess<3>;
417
418} // namespace PhaseField
419} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:26
GlobalMatrix::IndexType GlobalIndexType
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Global vector based on Eigen vector.
Definition EigenVector.h:25
void copyValues(std::vector< double > &u) const
void set(IndexType rowId, double v)
set entry
Definition EigenVector.h:73
bool isAxiallySymmetric() const
Definition Mesh.h:137
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:88
Properties & getProperties()
Definition Mesh.h:134
virtual void preTimestep(std::size_t const mesh_item_id, NumLib::LocalToGlobalIndexMap const &dof_table, GlobalVector const &x, double const t, double const delta_t)
virtual void initialize(std::size_t const mesh_item_id, NumLib::LocalToGlobalIndexMap const &dof_table)
bool isPhaseFieldProcess(int const process_id) const
NumLib::LocalToGlobalIndexMap const & getDOFTable(const int process_id) const override
void updateConstraints(GlobalVector &lower, GlobalVector &upper, int const process_id) override
MathLib::MatrixSpecifications getMatrixSpecifications(const int process_id) const override
MeshLib::PropertyVector< double > * _nodal_forces
void postNonLinearSolverConcreteProcess(std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, const double t, double const dt, int const process_id) override
void postTimestepConcreteProcess(std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, const double t, const double delta_t, int const process_id) override
void assembleConcreteProcess(const double t, double const dt, std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id, GlobalMatrix &M, GlobalMatrix &K, GlobalVector &b) override
void preTimestepConcreteProcess(std::vector< GlobalVector * > const &x, double const t, double const dt, const int process_id) override
std::vector< std::unique_ptr< LocalAssemblerInterface > > _local_assemblers
void initializeBoundaryConditions(std::map< int, std::shared_ptr< MaterialPropertyLib::Medium > > const &media) override
void initializeConcreteProcess(NumLib::LocalToGlobalIndexMap const &dof_table, MeshLib::Mesh const &mesh, unsigned const integration_order) override
Process specific initialization called by initialize().
PhaseFieldProcess(std::string name, MeshLib::Mesh &mesh, std::unique_ptr< ProcessLib::AbstractJacobianAssembler > &&jacobian_assembler, std::vector< std::unique_ptr< ParameterLib::ParameterBase > > const &parameters, unsigned const integration_order, std::vector< std::vector< std::reference_wrapper< ProcessVariable > > > &&process_variables, PhaseFieldProcessData< DisplacementDim > &&process_data, SecondaryVariableCollection &&secondary_variables, bool const use_monolithic_scheme)
void assembleWithJacobianConcreteProcess(const double t, double const dt, std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id, GlobalVector &b, GlobalMatrix &Jac) override
std::vector< std::unique_ptr< MeshLib::IntegrationPointWriter > > _integration_point_writer
Definition Process.h:390
Handles configuration of several secondary variables from the project file.
void assemble(std::size_t const mesh_item_id, LocalAssemblerInterface &local_assembler, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_tables, double const t, double const dt, std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id, GlobalMatrix *M, GlobalMatrix *K, GlobalVector *b)
void assembleWithJacobian(std::size_t const mesh_item_id, LocalAssemblerInterface &local_assembler, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_tables, const double t, double const dt, std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id, GlobalVector *b, GlobalMatrix *Jac)
Eigen::Matrix< double, kelvin_vector_dimensions(DisplacementDim), 1, Eigen::ColMajor > KelvinVectorType
void copy(PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:37
void setLocalAccessibleVector(PETScVector const &x)
Definition LinAlg.cpp:27
void scale(PETScVector &x, PetscScalar const a)
Definition LinAlg.cpp:44
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
@ BY_LOCATION
Ordering data by spatial location.
GlobalSparsityPattern computeSparsityPattern(LocalToGlobalIndexMap const &dof_table, MeshLib::Mesh const &mesh)
Computes a sparsity pattern for the given inputs.
void createLocalAssemblers(std::vector< MeshLib::Element * > const &mesh_elements, NumLib::LocalToGlobalIndexMap const &dof_table, std::vector< std::unique_ptr< LocalAssemblerInterface > > &local_assemblers, ProviderOrOrder const &provider_or_order, ExtraCtorArgs &&... extra_ctor_args)
SecondaryVariableFunctions makeExtrapolator(const unsigned num_components, NumLib::Extrapolator &extrapolator, LocalAssemblerCollection const &local_assemblers, typename NumLib::ExtrapolatableLocalAssemblerCollection< LocalAssemblerCollection >::IntegrationPointValuesMethod integration_point_values_method)
void setIPDataInitialConditions(std::vector< std::unique_ptr< MeshLib::IntegrationPointWriter > > const &_integration_point_writer, MeshLib::Properties const &mesh_properties, LocalAssemblersVector &local_assemblers)
static void executeSelectedMemberOnDereferenced(Method method, Container const &container, std::vector< std::size_t > const &active_container_ids, Args &&... args)
static void executeSelectedMemberDereferenced(Object &object, Method method, Container const &container, std::vector< std::size_t > const &active_container_ids, Args &&... args)
static void executeMemberOnDereferenced(Method method, Container const &container, Args &&... args)
virtual std::vector< double > getSigma() const =0