OGS
CentralDifferencesJacobianAssembler.cpp
Go to the documentation of this file.
1
12
13#include "BaseLib/ConfigTree.h"
14#include "BaseLib/Error.h"
17
18namespace ProcessLib
19{
21 std::vector<double>&& absolute_epsilons)
22 : _absolute_epsilons(std::move(absolute_epsilons))
23{
24 if (_absolute_epsilons.empty())
25 {
26 OGS_FATAL("No values for the absolute epsilons have been given.");
27 }
28}
29
31 LocalAssemblerInterface& local_assembler, const double t, double const dt,
32 const std::vector<double>& local_x_data,
33 const std::vector<double>& local_x_prev_data,
34 std::vector<double>& local_b_data, std::vector<double>& local_Jac_data)
35{
36 std::vector<double> local_M_data(local_Jac_data.size());
37 std::vector<double> local_K_data(local_Jac_data.size());
38
39 // TODO do not check in every call.
40 if (local_x_data.size() % _absolute_epsilons.size() != 0)
41 {
43 "The number of specified epsilons ({:d}) and the number of local "
44 "d.o.f.s ({:d}) do not match, i.e., the latter is not divisible by "
45 "the former.",
46 _absolute_epsilons.size(), local_x_data.size());
47 }
48
49 auto const num_r_c =
50 static_cast<Eigen::MatrixXd::Index>(local_x_data.size());
51
52 auto const local_x =
53 MathLib::toVector<Eigen::VectorXd>(local_x_data, num_r_c);
54 auto const local_x_prev =
55 MathLib::toVector<Eigen::VectorXd>(local_x_prev_data, num_r_c);
56 Eigen::VectorXd const local_xdot = (local_x - local_x_prev) / dt;
57
58 auto local_Jac =
59 MathLib::createZeroedMatrix(local_Jac_data, num_r_c, num_r_c);
60 _local_x_perturbed_data = local_x_data;
61
62 auto const num_dofs_per_component =
63 local_x_data.size() / _absolute_epsilons.size();
64
65 // Residual res := M xdot + K x - b
66 // Computing Jac := dres/dx
67 // = M dxdot/dx + dM/dx xdot + K dx/dx + dK/dx x - db/dx
68 // with dxdot/dx = 1/dt and dx/dx = 1
69 // (Note: dM/dx and dK/dx actually have the second and
70 // third index transposed.)
71 // The loop computes the dM/dx, dK/dx and db/dx terms, the rest is computed
72 // afterwards.
73 for (Eigen::MatrixXd::Index i = 0; i < num_r_c; ++i)
74 {
75 // assume that local_x_data is ordered by component.
76 auto const component = i / num_dofs_per_component;
77 auto const eps = _absolute_epsilons[component];
78
80 local_assembler.assemble(t, dt, _local_x_perturbed_data,
81 local_x_prev_data, local_M_data, local_K_data,
82 local_b_data);
83
84 _local_x_perturbed_data[i] = local_x_data[i] - eps;
85 local_assembler.assemble(t, dt, _local_x_perturbed_data,
86 local_x_prev_data, _local_M_data,
88
89 _local_x_perturbed_data[i] = local_x_data[i];
90
91 if (!local_M_data.empty())
92 {
93 auto const local_M_p =
94 MathLib::toMatrix(local_M_data, num_r_c, num_r_c);
95 auto const local_M_m =
96 MathLib::toMatrix(_local_M_data, num_r_c, num_r_c);
97 local_Jac.col(i).noalias() +=
98 // dM/dxi * x_dot
99 (local_M_p - local_M_m) * local_xdot / (2.0 * eps);
100 local_M_data.clear();
101 _local_M_data.clear();
102 }
103 if (!local_K_data.empty())
104 {
105 auto const local_K_p =
106 MathLib::toMatrix(local_K_data, num_r_c, num_r_c);
107 auto const local_K_m =
108 MathLib::toMatrix(_local_K_data, num_r_c, num_r_c);
109 local_Jac.col(i).noalias() +=
110 // dK/dxi * x
111 (local_K_p - local_K_m) * local_x / (2.0 * eps);
112 local_K_data.clear();
113 _local_K_data.clear();
114 }
115 if (!local_b_data.empty())
116 {
117 auto const local_b_p =
118 MathLib::toVector<Eigen::VectorXd>(local_b_data, num_r_c);
119 auto const local_b_m =
121 local_Jac.col(i).noalias() -=
122 // db/dxi
123 (local_b_p - local_b_m) / (2.0 * eps);
124 local_b_data.clear();
125 _local_b_data.clear();
126 }
127 }
128
129 // Assemble with unperturbed local x.
130 local_assembler.assemble(t, dt, local_x_data, local_x_prev_data,
131 local_M_data, local_K_data, local_b_data);
132
133 // Compute remaining terms of the Jacobian.
134 if (!local_M_data.empty())
135 {
136 auto local_M = MathLib::toMatrix(local_M_data, num_r_c, num_r_c);
137 local_Jac.noalias() += local_M / dt;
138 }
139 if (!local_K_data.empty())
140 {
141 auto local_K = MathLib::toMatrix(local_K_data, num_r_c, num_r_c);
142 local_Jac.noalias() += local_K;
143 }
144
145 // Move the M and K contributions to the residuum for evaluation of nodal
146 // forces, flow rates, and the like. Cleaning up the M's and K's storage so
147 // it is not accounted for twice.
148 auto b = [&]()
149 {
150 if (!local_b_data.empty())
151 {
152 return MathLib::toVector<Eigen::VectorXd>(local_b_data, num_r_c);
153 }
155 num_r_c);
156 }();
157
158 if (!local_M_data.empty())
159 {
160 auto M = MathLib::toMatrix(local_M_data, num_r_c, num_r_c);
161 b -= M * local_xdot;
162 local_M_data.clear();
163 }
164 if (!local_K_data.empty())
165 {
166 auto K = MathLib::toMatrix(local_K_data, num_r_c, num_r_c);
167 b -= K * local_x;
168 local_K_data.clear();
169 }
170}
171
172std::unique_ptr<CentralDifferencesJacobianAssembler>
174{
176 config.checkConfigParameter("type", "CentralDifferences");
177
178 // TODO make non-optional.
180 auto rel_eps = config.getConfigParameterOptional<std::vector<double>>(
181 "relative_epsilons");
183 auto comp_mag = config.getConfigParameterOptional<std::vector<double>>(
184 "component_magnitudes");
185
186 if (!!rel_eps != !!comp_mag)
187 {
188 OGS_FATAL(
189 "Either both or none of <relative_epsilons> and "
190 "<component_magnitudes> have to be specified.");
191 }
192
193 std::vector<double> abs_eps;
194
195 if (rel_eps)
196 {
197 if (rel_eps->size() != comp_mag->size())
198 {
199 OGS_FATAL(
200 "The numbers of components of <relative_epsilons> and "
201 "<component_magnitudes> do not match.");
202 }
203
204 abs_eps.resize(rel_eps->size());
205 for (std::size_t i = 0; i < rel_eps->size(); ++i)
206 {
207 abs_eps[i] = (*rel_eps)[i] * (*comp_mag)[i];
208 }
209 }
210 else
211 {
212 // By default 1e-8 is used as epsilon for all components.
213 // TODO: remove this default value.
214 abs_eps.emplace_back(1e-8);
215 }
216
217 return std::make_unique<CentralDifferencesJacobianAssembler>(
218 std::move(abs_eps));
219}
220
221std::unique_ptr<AbstractJacobianAssembler>
223{
224 return std::make_unique<CentralDifferencesJacobianAssembler>(*this);
225}
226
227} // namespace ProcessLib
#define OGS_FATAL(...)
Definition Error.h:26
std::optional< T > getConfigParameterOptional(std::string const &param) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
CentralDifferencesJacobianAssembler(std::vector< double > &&absolute_epsilons)
std::unique_ptr< AbstractJacobianAssembler > copy() const override
void assembleWithJacobian(LocalAssemblerInterface &local_assembler, double const t, double const dt, std::vector< double > const &local_x_data, std::vector< double > const &local_x_prev_data, std::vector< double > &local_b_data, std::vector< double > &local_Jac_data) override
virtual void assemble(double const t, double const dt, std::vector< double > const &local_x, std::vector< double > const &local_x_prev, std::vector< double > &local_M_data, std::vector< double > &local_K_data, std::vector< double > &local_b_data)
Eigen::Map< Vector > createZeroedVector(std::vector< double > &data, Eigen::VectorXd::Index size)
Eigen::Map< const Vector > toVector(std::vector< double > const &data, Eigen::VectorXd::Index size)
Creates an Eigen mapped vector from the given data vector.
Eigen::Map< Matrix > createZeroedMatrix(std::vector< double > &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
Eigen::Map< const Matrix > toMatrix(std::vector< double > const &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
std::unique_ptr< CentralDifferencesJacobianAssembler > createCentralDifferencesJacobianAssembler(BaseLib::ConfigTree const &config)