Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
interface_coala_shamrock.py
1import numpy as np
2
3from .generate_flux_intflux import (
4 compute_flux_coag_frag_k0_kdv,
5 compute_flux_coag_k0_kdv,
6 compute_flux_frag_k0_kdv,
7)
8
9
10def coala_coag_source_term_k0(nbins, massgrid, rhodust, rhodust_eps, tensor_tabflux_coag, v_dust):
11 r"""
12 Function to compute the source for coagulation in continuity equation for piecewise constant approximation (see Lombart et al., 2021)
13 Function for ballistic kernel with differential velocities dv
14 Used to evaluate the source term, then hydro code applies time solver
15
16 /!\ Only coagulation so far
17
18 Parameters
19 ----------
20 nbins : scalar, type -> integer
21 number of dust bins
22 massgrid : 1D array (dim = nbins+1), type -> float
23 grid of masses given borders value of mass bins
24 rhodust : 1D array (dim = nbins), type -> float
25 dust density for each grain size
26 rhodust_eps : scalar, type -> float
27 threshold value for rhodust
28 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
29 array to evaluate coagulation flux
30 v_dust : 1D array (dim = (nbins)), type -> float
31 array of the dust velocities (could also be delta_v in monofluid since it is a delta)
32
33 Returns
34 -------
35 S_coag : 1D array (dim = nbins), type -> float
36 Source term for dust coagulation in continuity equation
37 DG operator for piecewise constant approximation in each binls
38
39 """
40
41 # compute gij from rhodust for coala k=0
42 gij = np.zeros(nbins) # shape is 1D with k0
43 for j in range(nbins):
44 if rhodust[j] > rhodust_eps:
45 gij[j] = rhodust[j] / (massgrid[j + 1] - massgrid[j])
46
47 # dv_ij = \vec{v_dust}_j - \vec{v_dust}_i
48 dv = np.zeros((nbins, nbins))
49 for i in range(nbins):
50 for j in range(nbins):
51 delta_v = v_dust[j] - v_dust[i]
52 dv[i, j] = np.sqrt(delta_v[0] ** 2 + delta_v[1] ** 2 + delta_v[2] ** 2)
53
54 # compute flux for all dust bins
55 flux = compute_flux_coag_k0_kdv(gij, tensor_tabflux_coag, dv)
56
57 S_coag = np.zeros(nbins)
58 S_coag[0] = -flux[0]
59 S_coag[1:] = flux[:-1] - flux[1:]
60
61 return S_coag
62
63
64def coala_frag_source_term_k0(
65 nbins,
66 massgrid,
67 rhodust,
68 rhodust_eps,
69 tensor_tabflux_coag,
70 tensor_tabflux_frag_T1,
71 tensor_tabflux_frag_T2,
72 v_dust,
73):
74 r"""
75 Function to compute the source for fragmentation in continuity equation for piecewise constant approximation (see Lombart et al., 2024)
76 Function for ballistic kernel with differential velocities dv
77 Used to evaluate the source term, then hydro code applies time solver
78
79 /!\ Only fragmentation
80
81 Parameters
82 ----------
83 nbins : scalar, type -> integer
84 number of dust bins
85 massgrid : 1D array (dim = nbins+1), type -> float
86 grid of masses given borders value of mass bins
87 rhodust : 1D array (dim = nbins), type -> float
88 dust density for each grain size
89 rhodust_eps : scalar, type -> float
90 threshold value for rhodust
91 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
92 array to evaluate coagulation term in fragmentation flux
93 tensor_tabflux_frag_T1 : 3D array (dim = (nbins,nbins,nbins)), type -> float
94 array to evaluate the term 1 for fragmentation flux
95 tensor_tabflux_frag_T2 : 3D array (dim = (nbins,nbins,nbins)), type -> float
96 array to evaluate the term 2 for fragmentation flux
97 v_dust : 1D array (dim = (nbins)), type -> float
98 array of the dust velocities (could also be delta_v in monofluid since it is a delta)
99
100 Returns
101 -------
102 S_frag : 1D array (dim = nbins), type -> float
103 Source term for dust coagulation in continuity equation
104 DG operator for piecewise constant approximation in each bins
105
106 """
107
108 # compute gij from rhodust for coala k=0
109 gij = np.zeros(nbins) # shape is 1D with k0
110 for j in range(nbins):
111 if rhodust[j] > rhodust_eps:
112 gij[j] = rhodust[j] / (massgrid[j + 1] - massgrid[j])
113
114 # dv_ij = v_dust_j - v_dust_i
115 dv = v_dust[None, :] - v_dust[:, None]
116
117 # compute flux for all dust bins
118 flux = compute_flux_frag_k0_kdv(
119 gij, tensor_tabflux_coag, tensor_tabflux_frag_T1, tensor_tabflux_frag_T2, dv
120 )
121
122 S_frag = np.zeros(nbins)
123 S_frag[0] = -flux[0]
124 S_frag[1:] = flux[:-1] - flux[1:]
125
126 return S_frag
127
128
129def coala_coag_frag_source_term_k0(
130 nbins,
131 massgrid,
132 rhodust,
133 rhodust_eps,
134 tensor_tabflux_coag,
135 tensor_tabflux_frag_T1,
136 tensor_tabflux_frag_T2,
137 v_dust,
138 threshold_vfrag,
139):
140 r"""
141 Function to compute the source for coagulation and fragmentation in continuity equation for piecewise constant approximation (see Lombart et al., 2021)
142 Function for ballistic kernel with differential velocities dv
143 Used to evaluate the source term, then hydro code applies time solver
144
145 /!\ You can relaxed, coagulation and fragmentation are ready to use
146
147 Parameters
148 ----------
149 nbins : scalar, type -> integer
150 number of dust bins
151 massgrid : 1D array (dim = nbins+1), type -> float
152 grid of masses given borders value of mass bins
153 rhodust : 1D array (dim = nbins), type -> float
154 dust density for each grain size
155 rhodust_eps : scalar, type -> float
156 threshold value for rhodust
157 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
158 array to evaluate coagulation term in fragmentation flux
159 tensor_tabflux_frag_T1 : 3D array (dim = (nbins,nbins,nbins)), type -> float
160 array to evaluate the term 1 for fragmentation flux
161 tensor_tabflux_frag_T2 : 3D array (dim = (nbins,nbins,nbins)), type -> float
162 array to evaluate the term 2 for fragmentation flux
163 v_dust : 1D array (dim = (nbins)), type -> float
164 array of the dust velocities (could also be delta_v in monofluid since it is a delta)
165 threshold_vfrag : scalar, type -> float
166 threshold velocity value for fragmentation
167
168 Returns
169 -------
170 S_coag_frag : 1D array (dim = nbins), type -> float
171 Source term for dust coagulation + fragmentation in continuity equation
172 DG operator for piecewise constant approximation in each bins
173
174 """
175
176 # compute gij from rhodust for coala k=0
177 gij = np.zeros(nbins) # shape is 1D with k0
178 for j in range(nbins):
179 if rhodust[j] > rhodust_eps:
180 gij[j] = rhodust[j] / (massgrid[j + 1] - massgrid[j])
181
182 # dv_ij = v_dust_j - v_dust_i
183 dv = v_dust[None, :] - v_dust[:, None]
184
185 # probability
186 proba_frag = (1.5 * (threshold_vfrag / dv) ** 2 + 1.0) * np.exp(
187 -1.5 * (threshold_vfrag / dv) ** 2
188 )
189 proba_coag = 1.0 - proba_frag
190
191 # compute flux for all dust bins
192 flux = compute_flux_coag_frag_k0_kdv(
193 gij,
194 tensor_tabflux_coag,
195 tensor_tabflux_frag_T1,
196 tensor_tabflux_frag_T2,
197 dv,
198 proba_coag,
199 proba_frag,
200 )
201
202 S_coag_frag = np.zeros(nbins)
203 S_coag_frag[0] = -flux[0]
204 S_coag_frag[1:] = flux[:-1] - flux[1:]
205
206 return S_coag_frag
207
208
209import time
210
211from scipy.special import legendre
212
213from .generate_tabflux_tabintflux import (
214 compute_coagtabflux_k0_numba,
215 compute_fragtabflux_k0_numba,
216)
217from .utils_polynomials import legendre_coeffs
218
219
220def coala_precalc_tabflux_coag(K0, nbins, Q, massgrid):
221 """
222 Function to iterate coagulation solver to reach the time ndthydro x dthydro
223
224 Function for ballistic kernel with differential velocities dv
225
226 DG scheme k=0, piecewise constant approximation
227
228 Parameters
229 ----------
230 K0 : scalar, type -> float
231 constant value of the kernel function (used to adapt to code unit)
232 nbins : scalar, type -> integer
233 number of dust bins
234 Q : scalar, type -> integer
235 number of points for Gauss-Legendre quadrature
236 massgrid : 1D array (dim = nbins+1), type -> float
237 grid of masses given borders value of mass bins
238
239
240 Returns
241 -------
242 gij_init : 1D array (dim = nbins) or 2D array (dim = (nbins.kpol+1)), type -> float
243 initial components of g on the polynomial basis
244 gij : 1D array (dim = nbins) or 2D array (dim = (nbins.kpol+1)), type -> float
245 evolved components of g on the polynomial basis
246 time_coag : scalar, type -> float
247 final time ndthydro x dthydro
248
249 """
250
251 kernel = 3
252 kpol = 0
253
254 vecnodes, vecweights = np.polynomial.legendre.leggauss(Q)
255
256 # Legendre polynomial coefficients
257 mat_coeffs_leg = np.zeros((kpol + 1, kpol + 1))
258 mat_coeffs_leg = legendre_coeffs(kpol)
259
260 start = time.time()
261 tensor_tabflux_coag = np.zeros((nbins, nbins, nbins))
262
263 compute_coagtabflux_k0_numba(
264 kernel,
265 K0,
266 Q,
267 vecnodes,
268 vecweights,
269 nbins,
270 massgrid,
271 mat_coeffs_leg,
272 tensor_tabflux_coag,
273 )
274
275 finish = time.time()
276 print("Tensor tabflux generated in %.5f s" % (finish - start))
277
278 return tensor_tabflux_coag
279
280
281def coala_precalc_tabflux_frag(K0, nbins, Q, massgrid, frac_mass_fragments, beta):
282 """
283 Function to iterate coagulation solver to reach the time ndthydro x dthydro
284
285 Function for ballistic kernel with differential velocities dv
286
287 DG scheme k=0, piecewise constant approximation
288
289 Parameters
290 ----------
291 K0 : scalar, type -> float
292 constant value of the kernel function (used to adapt to code unit)
293 nbins : scalar, type -> integer
294 number of dust bins
295 Q : scalar, type -> integer
296 number of points for Gauss-Legendre quadrature
297 massgrid : 1D array (dim = nbins+1), type -> float
298 grid of masses given borders value of mass bins
299 frac_mass_fragments : scalar, type -> float
300 mass fraction of total mass (u+v) represented by fragments, m_frag = frac_mass_fragments*(u+v)
301 beta : scalar, type -> float
302 coefficient for power law fragment distribution in mass range [xmin,mfrag]
303
304
305 Returns
306 -------
307 gij_init : 1D array (dim = nbins) or 2D array (dim = (nbins.kpol+1)), type -> float
308 initial components of g on the polynomial basis
309 gij : 1D array (dim = nbins) or 2D array (dim = (nbins.kpol+1)), type -> float
310 evolved components of g on the polynomial basis
311 time_coag : scalar, type -> float
312 final time ndthydro x dthydro
313
314 """
315
316 kernel = 3
317 kpol = 0
318
319 vecnodes, vecweights = np.polynomial.legendre.leggauss(Q)
320
321 # Legendre polynomial coefficients
322 mat_coeffs_leg = np.zeros((kpol + 1, kpol + 1))
323 mat_coeffs_leg = legendre_coeffs(kpol)
324
325 start = time.time()
326 tensor_tabflux_frag_T1 = np.zeros((nbins, nbins, nbins))
327 tensor_tabflux_frag_T2 = np.zeros((nbins, nbins, nbins))
328
329 compute_fragtabflux_k0_numba(
330 kernel,
331 K0,
332 Q,
333 vecnodes,
334 vecweights,
335 nbins,
336 massgrid,
337 mat_coeffs_leg,
338 frac_mass_fragments,
339 beta,
340 tensor_tabflux_frag_T1,
341 tensor_tabflux_frag_T2,
342 )
343
344 finish = time.time()
345 print("Tensor tabflux frag generated in %.5f s" % (finish - start))
346
347 return tensor_tabflux_frag_T1, tensor_tabflux_frag_T2