Coverage for pySDC/implementations/sweeper_classes/generic_implicit.py: 100%
49 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-02 12:30 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-02 12:30 +0000
1from pySDC.core.sweeper import Sweeper
4class generic_implicit(Sweeper):
5 """
6 Generic implicit sweeper, expecting lower triangular matrix type as input
8 Attributes:
9 QI: lower triangular matrix
10 """
12 def __init__(self, params, level):
13 """
14 Initialization routine for the custom sweeper
16 Args:
17 params: parameters for the sweeper
18 level (pySDC.Level.level): the level that uses this sweeper
19 """
21 if 'QI' not in params:
22 params['QI'] = 'IE'
24 super().__init__(params, level)
26 # get QI matrix
27 self.QI = self.get_Qdelta_implicit(qd_type=self.params.QI)
29 def integrate(self):
30 """
31 Integrates the right-hand side
33 Returns:
34 list of dtype_u: containing the integral as values
35 """
37 L = self.level
38 P = L.prob
40 me = []
42 # integrate RHS over all collocation nodes
43 for m in range(1, self.coll.num_nodes + 1):
44 # new instance of dtype_u, initialize values with 0
45 me.append(P.dtype_u(P.init, val=0.0))
46 for j in range(1, self.coll.num_nodes + 1):
47 me[-1] += L.dt * self.coll.Qmat[m, j] * L.f[j]
49 return me
51 def update_nodes(self):
52 """
53 Update the u- and f-values at the collocation nodes -> corresponds to a single sweep over all nodes
55 Returns:
56 None
57 """
59 L = self.level
60 P = L.prob
62 # only if the level has been touched before
63 assert L.status.unlocked
65 # get number of collocation nodes for easier access
66 M = self.coll.num_nodes
68 # the terms which are known already (from the previous iteration) are u0 + QF(u^k) - QdF(u^k) + tau, built
69 # with Q - Qd in one pass over F(u^k), kept here because the sweep replaces L.f[m]
70 F = L.f[1:]
71 diff_weights = self.coll.Qmat - self.QI
73 # do the sweep
74 for m in range(0, M):
75 # build rhs, consisting of the known values and new values from previous nodes (at k+1)
76 rhs = P.dtype_u(L.u[0])
77 for j in range(1, M + 1):
78 rhs += L.dt * diff_weights[m + 1, j] * F[j - 1]
79 if L.tau[m] is not None:
80 rhs += L.tau[m]
81 for j in range(1, m + 1):
82 rhs += L.dt * self.QI[m + 1, j] * L.f[j]
84 # implicit solve with prefactor stemming from the diagonal of Qd
85 alpha = L.dt * self.QI[m + 1, m + 1]
86 if alpha == 0:
87 L.u[m + 1] = rhs
88 else:
89 L.u[m + 1] = P.solve_system(rhs, alpha, L.u[m + 1], L.time + L.dt * self.coll.nodes[m])
90 # update function values
91 L.f[m + 1] = P.eval_f(L.u[m + 1], L.time + L.dt * self.coll.nodes[m])
93 # indicate presence of new values at this level
94 L.status.updated = True
96 return None
98 def compute_end_point(self):
99 """
100 Compute u at the right point of the interval
102 The value uend computed here is a full evaluation of the Picard formulation unless do_full_update==False
104 Returns:
105 None
106 """
108 L = self.level
109 P = L.prob
111 # check if Mth node is equal to right point and do_coll_update is false, perform a simple copy
112 if self.coll.right_is_node and not self.params.do_coll_update:
113 # a copy is sufficient
114 L.uend = P.dtype_u(L.u[-1])
115 else:
116 # start with u0 and add integral over the full interval (using coll.weights)
117 L.uend = P.dtype_u(L.u[0])
118 for m in range(self.coll.num_nodes):
119 L.uend += L.dt * self.coll.weights[m] * L.f[m + 1]
120 # add up tau correction of the full interval (last entry)
121 if L.tau[-1] is not None:
122 L.uend += L.tau[-1]
124 return None