File: vanderpol.py

package info (click to toggle)
petsc4py 3.23.1-1exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,448 kB
  • sloc: python: 12,503; ansic: 1,697; makefile: 343; f90: 313; sh: 14
file content (286 lines) | stat: -rw-r--r-- 7,226 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Testing TSAdjoint and matrix-free Jacobian
# Basic usage:
#     python vanderpol.py
# Test implicit methods using implicit form:
#     python -implicitform
# Test explicit methods:
#     python -implicitform 0
# Test IMEX methods:
#     python -imexform
# Matrix-free implementations can be enabled with an additional option -mf

import sys
import petsc4py

petsc4py.init(sys.argv)

from petsc4py import PETSc


class VDP:
    n = 2
    comm = PETSc.COMM_SELF

    def __init__(self, mu_=1.0e3, mf_=False, imex_=False):
        self.mu_ = mu_
        self.mf_ = mf_
        self.imex_ = imex_
        if self.mf_:
            self.Jim_ = PETSc.Mat().createDense([self.n, self.n], comm=self.comm)
            self.Jim_.setUp()
            self.JimP_ = PETSc.Mat().createDense([self.n, 1], comm=self.comm)
            self.JimP_.setUp()
            self.Jex_ = PETSc.Mat().createDense([self.n, self.n], comm=self.comm)
            self.Jex_.setUp()
            self.JexP_ = PETSc.Mat().createDense([self.n, 1], comm=self.comm)
            self.JexP_.setUp()

    def initialCondition(self, u):
        mu = self.mu_
        u[0] = 2.0
        u[1] = -2.0 / 3.0 + 10.0 / (81.0 * mu) - 292.0 / (2187.0 * mu * mu)
        u.assemble()

    def evalFunction(self, ts, t, u, f):
        mu = self.mu_
        f[0] = u[1]
        if self.imex_:
            f[1] = 0.0
        else:
            f[1] = mu * ((1.0 - u[0] * u[0]) * u[1] - u[0])
        f.assemble()

    def evalJacobian(self, ts, t, u, A, B):
        if not self.mf_:
            J = A
        else:
            J = self.Jex_
        mu = self.mu_
        J[0, 0] = 0
        J[0, 1] = 1.0
        if self.imex_:
            J[1, 0] = 0
            J[1, 1] = 0
        else:
            J[1, 0] = -mu * (2.0 * u[1] * u[0] + 1.0)
            J[1, 1] = mu * (1.0 - u[0] * u[0])
        J.assemble()
        if A != B:
            B.assemble()

    def evalJacobianP(self, ts, t, u, C):
        if not self.mf_:
            Jp = C
        else:
            Jp = self.JexP_
        if not self.imex_:
            Jp[0, 0] = 0
            Jp[1, 0] = (1.0 - u[0] * u[0]) * u[1] - u[0]
            Jp.assemble()

    def evalIFunction(self, ts, t, u, udot, f):
        mu = self.mu_
        if self.imex_:
            f[0] = udot[0]
        else:
            f[0] = udot[0] - u[1]
        f[1] = udot[1] - mu * ((1.0 - u[0] * u[0]) * u[1] - u[0])
        f.assemble()

    def evalIJacobian(self, ts, t, u, udot, shift, A, B):
        if not self.mf_:
            J = A
        else:
            J = self.Jim_
        mu = self.mu_
        if self.imex_:
            J[0, 0] = shift
            J[0, 1] = 0.0
        else:
            J[0, 0] = shift
            J[0, 1] = -1.0
        J[1, 0] = mu * (2.0 * u[1] * u[0] + 1.0)
        J[1, 1] = shift - mu * (1.0 - u[0] * u[0])
        J.assemble()
        if A != B:
            B.assemble()

    def evalIJacobianP(self, ts, t, u, udot, shift, C):
        if not self.mf_:
            Jp = C
        else:
            Jp = self.JimP_
        Jp[0, 0] = 0
        Jp[1, 0] = u[0] - (1.0 - u[0] * u[0]) * u[1]
        Jp.assemble()


class JacShell:
    def __init__(self, ode):
        self.ode_ = ode

    def mult(self, A, x, y):
        "y <- A * x"
        self.ode_.Jex_.mult(x, y)

    def multTranspose(self, A, x, y):
        "y <- A' * x"
        self.ode_.Jex_.multTranspose(x, y)


class JacPShell:
    def __init__(self, ode):
        self.ode_ = ode

    def multTranspose(self, A, x, y):
        "y <- A' * x"
        self.ode_.JexP_.multTranspose(x, y)


class IJacShell:
    def __init__(self, ode):
        self.ode_ = ode

    def mult(self, A, x, y):
        "y <- A * x"
        self.ode_.Jim_.mult(x, y)

    def multTranspose(self, A, x, y):
        "y <- A' * x"
        self.ode_.Jim_.multTranspose(x, y)


class IJacPShell:
    def __init__(self, ode):
        self.ode_ = ode

    def multTranspose(self, A, x, y):
        "y <- A' * x"
        self.ode_.JimP_.multTranspose(x, y)


OptDB = PETSc.Options()

mu_ = OptDB.getScalar('mu', 1.0e3)
mf_ = OptDB.getBool('mf', False)

implicitform_ = OptDB.getBool('implicitform', False)
imexform_ = OptDB.getBool('imexform', False)

ode = VDP(mu_, mf_, imexform_)

if not mf_:
    Jim = PETSc.Mat().createDense([ode.n, ode.n], comm=ode.comm)
    Jim.setUp()
    JimP = PETSc.Mat().createDense([ode.n, 1], comm=ode.comm)
    JimP.setUp()
    Jex = PETSc.Mat().createDense([ode.n, ode.n], comm=ode.comm)
    Jex.setUp()
    JexP = PETSc.Mat().createDense([ode.n, 1], comm=ode.comm)
    JexP.setUp()
else:
    Jim = PETSc.Mat().create()
    Jim.setSizes([ode.n, ode.n])
    Jim.setType('python')
    shell = IJacShell(ode)
    Jim.setPythonContext(shell)
    Jim.setUp()
    Jim.assemble()
    JimP = PETSc.Mat().create()
    JimP.setSizes([ode.n, 1])
    JimP.setType('python')
    shell = IJacPShell(ode)
    JimP.setPythonContext(shell)
    JimP.setUp()
    JimP.assemble()
    Jex = PETSc.Mat().create()
    Jex.setSizes([ode.n, ode.n])
    Jex.setType('python')
    shell = JacShell(ode)
    Jex.setPythonContext(shell)
    Jex.setUp()
    Jex.assemble()
    JexP = PETSc.Mat().create()
    JexP.setSizes([ode.n, 1])
    JexP.setType('python')
    shell = JacPShell(ode)
    JexP.setPythonContext(shell)
    JexP.setUp()
    JexP.zeroEntries()
    JexP.assemble()

u = PETSc.Vec().createSeq(ode.n, comm=ode.comm)
f = u.duplicate()
adj_u = []
adj_u.append(PETSc.Vec().createSeq(ode.n, comm=ode.comm))
adj_u.append(PETSc.Vec().createSeq(ode.n, comm=ode.comm))
adj_p = []
adj_p.append(PETSc.Vec().createSeq(1, comm=ode.comm))
adj_p.append(PETSc.Vec().createSeq(1, comm=ode.comm))

ts = PETSc.TS().create(comm=ode.comm)
ts.setProblemType(ts.ProblemType.NONLINEAR)

if imexform_:
    ts.setType(ts.Type.ARKIMEX)
    ts.setIFunction(ode.evalIFunction, f)
    ts.setIJacobian(ode.evalIJacobian, Jim)
    ts.setIJacobianP(ode.evalIJacobianP, JimP)
    ts.setRHSFunction(ode.evalFunction, f)
    ts.setRHSJacobian(ode.evalJacobian, Jex)
    ts.setRHSJacobianP(ode.evalJacobianP, JexP)
else:
    if implicitform_:
        ts.setType(ts.Type.CN)
        ts.setIFunction(ode.evalIFunction, f)
        ts.setIJacobian(ode.evalIJacobian, Jim)
        ts.setIJacobianP(ode.evalIJacobianP, JimP)
    else:
        ts.setType(ts.Type.RK)
        ts.setRHSFunction(ode.evalFunction, f)
        ts.setRHSJacobian(ode.evalJacobian, Jex)
        ts.setRHSJacobianP(ode.evalJacobianP, JexP)

ts.setSaveTrajectory()
ts.setTime(0.0)
ts.setTimeStep(0.001)
ts.setMaxTime(0.5)
ts.setMaxSteps(1000)
ts.setExactFinalTime(PETSc.TS.ExactFinalTime.MATCHSTEP)

ts.setFromOptions()
ode.initialCondition(u)
ts.solve(u)

adj_u[0][0] = 1
adj_u[0][1] = 0
adj_u[0].assemble()
adj_u[1][0] = 0
adj_u[1][1] = 1
adj_u[1].assemble()
adj_p[0][0] = 0
adj_p[0].assemble()
adj_p[1][0] = 0
adj_p[1].assemble()

ts.setCostGradients(adj_u, adj_p)

ts.adjointSolve()

adj_u[0].view()
adj_u[1].view()
adj_p[0].view()
adj_p[1].view()


def compute_derp(du, dp):
    print(
        du[1] * (-10.0 / (81.0 * mu_ * mu_) + 2.0 * 292.0 / (2187.0 * mu_ * mu_ * mu_))
        + dp[0]
    )


compute_derp(adj_u[0], adj_p[0])
compute_derp(adj_u[1], adj_p[1])

del ode, Jim, JimP, Jex, JexP, u, f, ts, adj_u, adj_p