OGS
TimeDiscretizedODESystem.cpp
Go to the documentation of this file.
1
10
12
13#include <range/v3/numeric/accumulate.hpp>
14#include <range/v3/view/transform.hpp>
15
18#include "NumLib/Exceptions.h"
20
21namespace detail
22{
24template <typename Solutions, typename Vector>
25void applyKnownSolutions(std::vector<Solutions> const* const known_solutions,
26 Vector& x)
27{
28 if (!known_solutions)
29 {
30 return;
31 }
32
33 for (auto const& bc : *known_solutions)
34 {
35 for (std::size_t i = 0; i < bc.ids.size(); ++i)
36 {
37 // TODO that might have bad performance for some Vector types, e.g.,
38 // PETSc.
39 MathLib::setVector(x, bc.ids[i], bc.values[i]);
40 }
41 }
43}
44} // namespace detail
45
46namespace NumLib
47{
50 TimeDiscretizedODESystem(const int process_id, ODE& ode,
51 TimeDisc& time_discretization)
52 : _ode(ode),
53 _time_disc(time_discretization),
54 _mat_trans(createMatrixTranslator<ODETag>(time_discretization))
55{
57 _ode.getMatrixSpecifications(process_id), _Jac_id);
59 _ode.getMatrixSpecifications(process_id), _b_id);
60}
61
69
72 assemble(std::vector<GlobalVector*> const& x_new_timestep,
73 std::vector<GlobalVector*> const& x_prev,
74 int const process_id)
75{
76 auto const t = _time_disc.getCurrentTime();
77 auto const dt = _time_disc.getCurrentTimeIncrement();
78 auto const& x_curr = *x_new_timestep[process_id];
79
80 _b->setZero();
81 _Jac->setZero();
82 try
83 {
84 _ode.preAssemble(t, dt, x_curr);
85 _ode.assembleWithJacobian(t, dt, x_new_timestep, x_prev, process_id,
86 *_b, *_Jac);
87 }
88 catch (AssemblyException const&)
89 {
92 throw;
93 }
96}
97
100 getResidual(GlobalVector const& /*x_new_timestep*/,
101 GlobalVector const& /*x_prev*/,
102 GlobalVector& res) const
103{
104 MathLib::LinAlg::copy(*_b, res); // res = b
105 MathLib::LinAlg::scale(res, -1.); // res = -b
106}
107
111{
112 _mat_trans->computeJacobian(*_Jac, Jac);
113}
114
118 int const process_id)
119{
121 _ode.getKnownSolutions(_time_disc.getCurrentTime(), x, process_id);
122}
123
130
133 applyKnownSolutionsNewton(GlobalMatrix& Jac, GlobalVector& res,
134 GlobalVector const& x,
135 GlobalVector& minus_delta_x) const
136{
137 if (!_known_solutions)
138 {
139 return;
140 }
141
143 std::size_t const size = ranges::accumulate(
144 *_known_solutions | ranges::views::transform([](auto const& bc)
145 { return bc.ids.size(); }),
146 0);
147 std::vector<IndexType> ids;
148 ids.reserve(size);
149 std::vector<double> values;
150 values.reserve(size);
151
152 for (auto const& bc : *_known_solutions)
153 {
154 for (std::size_t i = 0; i < bc.ids.size(); ++i)
155 {
156 auto const id = bc.ids[i];
157 ids.push_back(id);
158 // minus_delta_x will be set to the difference between the current
159 // value and the Dirichlet BC value.
160 values.push_back(x[id] - bc.values[i]);
161 }
162 }
163
165 Jac, res, minus_delta_x, ids, values,
167}
168
171 applyKnownSolutionsPETScSNES(GlobalMatrix& Jac, GlobalVector& res,
172 GlobalVector& x) const
173{
174 if (!_known_solutions)
175 {
176 return;
177 }
178
180 std::vector<IndexType> ids;
181 for (auto const& bc : *_known_solutions)
182 {
183 ids.insert(end(ids), begin(bc.ids), end(bc.ids));
184 }
185
186 // For the Newton method the values must be zero
187 std::vector<double> values(ids.size(), 0);
189 Jac, res, x, ids, values,
191}
192
195 TimeDiscretizedODESystem(const int process_id, ODE& ode,
196 TimeDisc& time_discretization)
197 : _ode(ode),
198 _time_disc(time_discretization),
199 _mat_trans(createMatrixTranslator<ODETag>(time_discretization))
200{
202 ode.getMatrixSpecifications(process_id), _M_id);
204 ode.getMatrixSpecifications(process_id), _K_id);
206 ode.getMatrixSpecifications(process_id), _b_id);
207}
208
217
220 assemble(std::vector<GlobalVector*> const& x_new_timestep,
221 std::vector<GlobalVector*> const& x_prev,
222 int const process_id)
223{
224 namespace LinAlg = MathLib::LinAlg;
225
226 auto const t = _time_disc.getCurrentTime();
227 auto const dt = _time_disc.getCurrentTimeIncrement();
228 auto const& x_curr = *x_new_timestep[process_id];
229
230 _M->setZero();
231 _K->setZero();
232 _b->setZero();
233
234 _ode.preAssemble(t, dt, x_curr);
235 _ode.assemble(t, dt, x_new_timestep, x_prev, process_id, *_M, *_K, *_b);
236
240}
241
245 int const process_id)
246{
248 _ode.getKnownSolutions(_time_disc.getCurrentTime(), x, process_id);
249}
250
257
260 applyKnownSolutionsPicard(
263{
264 if (!_known_solutions)
265 {
266 return;
267 }
268
270 std::vector<IndexType> ids;
271 std::vector<double> values;
272 for (auto const& bc : *_known_solutions)
273 {
274 ids.insert(end(ids), begin(bc.ids), end(bc.ids));
275 values.insert(end(values), begin(bc.values), end(bc.values));
276 }
277 MathLib::applyKnownSolution(A, rhs, x, ids, values, mode);
278}
279
280} // namespace NumLib
MathLib::EigenMatrix GlobalMatrix
MathLib::EigenVector GlobalVector
std::unique_ptr< MatTrans > _mat_trans
the object used to compute the matrix/vector for the nonlinear solver
std::vector< NumLib::IndexValueVector< Index > > const * _known_solutions
stores precomputed values for known solutions
std::vector< NumLib::IndexValueVector< Index > > const * _known_solutions
stores precomputed values for known solutions
std::unique_ptr< MatTrans > _mat_trans
the object used to compute the matrix/vector for the nonlinear solver
std::unique_ptr< MatrixTranslator< ODETag > > createMatrixTranslator(TimeDiscretization const &timeDisc)
@ FirstOrderImplicitQuasilinear
Definition Types.h:34
void finalizeAssembly(PETScMatrix &A)
Definition LinAlg.cpp:198
void copy(PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:37
void scale(PETScVector &x, PetscScalar const a)
Definition LinAlg.cpp:44
DirichletBCApplicationMode
Definition LinAlgEnums.h:42
@ COMPLETE_MATRIX_UPDATE
Both A and b fully updated.
Definition LinAlgEnums.h:43
void setVector(PETScVector &v, std::initializer_list< double > values)
void applyKnownSolution(EigenMatrix &A, EigenVector &b, EigenVector &, const std::vector< EigenMatrix::IndexType > &vec_knownX_id, const std::vector< double > &vec_knownX_x, DirichletBCApplicationMode const mode)
void applyKnownSolutions(std::vector< Solutions > const *const known_solutions, Vector &x)
Applies known solutions to the solution vector x.
static NUMLIB_EXPORT MatrixProvider & provider
static NUMLIB_EXPORT VectorProvider & provider