OGS
PhaseFieldProcess.cpp
Go to the documentation of this file.
1
11#include "PhaseFieldProcess.h"
12
13#include <cassert>
14
15#include "BaseLib/MPI.h"
18#include "PhaseFieldFEM.h"
19#include "ProcessLib/Process.h"
22
23namespace ProcessLib
24{
25namespace PhaseField
26{
27template <int DisplacementDim>
29 std::string name,
30 MeshLib::Mesh& mesh,
31 std::unique_ptr<ProcessLib::AbstractJacobianAssembler>&& jacobian_assembler,
32 std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
33 unsigned const integration_order,
34 std::vector<std::vector<std::reference_wrapper<ProcessVariable>>>&&
35 process_variables,
37 SecondaryVariableCollection&& secondary_variables,
38 bool const use_monolithic_scheme)
39 : Process(std::move(name), mesh, std::move(jacobian_assembler), parameters,
40 integration_order, std::move(process_variables),
41 std::move(secondary_variables), use_monolithic_scheme),
42 _process_data(std::move(process_data))
43{
44 if (use_monolithic_scheme)
45 {
47 "Monolithic scheme is not implemented for the PhaseField process.");
48 }
49
51 mesh, "NodalForces", MeshLib::MeshItemType::Node, DisplacementDim);
52
53 _integration_point_writer.emplace_back(
54 std::make_unique<MeshLib::IntegrationPointWriter>(
55 "sigma_ip",
56 static_cast<int>(mesh.getDimension() == 2 ? 4 : 6) /*n components*/,
57 integration_order, _local_assemblers,
59}
60
61template <int DisplacementDim>
63{
64 return false;
65}
66
67template <int DisplacementDim>
70 const int process_id) const
71{
72 // For the M process (deformation) in the staggered scheme.
73 if (process_id == 0)
74 {
75 auto const& l = *_local_to_global_index_map;
76 return {l.dofSizeWithoutGhosts(), l.dofSizeWithoutGhosts(),
77 &l.getGhostIndices(), &this->_sparsity_pattern};
78 }
79
80 // For staggered scheme and phase field process.
81 auto const& l = *_local_to_global_index_map_single_component;
82 return {l.dofSizeWithoutGhosts(), l.dofSizeWithoutGhosts(),
83 &l.getGhostIndices(), &_sparsity_pattern_with_single_component};
84}
85
86template <int DisplacementDim>
89{
90 // For the M process (deformation) in the staggered scheme.
91 if (process_id == 0)
92 {
93 return *_local_to_global_index_map;
94 }
95
96 // For the equation of phasefield
97 return *_local_to_global_index_map_single_component;
98}
99
100template <int DisplacementDim>
102{
103 // For displacement equation.
104 const int mechanics_process_id = 0;
105 constructDofTableOfSpecifiedProcessStaggeredScheme(mechanics_process_id);
106
107 // TODO move the two data members somewhere else.
108 // for extrapolation of secondary variables of stress or strain
109 std::vector<MeshLib::MeshSubset> all_mesh_subsets_single_component{
110 *_mesh_subset_all_nodes};
111 _local_to_global_index_map_single_component =
112 std::make_unique<NumLib::LocalToGlobalIndexMap>(
113 std::move(all_mesh_subsets_single_component),
114 // by location order is needed for output
116
117 assert(_local_to_global_index_map_single_component);
118
119 // For phase field equation.
120 _sparsity_pattern_with_single_component = NumLib::computeSparsityPattern(
121 *_local_to_global_index_map_single_component, _mesh);
122}
123
124template <int DisplacementDim>
126 NumLib::LocalToGlobalIndexMap const& dof_table,
127 MeshLib::Mesh const& mesh,
128 unsigned const integration_order)
129{
131 DisplacementDim, PhaseFieldLocalAssembler>(
132 mesh.getElements(), dof_table, _local_assemblers,
133 NumLib::IntegrationOrder{integration_order}, mesh.isAxiallySymmetric(),
134 _process_data);
135
136 _secondary_variables.addSecondaryVariable(
137 "sigma",
139 DisplacementDim>::RowsAtCompileTime,
140 getExtrapolator(), _local_assemblers,
141 &LocalAssemblerInterface::getIntPtSigma));
142
143 _secondary_variables.addSecondaryVariable(
144 "epsilon",
146 DisplacementDim>::RowsAtCompileTime,
147 getExtrapolator(), _local_assemblers,
148 &LocalAssemblerInterface::getIntPtEpsilon));
149
150 _secondary_variables.addSecondaryVariable(
151 "sigma_tensile",
153 DisplacementDim>::RowsAtCompileTime,
154 getExtrapolator(), _local_assemblers,
155 &LocalAssemblerInterface::getIntPtSigmaTensile));
156
157 _secondary_variables.addSecondaryVariable(
158 "sigma_compressive",
160 DisplacementDim>::RowsAtCompileTime,
161 getExtrapolator(), _local_assemblers,
162 &LocalAssemblerInterface::getIntPtSigmaCompressive));
163
164 _secondary_variables.addSecondaryVariable(
165 "eps_tensile",
167 DisplacementDim>::RowsAtCompileTime,
168 getExtrapolator(), _local_assemblers,
169 &LocalAssemblerInterface::getIntPtEpsilonTensile));
170
171 setIPDataInitialConditions(_integration_point_writer, mesh.getProperties(),
172 _local_assemblers);
173
174 // Initialize local assemblers after all variables have been set.
176 &LocalAssemblerInterface::initialize, _local_assemblers,
177 *_local_to_global_index_map);
178}
179
180template <int DisplacementDim>
182 std::map<int, std::shared_ptr<MaterialPropertyLib::Medium>> const& media)
183{
184 // Staggered scheme:
185 // for the equations of deformation.
186 const int mechanical_process_id = 0;
187 initializeProcessBoundaryConditionsAndSourceTerms(
188 *_local_to_global_index_map, mechanical_process_id, media);
189 // for the phase field
190 const int phasefield_process_id = 1;
191 initializeProcessBoundaryConditionsAndSourceTerms(
192 *_local_to_global_index_map_single_component, phasefield_process_id,
193 media);
194}
195
196template <int DisplacementDim>
198 const double t, double const dt, std::vector<GlobalVector*> const& x,
199 std::vector<GlobalVector*> const& x_prev, int const process_id,
201{
202 DBUG("Assemble PhaseFieldProcess.");
203
204 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
205
206 // For the staggered scheme
207 if (process_id == 1)
208 {
209 DBUG(
210 "Assemble the equations of phase field in "
211 "PhaseFieldProcess for the staggered scheme.");
212 }
213 else
214 {
215 DBUG(
216 "Assemble the equations of deformation in "
217 "PhaseFieldProcess for the staggered scheme.");
218 }
219 dof_tables.emplace_back(_local_to_global_index_map_single_component.get());
220 dof_tables.emplace_back(_local_to_global_index_map.get());
221
222 // Call global assembler for each local assembly item.
224 _global_assembler, &VectorMatrixAssembler::assemble, _local_assemblers,
225 getActiveElementIDs(), dof_tables, t, dt, x, x_prev, process_id, &M, &K,
226 &b);
227}
228
229template <int DisplacementDim>
231 const double t, double const dt, std::vector<GlobalVector*> const& x,
232 std::vector<GlobalVector*> const& x_prev, int const process_id,
233 GlobalVector& b, GlobalMatrix& Jac)
234{
235 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
236
237 // For the staggered scheme
238 if (process_id == 1)
239 {
240 DBUG(
241 "Assemble the Jacobian equations of phase field in "
242 "PhaseFieldProcess for the staggered scheme.");
243 }
244 else
245 {
246 DBUG(
247 "Assemble the Jacobian equations of deformation in "
248 "PhaseFieldProcess for the staggered scheme.");
249 }
250 dof_tables.emplace_back(_local_to_global_index_map.get());
251 dof_tables.emplace_back(_local_to_global_index_map_single_component.get());
252
253 // Call global assembler for each local assembly item.
256 _local_assemblers, getActiveElementIDs(), dof_tables, t, dt, x, x_prev,
257 process_id, &b, &Jac);
258
259 if (process_id == 0)
260 {
261 b.copyValues(*_nodal_forces);
262 std::transform(_nodal_forces->begin(), _nodal_forces->end(),
263 _nodal_forces->begin(), [](double val) { return -val; });
264 }
265}
266
267template <int DisplacementDim>
269 std::vector<GlobalVector*> const& x, double const t, double const dt,
270 const int process_id)
271{
272 DBUG("PreTimestep PhaseFieldProcess {:d}.", process_id);
273
274 _process_data.injected_volume = t;
275
276 _x_previous_timestep =
278
280 &LocalAssemblerInterface::preTimestep, _local_assemblers,
281 getActiveElementIDs(), getDOFTable(process_id), *x[process_id], t, dt);
282}
283
284template <int DisplacementDim>
286 std::vector<GlobalVector*> const& x,
287 std::vector<GlobalVector*> const& /*x_prev*/, const double t,
288 const double /*delta_t*/, int const process_id)
289{
290 if (isPhaseFieldProcess(process_id))
291 {
292 DBUG("PostTimestep PhaseFieldProcess.");
293
294 _process_data.elastic_energy = 0.0;
295 _process_data.surface_energy = 0.0;
296 _process_data.pressure_work = 0.0;
297
298 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
299
300 dof_tables.emplace_back(_local_to_global_index_map.get());
301 dof_tables.emplace_back(
302 _local_to_global_index_map_single_component.get());
303
305 &LocalAssemblerInterface::computeEnergy, _local_assemblers,
306 getActiveElementIDs(), dof_tables, x, t,
307 _process_data.elastic_energy, _process_data.surface_energy,
308 _process_data.pressure_work);
309
310#ifdef USE_PETSC
311 BaseLib::MPI::Mpi mpi{};
312 _process_data.elastic_energy =
313 BaseLib::MPI::allreduce(_process_data.elastic_energy, MPI_SUM, mpi);
314 _process_data.surface_energy =
315 BaseLib::MPI::allreduce(_process_data.surface_energy, MPI_SUM, mpi);
316 _process_data.pressure_work =
317 BaseLib::MPI::allreduce(_process_data.pressure_work, MPI_SUM, mpi);
318#endif
319
320 INFO(
321 "Elastic energy: {:g} Surface energy: {:g} Pressure work: {:g} at "
322 "time: {:g} ",
323 _process_data.elastic_energy, _process_data.surface_energy,
324 _process_data.pressure_work, t);
325 if (_process_data.propagating_pressurized_crack)
326 {
327 INFO("Pressure: {:g} at time: {:g} ", _process_data.pressure, t);
328 }
329 }
330}
331
332template <int DisplacementDim>
334 std::vector<GlobalVector*> const& x,
335 std::vector<GlobalVector*> const& /*x_prev*/, const double t,
336 double const /*dt*/, const int process_id)
337{
338 _process_data.crack_volume = 0.0;
339
340 if (isPhaseFieldProcess(process_id))
341 {
342 if (_process_data.propagating_pressurized_crack)
343 {
344 auto& u = *x[0];
345 MathLib::LinAlg::scale(const_cast<GlobalVector&>(u),
346 1 / _process_data.pressure);
347 }
348 return;
349 }
350
351 std::vector<NumLib::LocalToGlobalIndexMap const*> dof_tables;
352
353 dof_tables.emplace_back(_local_to_global_index_map.get());
354 dof_tables.emplace_back(_local_to_global_index_map_single_component.get());
355
356 DBUG("PostNonLinearSolver crack volume computation.");
357
359 &LocalAssemblerInterface::computeCrackIntegral, _local_assemblers,
360 getActiveElementIDs(), dof_tables, x, t, _process_data.crack_volume);
361
362#ifdef USE_PETSC
363 _process_data.crack_volume = BaseLib::MPI::allreduce(
364 _process_data.crack_volume, MPI_SUM, BaseLib::MPI::Mpi{});
365#endif
366
367 INFO("Integral of crack: {:g}", _process_data.crack_volume);
368
369 if (_process_data.propagating_pressurized_crack)
370 {
371 _process_data.pressure_old = _process_data.pressure;
372 _process_data.pressure =
373 _process_data.injected_volume / _process_data.crack_volume;
374 _process_data.pressure_error =
375 std::abs(_process_data.pressure_old - _process_data.pressure) /
376 _process_data.pressure;
377 INFO("Internal pressure: {:g} and Pressure error: {:.4e}",
378 _process_data.pressure, _process_data.pressure_error);
379
380 auto& u = *x[0];
381 MathLib::LinAlg::scale(const_cast<GlobalVector&>(u),
382 _process_data.pressure);
383 }
384}
385
386template <int DisplacementDim>
388 GlobalVector& lower, GlobalVector& upper, int const /*process_id*/)
389{
390 lower.setZero();
391 MathLib::LinAlg::setLocalAccessibleVector(*_x_previous_timestep);
392 MathLib::LinAlg::copy(*_x_previous_timestep, upper);
393
394 GlobalIndexType const x_begin = _x_previous_timestep->getRangeBegin();
395 GlobalIndexType const x_end = _x_previous_timestep->getRangeEnd();
396
397 for (GlobalIndexType i = x_begin; i < x_end; i++)
398 {
399 if ((*_x_previous_timestep)[i] > _process_data.irreversible_threshold)
400 {
401 upper.set(i, 1.0);
402 }
403 }
404}
405
406template <int DisplacementDim>
408 int const process_id) const
409{
410 return process_id == 1;
411}
412
413template class PhaseFieldProcess<2>;
414template class PhaseFieldProcess<3>;
415
416} // namespace PhaseField
417} // 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)
static T allreduce(T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
Definition MPI.h:128
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