OGS
ProcessLib::CentralDifferencesJacobianAssembler Class Referencefinal

Detailed Description

Assembles the Jacobian matrix using central differences.

Definition at line 25 of file CentralDifferencesJacobianAssembler.h.

#include <CentralDifferencesJacobianAssembler.h>

Inheritance diagram for ProcessLib::CentralDifferencesJacobianAssembler:
[legend]
Collaboration diagram for ProcessLib::CentralDifferencesJacobianAssembler:
[legend]

Public Member Functions

 CentralDifferencesJacobianAssembler (std::vector< double > &&absolute_epsilons)
 
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
 
std::unique_ptr< AbstractJacobianAssemblercopy () const override
 
- Public Member Functions inherited from ProcessLib::AbstractJacobianAssembler
virtual void assembleWithJacobianForStaggeredScheme (LocalAssemblerInterface &, double const, double const, Eigen::VectorXd const &, Eigen::VectorXd const &, int const, std::vector< double > &, std::vector< double > &)
 
virtual ~AbstractJacobianAssembler ()=default
 

Private Attributes

std::vector< double > const _absolute_epsilons
 
std::vector< double > _local_M_data
 
std::vector< double > _local_K_data
 
std::vector< double > _local_b_data
 
std::vector< double > _local_x_perturbed_data
 

Constructor & Destructor Documentation

◆ CentralDifferencesJacobianAssembler()

ProcessLib::CentralDifferencesJacobianAssembler::CentralDifferencesJacobianAssembler ( std::vector< double > && absolute_epsilons)
explicit

Constructs a new instance.

Parameters
absolute_epsilonsperturbations of the components of the local solution vector used for evaluating the finite differences.
Note
The size of absolute_epsilons defines the "number of components" of the local solution vector (This is not the number of elements of the vector!). Therefore the size of the local solution vector must be divisible by the size of absolute_epsilons. This is the only consistency check performed. It is not checked whether said "number of components" is sensible. E.g., one could pass one epsilon per node, which would be valid but would not make sense at all.

Definition at line 20 of file CentralDifferencesJacobianAssembler.cpp.

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}
#define OGS_FATAL(...)
Definition Error.h:26

References _absolute_epsilons, and OGS_FATAL.

Member Function Documentation

◆ assembleWithJacobian()

void ProcessLib::CentralDifferencesJacobianAssembler::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 )
overridevirtual

Assembles the Jacobian, the matrices \(M\) and \(K\), and the vector \(b\). For the assembly the assemble() method of the given local_assembler is called several times and the Jacobian is built from finite differences. The number of calls of the assemble() method is \(2N+1\) if \(N\) is the size of local_x_data.

Attention
It is assumed that the local vectors and matrices are ordered by component.

Implements ProcessLib::AbstractJacobianAssembler.

Definition at line 30 of file CentralDifferencesJacobianAssembler.cpp.

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 =
120 MathLib::toVector<Eigen::VectorXd>(_local_b_data, num_r_c);
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 }
154 return MathLib::createZeroedVector<Eigen::VectorXd>(local_b_data,
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}
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)

References _absolute_epsilons, _local_b_data, _local_K_data, _local_M_data, _local_x_perturbed_data, ProcessLib::LocalAssemblerInterface::assemble(), MathLib::createZeroedMatrix(), OGS_FATAL, and MathLib::toMatrix().

◆ copy()

std::unique_ptr< AbstractJacobianAssembler > ProcessLib::CentralDifferencesJacobianAssembler::copy ( ) const
overridevirtual

Implements ProcessLib::AbstractJacobianAssembler.

Definition at line 222 of file CentralDifferencesJacobianAssembler.cpp.

223{
224 return std::make_unique<CentralDifferencesJacobianAssembler>(*this);
225}

Member Data Documentation

◆ _absolute_epsilons

std::vector<double> const ProcessLib::CentralDifferencesJacobianAssembler::_absolute_epsilons
private

◆ _local_b_data

std::vector<double> ProcessLib::CentralDifferencesJacobianAssembler::_local_b_data
private

Definition at line 70 of file CentralDifferencesJacobianAssembler.h.

Referenced by assembleWithJacobian().

◆ _local_K_data

std::vector<double> ProcessLib::CentralDifferencesJacobianAssembler::_local_K_data
private

Definition at line 69 of file CentralDifferencesJacobianAssembler.h.

Referenced by assembleWithJacobian().

◆ _local_M_data

std::vector<double> ProcessLib::CentralDifferencesJacobianAssembler::_local_M_data
private

Definition at line 68 of file CentralDifferencesJacobianAssembler.h.

Referenced by assembleWithJacobian().

◆ _local_x_perturbed_data

std::vector<double> ProcessLib::CentralDifferencesJacobianAssembler::_local_x_perturbed_data
private

Definition at line 71 of file CentralDifferencesJacobianAssembler.h.

Referenced by assembleWithJacobian().


The documentation for this class was generated from the following files: