version 0.4.1
vanishingstress.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
10// SPDX-License-Identifier: LGPL-3.0-or-later
11
12#pragma once
13
17
18namespace Ikarus {
19
20namespace Impl {
21
25 struct StressIndexPair
26 {
27 Eigen::Index row;
28 Eigen::Index col;
29 };
30
37 template <size_t size>
38 consteval auto createfreeVoigtIndices(const std::array<StressIndexPair, size>& fixed) {
39 std::array<size_t, 6 - size> res{};
40 std::array<size_t, size> voigtFixedIndices;
41 std::ranges::transform(fixed, voigtFixedIndices.begin(), [](auto pair) { return toVoigt(pair.row, pair.col); });
42 std::ranges::sort(voigtFixedIndices);
43 std::ranges::set_difference(std::ranges::iota_view(size_t(0), size_t(6)), voigtFixedIndices, res.begin());
44 std::ranges::sort(res);
45 return res;
46 }
47
54 template <size_t size>
55 consteval auto createFixedVoigtIndices(const std::array<StressIndexPair, size>& fixed) {
56 std::array<size_t, size> fixedIndices;
57 std::ranges::transform(fixed, fixedIndices.begin(), [](auto pair) { return toVoigt(pair.row, pair.col); });
58 std::ranges::sort(fixedIndices);
59 return fixedIndices;
60 }
61
68 template <size_t size>
69 constexpr size_t countDiagonalIndices(const std::array<StressIndexPair, size>& fixed) {
70 size_t count = 0;
71 for (auto v : fixed) {
72 if (v.col == v.row)
73 ++count;
74 }
75 return count;
76 }
77
78} // namespace Impl
79
86template <auto stressIndexPair, typename MI>
87struct VanishingStress : public Material<VanishingStress<stressIndexPair, MI>>
88{
94 explicit VanishingStress(MI mat, typename MI::ScalarType tol = 1e-12)
95 : matImpl_{mat},
96 tol_{tol} {}
97
98 using Underlying = MI;
99
100 static constexpr auto fixedPairs = stressIndexPair;
101 static constexpr auto freeVoigtIndices = createfreeVoigtIndices(fixedPairs);
102 static constexpr auto fixedVoigtIndices = createFixedVoigtIndices(fixedPairs);
103 static constexpr auto fixedDiagonalVoigtIndicesSize =
104 countDiagonalIndices(fixedPairs);
105 static constexpr auto freeStrains = freeVoigtIndices.size();
106 using ScalarType = typename Underlying::ScalarType;
107
108 [[nodiscard]] constexpr std::string nameImpl() const noexcept {
109 auto matName = matImpl_.name() + "_Vanishing(";
110 for (auto p : fixedPairs)
111 matName += "(" + std::to_string(p.row) + std::to_string(p.col) + ")";
112 matName += ")";
113 return matName;
114 }
115
116 static constexpr auto strainTag = Underlying::strainTag;
117 static constexpr auto stressTag = Underlying::stressTag;
118 static constexpr auto tangentModuliTag = Underlying::tangentModuliTag;
119 static constexpr bool energyAcceptsVoigt = Underlying::energyAcceptsVoigt;
120 static constexpr bool stressToVoigt = true;
121 static constexpr bool stressAcceptsVoigt = true;
122 static constexpr bool moduliToVoigt = true;
123 static constexpr bool moduliAcceptsVoigt = true;
124 static constexpr double derivativeFactor = 1;
125
132 template <typename Derived>
133 ScalarType storedEnergyImpl(const Eigen::MatrixBase<Derived>& E) const {
134 const auto [nonOp, Esol] = reduceStress(E);
135 return matImpl_.storedEnergyImpl(Esol);
136 }
137
145 template <bool voigt, typename Derived>
146 auto stressesImpl(const Eigen::MatrixBase<Derived>& E) const {
147 const auto [nonOp, Esol] = reduceStress(E);
148 auto stressesRed = matImpl_.template stresses<Underlying::strainTag, true>(Esol);
149
150 if constexpr (voigt) {
151 return removeCol(stressesRed, fixedVoigtIndices);
152 } else
153 return fromVoigt(stressesRed, false);
154 }
155
163 template <bool voigt, typename Derived>
164 auto tangentModuliImpl(const Eigen::MatrixBase<Derived>& E) const {
165 const auto [nonOp, Esol] = reduceStress(E);
166 auto C = matImpl_.template tangentModuli<Underlying::strainTag, true>(Esol);
167 if constexpr (voigt)
169 else
170 return fromVoigt(C);
171 }
172
178 template <typename ScalarTypeOther>
179 auto rebind() const {
180 auto reboundMatImpl = matImpl_.template rebind<ScalarTypeOther>();
182 }
183
184private:
191 template <typename Derived>
192 decltype(auto) maybeFromVoigt(const Eigen::MatrixBase<Derived>& E) const {
193 if constexpr (Concepts::EigenVector<Derived>) { // receiving vector means Voigt notation
194 return fromVoigt(E.derived(), true);
195 } else
196 return E.derived();
197 }
198
204 template <typename Derived>
205 void initUnknownStrains(Eigen::MatrixBase<Derived>& E) const {
206 for (size_t i = 0; i < fixedPairs.size(); ++i) {
207 ScalarType initialVal = E(fixedPairs[i].row, fixedPairs[i].col);
209 if (Dune::FloatCmp::eq(initialVal, ScalarType(0.0)) and (fixedPairs[i].row == fixedPairs[i].col))
210 initialVal = ScalarType(1.0);
211 }
212 if (fixedPairs[i].row != fixedPairs[i].col)
213 initialVal = ScalarType(0.0);
214 E(fixedPairs[i].row, fixedPairs[i].col) = E(fixedPairs[i].col, fixedPairs[i].row) = initialVal;
215 }
216 }
217
224 template <typename Derived>
225 auto reduceStress(const Eigen::MatrixBase<Derived>& Eraw) const {
226 auto E = maybeFromVoigt(Eraw);
227 initUnknownStrains(E);
228
229 std::array<size_t, fixedDiagonalVoigtIndicesSize> fixedDiagonalVoigtIndices;
230 for (size_t ri = 0; auto i : fixedVoigtIndices) {
231 auto indexPair = fromVoigt(i);
232 if (indexPair[0] == indexPair[1])
233 fixedDiagonalVoigtIndices[ri++] = i;
234 }
235
236 auto f = [&](auto&) {
237 auto S = matImpl_.template stresses<Underlying::strainTag, true>(E);
238 return S(fixedDiagonalVoigtIndices).eval();
239 };
240 auto df = [&](auto&) {
241 auto moduli = (matImpl_.template tangentModuli<Underlying::strainTag, true>(E)).eval();
242 return (moduli(fixedDiagonalVoigtIndices, fixedDiagonalVoigtIndices) / Underlying::derivativeFactor).eval();
243 };
244
245 auto Er = E(fixedDiagonalVoigtIndices, fixedDiagonalVoigtIndices).eval().template cast<ScalarType>();
246 auto nonOp = Ikarus::NonLinearOperator(functions(f, df), parameter(Er));
248 nonOp, [&](auto& r, auto& A) { return (A.inverse() * r).eval(); },
249 [&](auto& /* Ex33 */, auto& ecomps) {
250 for (int ri = 0; auto i : fixedDiagonalVoigtIndices) {
251 auto indexPair = fromVoigt(i);
252 E(indexPair[0], indexPair[1]) += ecomps(ri++);
253 }
254 });
255 nr->setup({.tol = tol_, .maxIter = 100});
256 if (!static_cast<bool>(nr->solve()))
257 DUNE_THROW(Dune::MathError, "The stress reduction of material " << nameImpl() << " was unsuccessful\n"
258 << "The strains are\n"
259 << E << "\n The stresses are\n"
260 << f(Er));
261 return std::make_pair(nonOp, E);
262 }
263
264 Underlying matImpl_;
265 double tol_{};
266};
267
276template <Impl::StressIndexPair... stressIndexPair, typename MaterialImpl>
277auto makeVanishingStress(MaterialImpl mat, typename MaterialImpl::ScalarType p_tol = 1e-12) {
278 return VanishingStress<std::to_array({stressIndexPair...}), MaterialImpl>(mat, p_tol);
279}
280
288template <typename MaterialImpl>
289auto planeStress(const MaterialImpl& mat, typename MaterialImpl::ScalarType tol = 1e-8) {
290 return makeVanishingStress<Impl::StressIndexPair{2, 1}, Impl::StressIndexPair{2, 0}, Impl::StressIndexPair{2, 2}>(
291 mat, tol);
292}
293
302template <typename MaterialImpl>
303auto shellMaterial(const MaterialImpl& mat, typename MaterialImpl::ScalarType tol = 1e-8) {
304 return makeVanishingStress<Impl::StressIndexPair{2, 2}>(mat, tol);
305}
306
315template <typename MaterialImpl>
316auto beamMaterial(const MaterialImpl& mat, typename MaterialImpl::ScalarType tol = 1e-8) {
317 return makeVanishingStress<Impl::StressIndexPair{1, 1}, Impl::StressIndexPair{2, 2}>(mat, tol);
318}
319} // namespace Ikarus
Provides a NonLinearOperator class for handling nonlinear operators.
Implementation of the Newton-Raphson method for solving nonlinear equations.
Contains the Material interface class and related template functions for material properties.
auto staticCondensation(const Eigen::MatrixBase< Derived > &E, const std::array< size_t, sizeOfCondensedIndices > &indices)
Performs static condensation on a square matrix.
Definition: linearalgebrahelper.hh:498
auto removeCol(const Eigen::MatrixBase< Derived > &E, const std::array< size_t, sizeOfRemovedCols > &indices)
Removes specified columns from a matrix.
Definition: linearalgebrahelper.hh:526
auto fromVoigt(const Eigen::Vector< ST, size > &EVoigt, bool isStrain=true)
Converts a vector given in Voigt notation to a matrix.
Definition: tensorutils.hh:256
Definition: simpleassemblers.hh:22
auto makeVanishingStress(MaterialImpl mat, typename MaterialImpl::ScalarType p_tol=1e-12)
Factory function to create a VanishingStress material with specified stress indices.
Definition: vanishingstress.hh:277
auto shellMaterial(const MaterialImpl &mat, typename MaterialImpl::ScalarType tol=1e-8)
Factory function to create a VanishingStress material for a shell material with zero normal stress co...
Definition: vanishingstress.hh:303
auto beamMaterial(const MaterialImpl &mat, typename MaterialImpl::ScalarType tol=1e-8)
Factory function to create a VanishingStress material for a beam material with two zero normal stress...
Definition: vanishingstress.hh:316
auto functions(Args &&... args)
Creates a Functions object.
Definition: nonlinearoperator.hh:127
auto parameter(Args &&... args)
Creates a Parameter object.
Definition: nonlinearoperator.hh:115
auto planeStress(const MaterialImpl &mat, typename MaterialImpl::ScalarType tol=1e-8)
Factory function to create a VanishingStress material for plane stress conditions.
Definition: vanishingstress.hh:289
auto makeNewtonRaphson(const NLO &nonLinearOperator, LS &&linearSolver={}, UF &&updateFunction={})
Function to create a NewtonRaphson solver instance.
Definition: newtonraphson.hh:161
Interface classf or materials.
Definition: interface.hh:77
VanishingStress material model that enforces stress components to be zero.
Definition: vanishingstress.hh:88
static constexpr auto tangentModuliTag
Tangent moduli tag.
Definition: vanishingstress.hh:118
auto rebind() const
Rebinds the material to a different scalar type.
Definition: vanishingstress.hh:179
static constexpr auto fixedPairs
Array of fixed stress components.
Definition: vanishingstress.hh:100
static constexpr double derivativeFactor
Derivative factor.
Definition: vanishingstress.hh:124
VanishingStress(MI mat, typename MI::ScalarType tol=1e-12)
Constructor for VanishingStress.
Definition: vanishingstress.hh:94
static constexpr auto strainTag
Strain tag.
Definition: vanishingstress.hh:116
static constexpr auto freeVoigtIndices
Free Voigt indices.
Definition: vanishingstress.hh:101
constexpr std::string nameImpl() const noexcept
Definition: vanishingstress.hh:108
static constexpr bool energyAcceptsVoigt
Energy accepts Voigt notation.
Definition: vanishingstress.hh:119
ScalarType storedEnergyImpl(const Eigen::MatrixBase< Derived > &E) const
Computes the stored energy for the VanishingStress material.
Definition: vanishingstress.hh:133
MI Underlying
The underlying material type.
Definition: vanishingstress.hh:98
auto stressesImpl(const Eigen::MatrixBase< Derived > &E) const
Computes the stresses for the VanishingStress material.
Definition: vanishingstress.hh:146
typename Underlying::ScalarType ScalarType
Scalar type.
Definition: vanishingstress.hh:106
static constexpr bool moduliToVoigt
Moduli to Voigt notation.
Definition: vanishingstress.hh:122
static constexpr bool stressToVoigt
Stress to Voigt notation.
Definition: vanishingstress.hh:120
auto tangentModuliImpl(const Eigen::MatrixBase< Derived > &E) const
Computes the tangent moduli for the VanishingStress material.
Definition: vanishingstress.hh:164
static constexpr auto fixedDiagonalVoigtIndicesSize
Number of fixed diagonal indices.
Definition: vanishingstress.hh:103
static constexpr auto freeStrains
Number of free strains.
Definition: vanishingstress.hh:105
static constexpr auto stressTag
Stress tag.
Definition: vanishingstress.hh:117
static constexpr bool stressAcceptsVoigt
Stress accepts Voigt notation.
Definition: vanishingstress.hh:121
static constexpr auto fixedVoigtIndices
Fixed Voigt indices.
Definition: vanishingstress.hh:102
static constexpr bool moduliAcceptsVoigt
Moduli accepts Voigt notation.
Definition: vanishingstress.hh:123
Represents a NonLinearOperator class for handling nonlinear operators.
Definition: nonlinearoperator.hh:156
Concept defining the requirements for Eigen vectors.
Definition: concepts.hh:353