Skip to content

Commit 337d52c

Browse files
committed
Preliminary changes to add multiple gas species
1 parent 1d6cd41 commit 337d52c

File tree

2 files changed

+97
-96
lines changed

2 files changed

+97
-96
lines changed

dustpy/simulation.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def checkmassconservation(self):
315315
self.dust.coagulation.stick, self.dust.coagulation.stick_ind, self.grid.m)
316316
tup = (j, i)
317317
color = "red"
318-
if(errmax < erracc):
318+
if (errmax < erracc):
319319
color = "green"
320320
error = "{:9.2e}".format(errmax)
321321
msg = " max. rel. error: {:}\n".format(
@@ -341,7 +341,7 @@ def checkmassconservation(self):
341341
A, klf, m, phi)
342342
tup = (j, i)
343343
color = "red"
344-
if(errmax < erracc):
344+
if (errmax < erracc):
345345
color = "green"
346346
error = "{:9.2e}".format(errmax)
347347
msg = " max. rel. error: {:}\n".format(
@@ -361,7 +361,7 @@ def checkmassconservation(self):
361361
A, eps, klf, krm, m, phi)
362362
tup = (j, i)
363363
color = "red"
364-
if(errmax < erracc):
364+
if (errmax < erracc):
365365
color = "green"
366366
error = "{:9.2e}".format(errmax)
367367
msg = " max. rel. error: {:}\n".format(
@@ -412,8 +412,10 @@ def initialize(self):
412412
),
413413
Instruction(std.gas.impl_1_direct,
414414
self.gas.Sigma,
415-
controller={"rhs": self.gas._rhs
416-
},
415+
controller={
416+
"boundary": self.gas.boundary,
417+
"Sext": self.gas.S.ext,
418+
},
417419
description="Gas: implicit 1st-order direct solver"
418420
),
419421
]

dustpy/std/gas.py

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def Hp(sim):
173173
)
174174

175175

176-
def jacobian(sim, x, dx=None, *args, **kwargs):
176+
def jacobian(sim, x, *args, **kwargs):
177177
"""Functions calculates the Jacobian for the gas.
178178
179179
Parameters
@@ -182,8 +182,6 @@ def jacobian(sim, x, dx=None, *args, **kwargs):
182182
Parent simulation frame
183183
x : IntVar
184184
Integration variable
185-
dx : float, optional, default : None
186-
stepsize
187185
args : additional positional arguments
188186
kwargs : additional keyworda arguments
189187
@@ -204,10 +202,6 @@ def jacobian(sim, x, dx=None, *args, **kwargs):
204202
v = sim.dust.backreaction.B * 2. * sim.gas.eta * sim.grid.r * sim.grid.OmegaK
205203

206204
# Helper variables for convenience
207-
if dx is None:
208-
dt = x.stepsize
209-
else:
210-
dt = dx
211205
r = sim.grid.r
212206
ri = sim.grid.ri
213207
area = sim.grid.A
@@ -224,86 +218,13 @@ def jacobian(sim, x, dx=None, *args, **kwargs):
224218
# Right hand side
225219
sim.gas._rhs[:] = sim.gas.Sigma
226220

227-
# Boundaries
228-
B00, B01, B02 = 0., 0., 0.
229-
BNm1Nm3, BNm1Nm2, BNm1Nm1 = 0., 0., 0.
230-
231-
# Inner boundary
232-
if sim.gas.boundary.inner is not None:
233-
# Given value
234-
if sim.gas.boundary.inner.condition == "val":
235-
sim.gas._rhs[0] = sim.gas.boundary.inner.value
236-
# Constant value
237-
elif sim.gas.boundary.inner.condition == "const_val":
238-
B01 = (1./dt)[0]
239-
sim.gas._rhs[0] = 0.
240-
# Given gradient
241-
elif sim.gas.boundary.inner.condition == "grad":
242-
K1 = - r[1]/r[0]
243-
B01 = -(K1/dt)[0]
244-
sim.gas._rhs[0] = - ri[1]/r[0] * \
245-
(r[1]-r[0])*sim.gas.boundary.inner.value
246-
# Constant gradient
247-
elif sim.gas.boundary.inner.condition == "const_grad":
248-
Di = ri[1]/ri[2] * (r[1]-r[0]) / (r[2]-r[0])
249-
K1 = - r[1]/r[0] * (1. + Di)
250-
K2 = r[2]/r[0] * Di
251-
B00, B01, B02 = 0., -(K1/dt)[0], -(K2/dt)[0]
252-
sim.gas._rhs[0] = 0.
253-
# Given power law
254-
elif sim.gas.boundary.inner.condition == "pow":
255-
p = sim.gas.boundary.inner.value
256-
sim.gas._rhs[0] = sim.gas.Sigma[1] * (r[0]/r[1])**p
257-
# Constant power law
258-
elif sim.gas.boundary.inner.condition == "const_pow":
259-
p = np.log(sim.gas.Sigma[2] /
260-
sim.gas.Sigma[1]) / np.log(r[2]/r[1])
261-
K1 = - (r[0]/r[1])**p
262-
B01 = -(K1/dt)[0]
263-
sim.gas._rhs[0] = 0.
264-
221+
# Boundaries. This is only reserving space in the sparce matrix
265222
row_in = [0, 0, 0]
266223
col_in = [0, 1, 2]
267-
dat_in = [B00, B01, B02]
268-
269-
# Outer boundary
270-
if sim.gas.boundary.outer is not None:
271-
# Given value
272-
if sim.gas.boundary.outer.condition == "val":
273-
sim.gas._rhs[-1] = sim.gas.boundary.outer.value
274-
# Constant value
275-
elif sim.gas.boundary.outer.condition == "const_val":
276-
BNm1Nm2 = (1./dt)[0]
277-
sim.gas._rhs[-1] = 0.
278-
# Given gradient
279-
elif sim.gas.boundary.outer.condition == "grad":
280-
KNrm2 = - r[-2]/r[-1]
281-
BNm1Nm2 = -(KNrm2/dt)[0]
282-
sim.gas._rhs[-1] = ri[-2]/r[-1] * \
283-
(r[-1]-r[-2])*sim.gas.boundary.outer.value
284-
# Constant gradient
285-
elif sim.gas.boundary.outer.condition == "const_grad":
286-
Do = ri[-2]/ri[-3] * (r[-1]-r[-2]) / (r[-2]-r[-3])
287-
KNrm2 = - r[-2]/r[-1] * (1. + Do)
288-
KNrm3 = r[-3]/r[-1] * Do
289-
BNm1Nm2 = -(KNrm2/dt)[0]
290-
BNm1Nm3 = -(KNrm3/dt)[0]
291-
sim.gas._rhs[-1] = 0.
292-
# Given power law
293-
elif sim.gas.boundary.outer.condition == "pow":
294-
p = sim.gas.boundary.outer.value
295-
sim.gas._rhs[-1] = sim.gas.Sigma[-2] * (r[-1]/r[-2])**p
296-
# Constant power law
297-
elif sim.gas.boundary.outer.condition == "const_pow":
298-
p = np.log(sim.gas.Sigma[-2] /
299-
sim.gas.Sigma[-3]) / np.log(r[-2]/r[-3])
300-
KNrm2 = - (r[-1]/r[-2])**p
301-
BNm1Nm2 = -(KNrm2/dt)[0]
302-
sim.gas._rhs[-1] = 0.
303-
224+
dat_in = [0., 0., 0.]
304225
row_out = [Nr-1, Nr-1, Nr-1]
305226
col_out = [Nr-3, Nr-2, Nr-1]
306-
dat_out = [BNm1Nm3, BNm1Nm2, BNm1Nm1]
227+
dat_out = [0., 0., 0.]
307228

308229
# Stitching together the generators
309230
row = np.hstack((row_hyd, row_in, row_out))
@@ -520,7 +441,7 @@ def vvisc(sim):
520441
return gas_f.v_visc(sim.gas.Sigma, sim.gas.nu, sim.grid.r, sim.grid.ri)
521442

522443

523-
def _f_impl_1_direct(x0, Y0, dx, jac=None, rhs=None, *args, **kwargs):
444+
def _f_impl_1_direct(x0, Y0, dx, *args, **kwargs):
524445
"""Implicit 1st-order Euler integration scheme with direct matrix inversion
525446
526447
Parameters
@@ -531,8 +452,6 @@ def _f_impl_1_direct(x0, Y0, dx, jac=None, rhs=None, *args, **kwargs):
531452
Variable to be integrated at the beginning of scheme
532453
dx : IntVar
533454
Stepsize of integration variable
534-
jac : Field, optional, defaul : None
535-
Current Jacobian. Will be calculated, if not set
536455
args : additional positional arguments
537456
kwargs : additional keyworda arguments
538457
@@ -547,16 +466,96 @@ def _f_impl_1_direct(x0, Y0, dx, jac=None, rhs=None, *args, **kwargs):
547466
---|---
548467
| 1
549468
"""
550-
if jac is None:
551-
jac = Y0.jacobian(x0, dx)
552-
if rhs is None:
553-
rhs = np.array(Y0)
469+
# Getting keyword arguments. Default is standard gas.
470+
boundary = kwargs.get("boundary", Y0._owner.gas.boundary)
471+
Sext = kwargs.get("Sext", Y0._owner.gas.S.ext)
472+
473+
dt = dx[0]
474+
jac = Y0.jacobian(x0, dx)
475+
rhs = np.array(Y0)
476+
477+
# Setting boundary values in jac and rhs
478+
479+
# Inner boundary
480+
if boundary.inner is not None:
481+
# Given value
482+
if boundary.inner.condition == "val":
483+
rhs[0] = boundary.inner.value
484+
# Constant value
485+
elif boundary.inner.condition == "const_val":
486+
jac[0, 1] = 1./dt
487+
rhs[0] = 0.
488+
# Given gradient
489+
elif boundary.inner.condition == "grad":
490+
K1 = - boundary.inner._r[1]/boundary.inner._r[0]
491+
jac[0, 1] = -K1/dt
492+
rhs[0] = - boundary.inner._ri[1]/boundary.inner._r[0] * \
493+
(boundary.inner._r[1]-boundary.inner._r[0]) * \
494+
boundary.inner.value
495+
# Constant gradient
496+
elif boundary.inner.condition == "const_grad":
497+
Di = boundary.inner._ri[1]/boundary.inner._ri[2] * (
498+
boundary.inner._r[1]-boundary.inner._r[0]) / (boundary.inner._r[2]-boundary.inner._r[0])
499+
K1 = - boundary.inner._r[1]/boundary.inner._r[0] * (1. + Di)
500+
K2 = boundary.inner._r[2]/boundary.inner._r[0] * Di
501+
jac[0, :3] = 0.
502+
jac[0, 1] = -K1/dt
503+
jac[0, 2] = -K2/dt
504+
rhs[0] = 0.
505+
# Given power law
506+
elif boundary.inner.condition == "pow":
507+
p = boundary.inner.value
508+
rhs[0] = Y0[1] * (boundary.inner._r[0]/boundary.inner._r[1])**p
509+
# Constant power law
510+
elif boundary.inner.condition == "const_pow":
511+
p = np.log(Y0[2] / Y0[1]) / \
512+
np.log(boundary.inner._r[2]/boundary.inner._r[1])
513+
K1 = - (boundary.inner._r[0]/boundary.inner._r[1])**p
514+
jac[0, 1] = -K1/dt
515+
rhs[0] = 0.
516+
517+
# Outer boundary
518+
if boundary.outer is not None:
519+
# Given value
520+
if boundary.outer.condition == "val":
521+
rhs[-1] = boundary.outer.value
522+
# Constant value
523+
elif boundary.outer.condition == "const_val":
524+
jac[-1, -2] = (1./dt)
525+
rhs[-1] = 0.
526+
# Given gradient
527+
elif boundary.outer.condition == "grad":
528+
KNrm2 = - boundary.outer._r[1]/boundary.outer._r[0]
529+
jac[-1, -2] = -(KNrm2/dt)
530+
rhs[-1] = boundary.outer._ri[1]/boundary.outer._r[0] * \
531+
(boundary.outer._r[0]-boundary.outer._r[1]) * \
532+
boundary.outer.value
533+
# Constant gradient
534+
elif boundary.outer.condition == "const_grad":
535+
Do = boundary.outer._ri[1]/boundary.outer._ri[2] * (
536+
boundary.outer._r[0]-boundary.outer._r[1]) / (boundary.outer._r[1]-boundary.outer._r[2])
537+
KNrm2 = - boundary.outer._r[1]/boundary.outer._r[0] * (1. + Do)
538+
KNrm3 = boundary.outer._r[2]/boundary.outer._r[0] * Do
539+
jac[-1, -2] = -KNrm2/dt
540+
jac[-1, -3] = -KNrm3/dt
541+
rhs[-1] = 0.
542+
# Given power law
543+
elif boundary.outer.condition == "pow":
544+
p = boundary.outer.value
545+
rhs[-1] = Y0[-2] * (boundary.outer._r[-0]/boundary.outer._r[1])**p
546+
# Constant power law
547+
elif boundary.outer.condition == "const_pow":
548+
p = np.log(Y0[-2] / Y0[-3]) / \
549+
np.log(boundary.outer._r[1]/boundary.outer._r[2])
550+
KNrm2 = - (boundary.outer._r[0]/boundary.outer._r[1])**p
551+
jac[-1, -2] = -KNrm2/dt
552+
rhs[-1] = 0.
554553

555554
# Add external source terms to right-hand side
556555
rhs[:] = gas_f.modified_rhs(
557556
dx[0],
558557
rhs,
559-
Y0._owner.gas.S.ext
558+
Sext
560559
)
561560

562561
jac.data[:] = gas_f.modified_jacobian(

0 commit comments

Comments
 (0)