svMultiPhysics
Loading...
Searching...
No Matches
Integrator.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef INTEGRATOR_H
5#define INTEGRATOR_H
6
7#include "Array.h"
8#include "SolutionStates.h"
9#include "Vector.h"
10#include "Simulation.h"
11
12/**
13 * @brief Integrator class that encapsulates the Newton iteration loop for time
14 * integration.
15 *
16 * This class handles the nonlinear Newton iteration scheme for solving coupled
17 * multi-physics equations in svMultiPhysics. It manages:
18 * - Solution variables (Ag, Yg, Dg) at generalized-alpha time levels
19 * - Newton iteration loop with convergence checking
20 * - Linear system assembly and solve
21 * - Boundary condition application
22 *
23 * ## Line search
24 *
25 * The nonlinear iterations of an equation can be globalized by a backtracking
26 * line search, enabled by the `Line_search_experimental` XML element of the
27 * equation (see @ref LineSearchParameters). Without it, every iteration applies
28 * the full increment of the linear solve.
29 *
30 * At every nonlinear iteration of an equation with line search enabled, the
31 * increment @f$\delta u@f$ of the linear solve is scaled by a step length
32 * @f$\alpha@f$ before being applied, so that the solution is updated to
33 * @f$u + \alpha\,\delta u@f$. The step lengths @f$\alpha = 1, \rho, \rho^2,
34 * \dots@f$ are tried in turn, with @f$\rho@f$ the step reduction factor, and
35 * the first one satisfying
36 * @f[
37 * \| R(u + \alpha\,\delta u) \|_W \leq (1 - c\,\alpha)\,\| R(u) \|_W
38 * @f]
39 * is applied, with @f$c@f$ the minimum decrease. Once the step length reaches
40 * the minimum step length, it is applied regardless of the residual decrease.
41 *
42 * The norm @f$\|\cdot\|_W@f$ weights every residual entry by the inverse
43 * square root of the magnitude of the corresponding diagonal entry of the
44 * tangent at @f$u@f$, as the Jacobi preconditioner of the linear solver does,
45 * so that it is dimensionally homogeneous across the degrees of freedom of the
46 * equation. Degrees of freedom constrained by a Dirichlet condition have zero
47 * weight, since their residual is the reaction of the constraint. The weights
48 * are computed once per iteration and kept fixed while step lengths are tried,
49 * so that the norms of different step lengths are comparable (see
50 * compute_residual_weights).
51 *
52 * The linear system is solved once per iteration, whatever the number of step
53 * lengths tried. Measuring a step length costs one assembly of the linear
54 * system at the trial solution, which re-advances implicitly coupled active
55 * stress models as any other assembly does. The convergence test that follows
56 * (see finalize_iteration) is the same with or without line search.
57 *
58 * Line search is not implemented for prestress simulations: the nodal
59 * prestress is accumulated over the assemblies of a time step, which would
60 * include those of the trial step lengths unless they were excluded.
61 *
62 * @warning While it is implemented for all equations, line search is currently
63 * only tested for struct, should be regarded as experimental, and used with
64 * caution.
65 */
67
68public:
69 /**
70 * @brief Construct a new Integrator object
71 *
72 * @param simulation Pointer to the Simulation object containing problem data
73 * @param solutions Solution states containing old time level arrays (takes ownership via move)
74 */
75 Integrator(Simulation* simulation, SolutionStates&& solutions);
76
77 /**
78 * @brief Execute one time step with Newton iteration loop
79 *
80 * Performs the complete Newton iteration sequence including initialization,
81 * assembly, boundary condition application, linear solve, and convergence check.
82 * One line is written to the standard output and to the history file for every
83 * Newton iteration, including the converged one.
84 *
85 * @param[in] save_results True if the results of this time step are written to a
86 * VTU file, which flags the converged iteration with an 's' in the standard
87 * output.
88 *
89 * @return True if all equations converged, false otherwise
90 */
91 bool step(bool save_results = false);
92
93 /**
94 * @brief Perform predictor step for next time step
95 *
96 * Performs predictor step using generalized-alpha method to estimate
97 * solution at n+1 time level based on current solution at n time level.
98 * This should be called once per time step before the Newton iteration loop.
99 */
100 void predictor();
101
102 /**
103 * @brief Get reference to solution variable Ag (time derivative of variables)
104 *
105 * @return Reference to Ag array (acceleration in structural mechanics)
106 */
107 Array<double>& get_Ag() { return solutions_.intermediate.get_acceleration(); }
108 const Array<double>& get_Ag() const { return solutions_.intermediate.get_acceleration(); }
109
110 /**
111 * @brief Get reference to solution variable Yg (variables)
112 *
113 * @return Reference to Yg array (velocity in structural mechanics)
114 */
115 Array<double>& get_Yg() { return solutions_.intermediate.get_velocity(); }
116 const Array<double>& get_Yg() const { return solutions_.intermediate.get_velocity(); }
117
118 /**
119 * @brief Get reference to solution variable Dg (integrated variables)
120 *
121 * @return Reference to Dg array (displacement in structural mechanics)
122 */
123 Array<double>& get_Dg() { return solutions_.intermediate.get_displacement(); }
124 const Array<double>& get_Dg() const { return solutions_.intermediate.get_displacement(); }
125
126 /**
127 * @brief Get reference to solution states struct
128 *
129 * Provides access to all solution arrays at old (n) and current (n+1) time levels.
130 * Use this to access An, Dn, Yn (current) and Ao, Do, Yo (old) via:
131 * auto& solutions = integrator.get_solutions();
132 * auto& An = solutions.current.get_acceleration();
133 * auto& Do = solutions.old.get_displacement();
134 *
135 * @return Reference to SolutionStates struct containing all solution arrays
136 */
137 SolutionStates& get_solutions() { return solutions_; }
138 const SolutionStates& get_solutions() const { return solutions_; }
139
140private:
141 /** @brief Pointer to the simulation object */
142 Simulation* simulation_;
143
144 /** @brief Solution states at old, current, and intermediate time levels */
145 SolutionStates solutions_;
146
147 /** @brief Residual vector for face-based quantities */
148 Vector<double> res_;
149
150 /** @brief Increment flag for faces in linear solver */
151 Vector<int> incL_;
152
153 /** @brief Newton iteration counter for current time step */
154 int newton_count_;
155
156 /** @brief Debug output suffix string combining time step and iteration number */
157 std::string istr_;
158
159 /**
160 * @brief Initialize solution arrays for Ag, Yg, Dg based on problem size
161 */
162 void initialize_arrays();
163
164 /**
165 * @brief Perform initiator step for Generalized-alpha Method
166 *
167 * Computes quantities at intermediate time levels (n+alpha_m, n+alpha_f)
168 */
169 void initiator_step();
170
171 /**
172 * @brief Allocate right-hand side (RHS) and left-hand side (LHS) arrays
173 *
174 * @param eq Reference to the equation being solved
175 */
176 void allocate_linear_system(eqType& eq);
177
178 /**
179 * @brief Set body forces for the current time step
180 */
181 void set_body_forces();
182
183 /**
184 * @brief Assemble global equations for all meshes
185 */
186 void assemble_equations();
187
188 /**
189 * @brief Apply all boundary conditions (Neumann, Dirichlet, CMM, contact, etc.)
190 */
191 void apply_boundary_conditions();
192
193 /**
194 * @brief Solve the assembled linear system
195 */
196 void solve_linear_system();
197
198 /**
199 * @brief Search for a step length that decreases the residual.
200 *
201 * Measures the increment of the linear solve at decreasing step lengths,
202 * starting from one, until the residual assembled at the resulting solution
203 * falls below \p reference_norm by the margin the settings of the equation
204 * ask for, or until the shortest step length is reached, which is returned
205 * without being measured. Measuring a step length costs one assembly of the
206 * linear system, while the increment itself is reused, so the linear system
207 * is solved once per nonlinear iteration however many step lengths are
208 * tried.
209 *
210 * The solution and the increment are left as they were found, so that the
211 * caller can apply the returned step length to them.
212 *
213 * @param[in,out] eq Equation whose increment is measured, and whose settings
214 * configure the search.
215 * @param[in] weights Weight of every residual entry in the norm, as computed
216 * by compute_residual_weights at the solution the search starts from.
217 * @param[in] reference_norm Norm of the residual at the solution the search
218 * starts from, measured with \p weights.
219 *
220 * @return Step length to apply to the increment.
221 */
222 double line_search_step_length(eqType &eq, const Array<double> &weights,
223 const double reference_norm);
224
225 /**
226 * @brief Update residual and increment arrays for linear solver
227 *
228 * @param eq Reference to the equation being solved
229 */
230 void update_residual_arrays(eqType& eq);
231
232 /**
233 * @brief Assemble the linear system of an equation
234 *
235 * Evaluates the boundary conditions of the equation, forms the solution at
236 * the intermediate generalized-alpha time levels, and assembles the residual
237 * into com_mod.R and the tangent into com_mod.Val, including the
238 * contributions of the coupled surfaces.
239 *
240 * @param[in,out] eq Equation whose linear system is assembled, and whose
241 * active stress models are re-advanced when their state is coupled
242 * implicitly.
243 */
244 void assemble_linear_system(eqType &eq);
245
246 /**
247 * @brief Compute the weight of every residual entry in the residual norm.
248 *
249 * The weight of a degree of freedom is the inverse square root of the
250 * magnitude of the corresponding diagonal entry of the tangent, assembled
251 * across processes, which is what the Jacobi preconditioner of the linear
252 * solver scales the rows of the linear system by. The weight of a degree of
253 * freedom constrained by a Dirichlet condition is zero, so that the residual
254 * norm ignores the rows the linear solve eliminates, whose residual is a
255 * reaction rather than an equation left to be satisfied.
256 *
257 * The resulting norm is dimensionally homogeneous across the degrees of
258 * freedom of an equation, which a norm of the residual entries themselves is
259 * not.
260 *
261 * The weights are read from the tangent assembled last, which the assembly
262 * of a further solution overwrites, so a line search computes them once at
263 * the solution it starts from and reuses them for every step length it
264 * tries. Keeping them fixed is what makes the norm a function of the
265 * solution alone, and so makes the norms of two step lengths comparable.
266 *
267 * @param[out] weights Weight of every degree of freedom of every node, in
268 * the node numbering of the linear solver.
269 */
270 void compute_residual_weights(Array<double> &weights) const;
271
272 /**
273 * @brief Compute the weighted norm of the assembled residual
274 *
275 * Sums the squares of the weighted entries of com_mod.R belonging to the
276 * nodes owned by each process and reduces them across processes, so that the
277 * result does not depend on the partitioning. It is available as soon as the
278 * residual has been assembled, before the linear system is solved.
279 *
280 * com_mod.R holds the residual of the equation assembled last, and its rows
281 * are the degrees of freedom of that equation, numbered from zero.
282 *
283 * @param[in] weights Weight of every degree of freedom of every node, as
284 * computed by compute_residual_weights.
285 *
286 * @return Weighted Euclidean norm of the assembled residual.
287 */
288 double residual_norm(const Array<double> &weights) const;
289
290 /**
291 * @brief Compute the fiber stretch and fiber stretch rate at every node.
292 *
293 * The stretch is computed from the current displacement, so that calling this
294 * function within the nonlinear iterations yields the stretch of the current
295 * displacement iterate. Both vectors are left empty if no equation needs
296 * them, and are filled with the neutral values (1 for the stretch, 0 for the
297 * stretch rate) if they are needed but no equation solves for the
298 * displacement.
299 *
300 * @param[out] fiber_stretch Fiber stretch at every node.
301 * @param[out] fiber_stretch_rate Fiber stretch rate at every node.
302 */
303 void compute_fiber_stretch(Vector<double>& fiber_stretch,
304 Vector<double>& fiber_stretch_rate);
305
306 /**
307 * @brief Whether any domain uses an active stress model with implicit state
308 * coupling, i.e. one whose state is updated within the nonlinear iterations.
309 *
310 * @return True if at least one such domain exists, false otherwise.
311 */
312 bool has_implicit_active_stress_state_coupling() const;
313
314 /**
315 * @brief Advance active stress models of an equation to the next time step.
316 */
317 void time_advance_active_stress(eqType &eq);
318
319 /**
320 * @brief Update the active stress models of an equation.
321 *
322 * @param[in,out] eq Equation whose domains carry the active stress models.
323 * @param[in] fiber_stretch Fiber stretch at every node.
324 * @param[in] fiber_stretch_rate Fiber stretch rate at every node.
325 * @param[in] within_nonlinear_iterations True when called within the
326 * nonlinear iterations, in which case only the models with implicit
327 * coupling are advanced again, from the state stored at the beginning of
328 * the time step. False when called once per time step from the predictor,
329 * in which case all models are advanced from that state.
330 */
331 void update_active_stress(eqType &eq, const Vector<double> &fiber_stretch,
332 const Vector<double> &fiber_stretch_rate,
333 const bool within_nonlinear_iterations);
334
335 /**
336 * @brief Initiator function for generalized-alpha method (initiator)
337 *
338 * Computes solution variables at intermediate time levels using
339 * generalized-alpha parameters (am, af) for time integration.
340 * Updates solutions.intermediate (Ag, Yg, Dg) based on solutions.current
341 * (An, Yn, Dn) and solutions.old (Ao, Yo, Do).
342 *
343 * @param solutions Solution states containing old, current, and intermediate levels
344 */
345 void initiator(SolutionStates& solutions);
346
347 /**
348 * @brief Apply a multiple of the increment of the linear solve to the
349 * solution
350 *
351 * Advances the solution of an equation at the n+1 time level by @c
352 * step_length times the increment held in com_mod.R (and com_mod.Rd for the
353 * velocity-based formulation), and updates the quantities that are derived
354 * from the resulting solution: the Taylor-Hood pressure at edge nodes, the
355 * copy of the solution onto the solid subdomain of an FSI equation, the
356 * ionic state of an electrophysiology equation, and the wall filter of a CMM
357 * equation.
358 *
359 * The solution change is proportional to step_length, so calling this
360 * function from the same starting solution with different step lengths traces
361 * the segment between that solution and the one the full increment produces.
362 * Every quantity derived from the solution is recomputed, so any step length
363 * leaves a consistent state.
364 *
365 * Modifies:
366 * \code {.cpp}
367 * com_mod.Ad
368 * solutions_.current.A
369 * solutions_.current.D
370 * solutions_.current.Y
371 * cep_mod.Xion
372 * \endcode
373 *
374 * @param[in] eq Equation whose solution is advanced, which selects the
375 * degrees of freedom the increment is applied to and the coefficients of
376 * the time integration scheme.
377 * @param[in] step_length Step length multiplying the increment. One applies
378 * the full increment of the linear solve.
379 */
380 void apply_increment(const eqType &eq, const double step_length);
381
382 /**
383 * @brief Close a nonlinear iteration of the current equation
384 *
385 * Normalizes the nodal prestress accumulated during the assembly, tests the
386 * residual norm of the current equation for convergence, and selects the
387 * equation to be solved by the next iteration.
388 *
389 * The nodal quantities this function reduces are accumulated once per
390 * assembly and its convergence bookkeeping counts one iteration, so it is
391 * called once per iteration, after the solution has been advanced by
392 * apply_increment.
393 *
394 * Modifies:
395 * \code {.cpp}
396 * com_mod.pSa
397 * com_mod.pSn
398 * solutions_.current.A
399 * solutions_.current.D
400 * solutions_.current.Y
401 *
402 * com_mod.cEq
403 * eq.FSILS.RI.iNorm
404 * eq.iNorm
405 * eq.ok
406 * eq.pNorm
407 * \endcode
408 */
409 void finalize_iteration();
410
411 /**
412 * @brief Pressure correction for Taylor-Hood elements (corrector_taylor_hood)
413 *
414 * Interpolates pressure at edge nodes using reduced basis applied
415 * on element vertices for Taylor-Hood type elements.
416 */
417 void corrector_taylor_hood();
418};
419
420#endif // INTEGRATOR_H
Integrator(Simulation *simulation, SolutionStates &&solutions)
Construct a new Integrator object.
Definition Integrator.cpp:32
bool step(bool save_results=false)
Execute one time step with Newton iteration loop.
Definition Integrator.cpp:63
Array< double > & get_Dg()
Get reference to solution variable Dg (integrated variables).
Definition Integrator.h:123
SolutionStates & get_solutions()
Get reference to solution states struct.
Definition Integrator.h:137
Array< double > & get_Yg()
Get reference to solution variable Yg (variables).
Definition Integrator.h:115
Array< double > & get_Ag()
Get reference to solution variable Ag (time derivative of variables).
Definition Integrator.h:107
void predictor()
Perform predictor step for next time step.
Definition Integrator.cpp:721
Definition Simulation.h:19
Holds solution state at old, current, and intermediate time levels.
Definition SolutionStates.h:39
Solution intermediate
Generalized-alpha intermediate level (Ag, Yg, Dg).
Definition SolutionStates.h:42