tpsamodel.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 Copyright 2025 NORCE AS
5
6 This file is part of the Open Porous Media project (OPM).
7
8 OPM is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 OPM is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with OPM. If not, see <http://www.gnu.org/licenses/>.
20
21 Consult the COPYING file in the top-level source directory of this
22 module for the precise wording of the license and the list of
23 copyright holders.
24*/
25#ifndef TPSA_MODEL_HPP
26#define TPSA_MODEL_HPP
27
28#include <dune/grid/common/gridenums.hh>
29
30#include <dune/common/dynmatrix.hh>
31#include <dune/common/dynvector.hh>
32
33#include <opm/grid/utility/ElementChunks.hpp>
34
35#include <opm/common/utility/SymmTensor.hpp>
36#include <opm/common/utility/VoigtArray.hpp>
37#include <opm/material/common/MathToolbox.hpp>
38
43
44#include <array>
45#include <memory>
46#include <unordered_map>
47
48
49namespace Opm {
50
56template <class TypeTag>
58{
70
71 enum { dimWorld = GridView::dimensionworld };
72 enum { historySize = getPropValue<TypeTag, Properties::SolutionHistorySizeTPSA>() };
73 enum { numEq = getPropValue<TypeTag, Properties::NumEqTPSA>() };
74
75 enum { disp0Idx = Indices::disp0Idx };
76 enum { rot0Idx = Indices::rot0Idx };
77 enum { solidPres0Idx = Indices::solidPres0Idx };
78
79 using MaterialState = MaterialStateTPSA<Evaluation>;
80
81 using DimVector = Dune::FieldVector<Scalar, dimWorld>;
82 using SymTensor = SymmTensor<Scalar>;
83 using PotForceVector = Dune::BlockVector<Scalar>;
84
85public:
90 {
91 protected:
92 SolutionVector blockVector_;
93 public:
100 TpsaBlockVectorWrapper(const std::string&, const std::size_t size)
101 : blockVector_(size)
102 {}
103
108
113 {
114 TpsaBlockVectorWrapper result("dummy", 3);
115 result.blockVector_[0] = 1.0;
116 result.blockVector_[1] = 2.0;
117 result.blockVector_[2] = 3.0;
118
119 return result;
120 }
121
127 SolutionVector& blockVector()
128 { return blockVector_; }
129
135 const SolutionVector& blockVector() const
136 { return blockVector_; }
137
144 bool operator==(const TpsaBlockVectorWrapper& wrapper) const
145 {
146 return std::equal(this->blockVector_.begin(), this->blockVector_.end(),
147 wrapper.blockVector_.begin(), wrapper.blockVector_.end());
148 }
149
155 template<class Serializer>
156 void serializeOp(Serializer& serializer)
157 {
158 serializer(blockVector_);
159 }
160 };
161
162 // ///
163 // Public functions
164 // ///
170 explicit TpsaModel(Simulator& simulator)
171 : linearizer_(std::make_unique<Linearizer>())
172 , newtonMethod_(simulator)
173 , simulator_(simulator)
174 , element_chunks_(simulator.gridView(), Dune::Partitions::all, ThreadManager::maxThreads())
175 {
176 // Initialize equation weights to 1.0
177 eqWeights_.resize(numEq, 1.0);
178
179 // Initialize historic solution vectors
180 // OBS: need at least history size = 2, due to time-derivative of solid-pressure in Flow coupling term
181 const std::size_t numDof = simulator_.model().numGridDof();
182 for (unsigned timeIdx = 0; timeIdx < historySize; ++timeIdx) {
183 solution_[timeIdx] = std::make_unique<TpsaBlockVectorWrapper>("solution", numDof);
184 }
185
186 // Initialize potential force vectors
187 mechPotPresForce_.resize(numDof);
188 mechPotTempForce_.resize(numDof);
189 }
190
195 {
196 // Initialize the linearizer
197 linearizer_->init(simulator_);
198
199 // Resize material state vector
201 }
202
206 static void registerParameters()
207 {
208 // Newton method parameters
210 }
211
216 {
217 // Update historic solution
218 solution(/*timeIdx=*/1) = solution(/*timeIdx=*/0);
219 }
220
227 {
228 // Syncronize the solution on the ghost and overlap elements
229 using GhostSyncHandle = GridCommHandleGhostSync<PrimaryVariables,
230 SolutionVector,
231 DofMapper,
232 /*commCodim=*/0>;
233
234 auto ghostSync = GhostSyncHandle(solution(/*timeIdx=*/0),
235 simulator_.model().dofMapper());
236
237 simulator_.gridView().communicate(ghostSync,
238 Dune::InteriorBorder_All_Interface,
239 Dune::ForwardCommunication);
240 }
241
249 void updateMaterialState(const unsigned /*timeIdx*/)
250 {
251 // Loop over all elements chuncks and update material state from current solution
252 const auto& elementMapper = simulator_.model().elementMapper();
253#ifdef _OPENMP
254#pragma omp parallel for
255#endif
256 for (const auto& chunk : element_chunks_) {
257 for (const auto& elem : chunk) {
258 const unsigned globalIdx = elementMapper.index(elem);
259 auto& currSol = solution(/*timeIdx=*/0)[globalIdx];
260 setMaterialState_(globalIdx, /*timeIdx=*/0, currSol);
261 }
262 }
263 }
264
265 // ///
266 // Public get and set functions
267 // ///
273 const Linearizer& linearizer() const
274 {
275 return *linearizer_;
276 }
277
283 Linearizer& linearizer()
284 {
285 return *linearizer_;
286 }
287
293 const NewtonMethod& newtonMethod() const
294 {
295 return newtonMethod_;
296 }
297
303 NewtonMethod& newtonMethod()
304 {
305 return newtonMethod_;
306 }
307
314 const SolutionVector& solution(unsigned timeIdx) const
315 {
316 return solution_[timeIdx]->blockVector();
317 }
318
325 SolutionVector& solution(unsigned timeIdx)
326 {
327 return solution_[timeIdx]->blockVector();
328 }
329
334 std::size_t numGridDof() const
335 {
336 return simulator_.model().numGridDof();
337 }
338
343 std::size_t numTotalDof() const
344 {
345 return numGridDof() + numAuxiliaryDof();
346 }
347
354 Scalar dofTotalVolume(unsigned globalIdx) const
355 {
356 return simulator_.model().dofTotalVolume(globalIdx);
357 }
358
366 Scalar eqWeight(unsigned /*dofIdx*/, unsigned eqIdx) const
367 {
368 return eqWeights_[eqIdx];
369 }
370
377 void setEqWeight(unsigned eqIdx, Scalar value)
378 {
379 eqWeights_[eqIdx] = value;
380 }
381
386 std::size_t numAuxiliaryModules() const
387 {
388 return 0;
389 }
390
395 std::size_t numAuxiliaryDof() const
396 {
397 return 0;
398 }
399
409 const MaterialState& materialState(const unsigned globalIdx, unsigned /*timeIdx*/) const
410 {
411 return materialState_[globalIdx];
412 }
413
423 DimVector disp(const unsigned globalIdx, const bool /*with_fracture*/) const
424 {
425 DimVector d;
426 for (std::size_t i = 0; i < 3; ++i) {
427 d[i] = decay<Scalar>(materialState_[globalIdx].displacement(i));
428 }
429 return d;
430 }
431
438 DimVector rotation(const unsigned globalIdx) const
439 {
440 DimVector rot;
441 for (std::size_t i = 0; i < 3; ++i) {
442 rot[i] = decay<Scalar>(materialState_[globalIdx].rotation(i));
443 }
444 return rot;
445 }
446
453 Scalar solidPressure(const unsigned globalIdx) const
454 {
455 return decay<Scalar>(materialState_[globalIdx].solidPressure());
456 }
457
464 SymTensor delstress(const unsigned globalIdx) const
465 {
466 return stress(globalIdx, false);
467 }
468
477 SymTensor fractureStress(const unsigned /*globalIdx*/) const
478 {
479 SymTensor val;
480 return val;
481 }
482
491 SymTensor linstress(const unsigned globalIdx) const
492 {
493 // Subtract the potential forces from pressure and temperature from the main diagonal of
494 // the total stress
495 SymTensor linStressTensor = stress(globalIdx, false);
496 const auto potForce = mechPotentialForce(globalIdx);
497 for (const auto& dirIdx : SymTensor::diag_indices) {
498 linStressTensor[dirIdx] -= potForce;
499 }
500 return -1.0 * linStressTensor;
501 }
502
513 SymTensor stress(const unsigned globalIdx, const bool /*with_fracture*/) const
514 {
515 SymTensor stressOutput;
516 const auto& stressInfo = linearizer_->getStressInfo();
517 if (!stressInfo.empty()) {
518 const auto& stressInfoGlobI = stressInfo[globalIdx];
519
520 // Setup least-squares matrix of face normals and right-hand side of traction vectors
521 // per faces. Matrix shape = 3 rows per face x 6 columns for each (symmetric) stress
522 // tensor component.
523 std::unordered_multimap<int, std::size_t> faceIdMap;
524 std::size_t mapSize = 0;
525 for (std::size_t sInfoIdx = 0; sInfoIdx < stressInfoGlobI.size(); ++sInfoIdx) {
526 const auto faceId = stressInfoGlobI[sInfoIdx].faceId;
527
528 // OBS: NNC faces are not added to least squares system, since TPSA does not handle
529 // NNC connections, like numerical aquifers, yet!
530 if (faceId < 0 || stressInfoGlobI[sInfoIdx].faceArea == 0.0) {
531 continue;
532 }
533 if (!faceIdMap.contains(faceId)) {
534 ++mapSize;
535 }
536 faceIdMap.insert({faceId, sInfoIdx});
537 }
538
539 // If no valid faces exist for cell, return zero SymTensor
540 if (mapSize == 0) {
541 return stressOutput;
542 }
543
544 Dune::DynamicMatrix<Scalar> mat(3 * mapSize, 6);
545 Dune::DynamicVector<Scalar> rhs(3 * mapSize);
546 std::size_t rowIdx = 0;
547 for (auto it = faceIdMap.begin(); it != faceIdMap.end();) {
548 auto [first, last] = faceIdMap.equal_range(it->first);
549
550 // Loop over possible multiple of the same face
551 Scalar sumFaceArea = 0.0;
552 for (auto inner = first; inner != last; ++inner) {
553 const auto sInfoIdx = inner->second;
554 const auto& faceStressInfo = stressInfoGlobI[sInfoIdx];
555
556 // One normal per face
557 if (inner == first) {
558 const auto& fNormal = faceStressInfo.faceNormal;
559 mat[3*rowIdx][0] = fNormal[0];
560 mat[3*rowIdx][3] = fNormal[1];
561 mat[3*rowIdx][4] = fNormal[2];
562
563 mat[3*rowIdx + 1][1] = fNormal[1];
564 mat[3*rowIdx + 1][3] = fNormal[0];
565 mat[3*rowIdx + 1][5] = fNormal[2];
566
567 mat[3*rowIdx + 2][2] = fNormal[2];
568 mat[3*rowIdx + 2][4] = fNormal[0];
569 mat[3*rowIdx + 2][5] = fNormal[1];
570 }
571
572 // Add traction vectors for same faces
573 const auto& fTraction = faceStressInfo.traction;
574 rhs[3*rowIdx] += fTraction[0];
575 rhs[3*rowIdx + 1] += fTraction[1];
576 rhs[3*rowIdx + 2] += fTraction[2];
577
578 // Sum face area
579 sumFaceArea += faceStressInfo.faceArea;
580 }
581 // Divide rhs for (unique) face by sum of face areas
582 rhs[3*rowIdx] /= sumFaceArea;
583 rhs[3*rowIdx + 1] /= sumFaceArea;
584 rhs[3*rowIdx + 2] /= sumFaceArea;
585
586 // Move to next unique face
587 ++rowIdx;
588 it = last;
589 }
590
591 // Use least squares solver for underdetermined system
592 LinearLeastSquares<Scalar> lsq(mat, rhs);
593 lsq.solve();
594
595 // Reconstructed stress tensor at cell center
596 const auto& stress = lsq.x();
597 stressOutput[VoigtIndex::XX] = stress[0]; // XX
598 stressOutput[VoigtIndex::YY] = stress[1]; // YY
599 stressOutput[VoigtIndex::ZZ] = stress[2]; // ZZ
600 stressOutput[VoigtIndex::YZ] = stress[5]; // YZ
601 stressOutput[VoigtIndex::XZ] = stress[4]; // XZ
602 stressOutput[VoigtIndex::XY] = stress[3]; // XY
603 }
604 return stressOutput;
605 }
606
616 std::array<DimVector, 6> traction(const unsigned globalIdx) const
617 {
618 std::array<DimVector, 6> tractionOutput{};
619 const auto& stressInfo = linearizer_->getStressInfo();
620 if (stressInfo.empty()) {
621 return tractionOutput;
622 }
623
624 // Average traction (force per area) over all face entries sharing the same face
625 // direction, consistent with the face-area weighting used in stress().
626 std::array<Scalar, 6> sumFaceArea{};
627 for (const auto& faceStressInfo : stressInfo[globalIdx]) {
628 const auto faceId = faceStressInfo.faceId;
629
630 // OBS: NNC faces are not added, since TPSA does not handle NNC connections yet!
631 if (faceId < 0 || faceStressInfo.faceArea == 0.0) {
632 continue;
633 }
634
635 tractionOutput[faceId] += faceStressInfo.traction;
636 sumFaceArea[faceId] += faceStressInfo.faceArea;
637 }
638
639 for (std::size_t faceId = 0; faceId < 6; ++faceId) {
640 if (sumFaceArea[faceId] > 0.0) {
641 tractionOutput[faceId] /= sumFaceArea[faceId];
642 }
643 }
644
645 return tractionOutput;
646 }
647
655 SymTensor strain(const unsigned globalIdx, const bool /*with_fracture*/) const
656 {
657 // Deviatoric stress
658 auto stressDev = this->linstress(globalIdx);
659 Scalar traceStress = 1.0 / 3.0 * stressDev.trace();
660 stressDev[VoigtIndex::XX] -= traceStress;
661 stressDev[VoigtIndex::YY] -= traceStress;
662 stressDev[VoigtIndex::ZZ] -= traceStress;
663
664 // Deviatoric strain
665 auto& problem = simulator_.problem();
666 const auto sMod = problem.shearModulus(globalIdx);
667 SymTensor strainDev = 1.0 / (2.0 * sMod) * stressDev;
668
669 // Volumetric strain
670 const auto lameParam = problem.lame(globalIdx);
671 Scalar strainVolTerm = traceStress / (3.0 * lameParam + 2 * sMod);
672
673 SymTensor strainVol;
674 strainVol[VoigtIndex::XX] = strainVolTerm;
675 strainVol[VoigtIndex::YY] = strainVolTerm;
676 strainVol[VoigtIndex::ZZ] = strainVolTerm;
677
678 // Total
679 SymTensor strainOutput = strainDev + strainVol;
680 return strainOutput;
681 }
682
691 Scalar mechPotentialForce(unsigned globalIdx) const
692 {
693 return mechPotentialPressForce(globalIdx) + mechPotentialTempForce(globalIdx);
694 }
695
702 Scalar mechPotentialPressForce(unsigned globalIdx) const
703 {
704 return mechPotPresForce_[globalIdx];
705 }
706
713 void setMechPotentialPressForce(unsigned globalIdx, Scalar val)
714 {
715 mechPotPresForce_[globalIdx] = val;
716 }
717
724 Scalar mechPotentialTempForce(unsigned globalIdx) const
725 {
726 return mechPotTempForce_[globalIdx];
727 }
728
735 void setMechPotentialTempForce(unsigned globalIdx, Scalar val)
736 {
737 mechPotTempForce_[globalIdx] = val;
738 }
739
740protected:
741 // ///
742 // Protected functions
743 // ///
750 {
751 const std::size_t numDof = simulator_.model().numGridDof();
752 materialState_.resize(numDof);
753 }
754
755private:
756 // ///
757 // Private functions
758 // ///
768 void setMaterialState_(const unsigned globalIdx, const unsigned /*timeIdx*/, PrimaryVariables& values)
769 {
770 auto& dofMaterialState = materialState_[globalIdx];
771 for (unsigned dirIdx = 0; dirIdx < 3; ++dirIdx) {
772 dofMaterialState.setDisplacement(dirIdx, values.makeEvaluation(disp0Idx + dirIdx, 0));
773 dofMaterialState.setRotation(dirIdx, values.makeEvaluation(rot0Idx + dirIdx, 0));
774 }
775 dofMaterialState.setSolidPressure(values.makeEvaluation(solidPres0Idx, 0));
776 }
777
778 std::unique_ptr<Linearizer> linearizer_;
779 NewtonMethod newtonMethod_;
780 Simulator& simulator_;
781 ElementChunks<GridView, Dune::Partitions::All> element_chunks_;
782
783 std::array<std::unique_ptr<TpsaBlockVectorWrapper>, historySize> solution_;
784 std::vector<Scalar> eqWeights_;
785 std::vector<MaterialState> materialState_;
786 PotForceVector mechPotPresForce_;
787 PotForceVector mechPotTempForce_;
788}; // class TpsaModel
789
790} // namespace Opm
791
792
793#endif
Data handle for parallel communication which can be used to set the values values of ghost and overla...
Definition: gridcommhandles.hh:109
Linear least squares calculations and properties.
Definition: linearleastsquares.hpp:45
const Vector & x() const
Read-only vector of calculated coefficient vector.
Definition: linearleastsquares.hpp:76
void solve()
Solve linear least squares system.
Definition: linearleastsquares.hpp:66
static void registerParameters()
Register all run-time parameters for the Newton method.
Definition: newtonmethod.hh:135
Simplifies multi-threaded capabilities.
Definition: threadmanager.hpp:36
Small block vector wrapper class for model solutions.
Definition: tpsamodel.hpp:90
bool operator==(const TpsaBlockVectorWrapper &wrapper) const
Check if incoming block vector is the same as current.
Definition: tpsamodel.hpp:144
static TpsaBlockVectorWrapper serializationTestObject()
Test function for serialization.
Definition: tpsamodel.hpp:112
TpsaBlockVectorWrapper()=default
Default constructor.
TpsaBlockVectorWrapper(const std::string &, const std::size_t size)
Constructor.
Definition: tpsamodel.hpp:100
void serializeOp(Serializer &serializer)
Serializing operation.
Definition: tpsamodel.hpp:156
const SolutionVector & blockVector() const
Get const reference of block vector.
Definition: tpsamodel.hpp:135
SolutionVector & blockVector()
Get reference of block vector.
Definition: tpsamodel.hpp:127
SolutionVector blockVector_
Definition: tpsamodel.hpp:92
TPSA geomechanics model.
Definition: tpsamodel.hpp:58
SymTensor delstress(const unsigned globalIdx) const
Output stress tensor without fracture contribution.
Definition: tpsamodel.hpp:464
NewtonMethod & newtonMethod()
Return the Newton method.
Definition: tpsamodel.hpp:303
std::size_t numAuxiliaryDof() const
Return number of auxillary degrees of freedom.
Definition: tpsamodel.hpp:395
SymTensor stress(const unsigned globalIdx, const bool) const
Output stress tensor.
Definition: tpsamodel.hpp:513
SymTensor strain(const unsigned globalIdx, const bool) const
Output strain tensor.
Definition: tpsamodel.hpp:655
void setMechPotentialTempForce(unsigned globalIdx, Scalar val)
Sets potential temperature force.
Definition: tpsamodel.hpp:735
DimVector disp(const unsigned globalIdx, const bool) const
Output displacement vector.
Definition: tpsamodel.hpp:423
void finishInit()
Initialize TPSA model.
Definition: tpsamodel.hpp:194
void setEqWeight(unsigned eqIdx, Scalar value)
Set weights for equation.
Definition: tpsamodel.hpp:377
std::size_t numGridDof() const
Return number of degrees of freedom in the grid from the Flow model.
Definition: tpsamodel.hpp:334
const Linearizer & linearizer() const
Return the linearizer.
Definition: tpsamodel.hpp:273
const NewtonMethod & newtonMethod() const
Return the Newton method.
Definition: tpsamodel.hpp:293
std::size_t numAuxiliaryModules() const
Return number of auxillary modules.
Definition: tpsamodel.hpp:386
void updateMaterialState(const unsigned)
Update material state for all cells.
Definition: tpsamodel.hpp:249
Scalar mechPotentialPressForce(unsigned globalIdx) const
Output potential pressure forces.
Definition: tpsamodel.hpp:702
Scalar mechPotentialTempForce(unsigned globalIdx) const
Output potential temparature forces.
Definition: tpsamodel.hpp:724
SymTensor linstress(const unsigned globalIdx) const
Output linear stress tensor.
Definition: tpsamodel.hpp:491
const SolutionVector & solution(unsigned timeIdx) const
Get reference to history solution vector.
Definition: tpsamodel.hpp:314
const MaterialState & materialState(const unsigned globalIdx, unsigned) const
Return current material state.
Definition: tpsamodel.hpp:409
Scalar dofTotalVolume(unsigned globalIdx) const
Return the total grid volume from the Flow model.
Definition: tpsamodel.hpp:354
void resizeMaterialState_()
Resize material state vector.
Definition: tpsamodel.hpp:749
static void registerParameters()
Register runtime parameters.
Definition: tpsamodel.hpp:206
void setMechPotentialPressForce(unsigned globalIdx, Scalar val)
Sets potential pressure force.
Definition: tpsamodel.hpp:713
Scalar eqWeight(unsigned, unsigned eqIdx) const
Return equation weights.
Definition: tpsamodel.hpp:366
TpsaModel(Simulator &simulator)
Constructor.
Definition: tpsamodel.hpp:170
std::array< DimVector, 6 > traction(const unsigned globalIdx) const
Output traction vector for each of the 6 face directions.
Definition: tpsamodel.hpp:616
SolutionVector & solution(unsigned timeIdx)
Get reference to history solution vector.
Definition: tpsamodel.hpp:325
void prepareTPSA()
Prepare TPSA model for coupled Flow-TPSA scheme.
Definition: tpsamodel.hpp:215
Scalar solidPressure(const unsigned globalIdx) const
Output solid pressure.
Definition: tpsamodel.hpp:453
SymTensor fractureStress(const unsigned) const
Output fracture stress tensor.
Definition: tpsamodel.hpp:477
std::size_t numTotalDof() const
Return the total number of degrees of freedom.
Definition: tpsamodel.hpp:343
Linearizer & linearizer()
Return the linearizer.
Definition: tpsamodel.hpp:283
void syncOverlap()
Sync primary variables in overlapping cells.
Definition: tpsamodel.hpp:226
DimVector rotation(const unsigned globalIdx) const
Output rotation vector.
Definition: tpsamodel.hpp:438
Scalar mechPotentialForce(unsigned globalIdx) const
Output total potential forces.
Definition: tpsamodel.hpp:691
Definition: fvbaseprimaryvariables.hh:161
Definition: blackoilbioeffectsmodules.hh:45
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:233
The Opm property system, traits with inheritance.