|
18 | 18 |
|
19 | 19 |
|
20 | 20 | # ----------------------------------------------------------------------------- |
21 | | -# Tentative for a cleaner interface between pySDC and Dedalus |
| 21 | +# Tentative for a cleaner interface for pySDC |
22 | 22 | # ----------------------------------------------------------------------------- |
23 | | -class Tendencies(object): |
| 23 | +class FValues(object): |
24 | 24 |
|
25 | | - def __init__(self): |
26 | | - # TODO : constructor requirements ? |
27 | | - # -> Step.init_step : copy of initial tendency with u[0] = P.dtype_u(u0) |
28 | | - self.terms = [] |
| 25 | + def copy(self) -> "FValues": |
| 26 | + # TODO : return a copy of itself |
| 27 | + raise NotImplementedError |
29 | 28 |
|
30 | | - def __iadd__(self, f:"Tendencies") -> "Tendencies": |
| 29 | +class Tendency(object): |
| 30 | + |
| 31 | + def __iadd__(self, f:FValues) -> "Tendency": |
31 | 32 | # TODO : inplace addition with other full tendencies |
32 | | - pass |
| 33 | + raise NotImplementedError |
33 | 34 |
|
34 | | - def axpy(self, a:float|list[float], x:"Tendencies") -> "Tendencies": |
35 | | - if isinstance(a, float): |
| 35 | + def axpy(self, a:float, x:FValues|"Tendency") -> "Tendency": |
| 36 | + # Note : if a == 0, this should be a no-op ... or better, assert a != 0 |
| 37 | + if isinstance(x, FValues): |
36 | 38 | # TODO : y += a*x when x contains all tendencies |
37 | 39 | pass |
38 | | - if isinstance(a, list): |
39 | | - # TODO : y += a1*x1 + a2*x2 + ... when x1, x2 are each tendency |
40 | | - # Note : if some a[i] are zeros, it should be a no-op |
| 40 | + if isinstance(x, Tendency): |
| 41 | + # TODO : y += a*x when x is only one tendency |
41 | 42 | pass |
42 | | - raise ValueError("wrong type for a") |
| 43 | + raise ValueError("wrong type for x") |
43 | 44 |
|
44 | | - |
45 | | -class Solution(object): |
| 45 | +class UValues(object): |
46 | 46 |
|
47 | 47 | def __init__(self, init=None, val=0.0): |
48 | 48 | # TODO : constructor requirements ? |
49 | | - pass |
| 49 | + # -> Step.init_step : copy of initial tendency with u[0] = P.dtype_u(u0) |
| 50 | + if isinstance(init, UValues): |
| 51 | + pass |
| 52 | + else: |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | + def setValues(self, other:"UValues"): |
| 57 | + # TODO : copy other values into current instance |
| 58 | + raise NotImplementedError |
| 59 | + |
| 60 | + def toTendency(self) -> Tendency: |
| 61 | + """Convert into a Tendency instance""" |
| 62 | + raise NotImplementedError |
| 63 | + |
50 | 64 |
|
51 | | -class DProblem(Problem): |
52 | 65 |
|
53 | | - dtype_u = Solution |
54 | | - dtype_f = Tendencies |
| 66 | +class BaseProblem(Problem): |
55 | 67 |
|
56 | | - def eval_f(self, u:Solution, t:float, f:Tendencies) -> Tendencies: |
57 | | - # TODO : inplace modify f with the tendencies evaluation |
| 68 | + dtype_u = UValues |
| 69 | + dtype_f = FValues |
| 70 | + |
| 71 | + def eval_f(self, u:UValues, t:float, f:FValues) -> FValues: |
| 72 | + # TODO : inplace evaluate f(u, t) |
| 73 | + raise NotImplementedError |
| 74 | + |
| 75 | + def solve_system(self, rhs:Tendency, dt:float, u:UValues, t:float) -> UValues: |
| 76 | + # TODO : inplace modify u with the system solve |
| 77 | + # u + dt*f_I(u, t) = rhs |
| 78 | + # using u as initial solution for an eventual iterative solver |
| 79 | + raise NotImplementedError |
| 80 | + |
| 81 | + def apply_mass_matrix(self, u:UValues) -> Tendency: |
| 82 | + """Apply eventual mass matrix on u to produce a tendency (default: no mass matrix)""" |
| 83 | + return u.toTendency() |
| 84 | + |
| 85 | +# ----------------------------------------------------------------------------- |
| 86 | +# Dedalus specific implementation |
| 87 | +# ----------------------------------------------------------------------------- |
| 88 | +class DedalusTendency(Tendency): |
| 89 | + |
| 90 | + def __init__(self): |
| 91 | + self.vals:CoeffSystem = None |
| 92 | + |
| 93 | + def __iadd__(self, f: "DedalusTendency"): |
| 94 | + self.vals.data += f.vals.data |
| 95 | + return self |
| 96 | + |
| 97 | + def axpy(self, a:float, x:"DedalusTendency"|"DedalusFValues") -> "DedalusTendency": |
| 98 | + if isinstance(x, DedalusTendency): |
| 99 | + self.vals.data += a*x.vals.data |
| 100 | + if isinstance(x, DedalusFValues): |
| 101 | + self.vals.data += a*(x.impl.vals.data + x.expl.vals.data) |
| 102 | + |
| 103 | + |
| 104 | +class DedalusFValues(FValues): |
| 105 | + |
| 106 | + def __init__(self): |
| 107 | + self.impl:DedalusTendency = None |
| 108 | + self.expl:DedalusTendency = None |
| 109 | + |
| 110 | + |
| 111 | +class DedalProblem(BaseProblem): |
| 112 | + |
| 113 | + dtype_u = UValues |
| 114 | + dtype_f = FValues |
| 115 | + |
| 116 | + def eval_f(self, u:UValues, t:float) -> FValues: |
| 117 | + # TODO : evaluate f(u, t) |
58 | 118 | pass |
59 | 119 |
|
60 | | - def solve_system(self, rhs:Tendencies, dt:float, u:Solution, t:float) -> Solution: |
| 120 | + def solve_system(self, rhs:Tendency, dt:float, u:UValues, t:float) -> UValues: |
61 | 121 | # TODO : inplace modify u with the system solve |
62 | 122 | # u + dt*f_I(u, t) = rhs |
63 | 123 | # using u as initial solution for an eventual iterative solver |
64 | 124 | pass |
65 | 125 |
|
66 | | - def apply_mass_matrix(self, u:Solution, rhs:Tendencies) -> Tendencies: |
| 126 | + def apply_mass_matrix(self, u:UValues, rhs:Tendency) -> Tendency: |
67 | 127 | # TODO : inplace evaluation in rhs of mass-matrix multiplication |
68 | 128 | pass |
69 | 129 |
|
70 | | - |
71 | 130 | # ----------------------------------------------------------------------------- |
72 | 131 | # First interface |
73 | 132 | # ----------------------------------------------------------------------------- |
|
0 commit comments