Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
generate_flux_intflux.py
1import numpy as np
2
3# -------------------------------
4# functions for coagulation
5# -------------------------------
6
7
8def compute_flux_coag_k0(gij, tensor_tabflux_coag):
9 """
10 Function to compute the approximation of the coagulation flux with DG scheme k=0
11 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
12
13 Parameters
14 ----------
15 gij : 1D array (dim = nbins), type -> float
16 components of g on the polynomial basis
17 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
18 array to evaluate coagulation flux
19
20
21 Returns
22 -------
23 flux : 1D array (dim = nbins), type -> float
24 approximation of the coagulation flux at each bin
25
26 """
27
28 # use Einstein summation over all the pair interaction (l,m) for each j.
29 # compared to the definition of tensor_tabflux_coag l <-> lp and m <-> l
30 flux = np.einsum("jlm,l,m->j", tensor_tabflux_coag, gij, gij)
31
32 return flux
33
34
35def compute_flux_coag_k0_kdv(gij, tensor_tabflux_coag, dv):
36 """
37 Function to compute the approximation of the coagulation flux with DG scheme k=0
38 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
39 Function for ballistic kernel with differential velocities dv
40
41 Parameters
42 ----------
43 gij : 1D array (dim = nbins), type -> float
44 components of g on the polynomial basis
45 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
46 array to evaluate coagulation flux
47 dv : 2D array (dim = (nbins,nbins)), type -> float
48 array of the differential velocity between grains
49
50
51 Returns
52 -------
53 flux : 1D array (dim = nbins), type -> float
54 approximation of the coagulation flux at each bin
55
56 """
57
58 # use Einstein summation over all the pair interaction (l,m) for each j.
59 # compared to the definition of tensor_tabflux_coag l <-> lp and m <-> l
60 flux = np.einsum("jlm,lm,l,m->j", tensor_tabflux_coag, dv, gij, gij)
61
62 return flux
63
64
65def compute_flux_coag(gij, tensor_tabflux_coag):
66 """
67 Function to compute the approximation of the coagulation flux with DG scheme k>0
68 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
69
70 Parameters
71 ----------
72 gij : 2D array (dim = (nbins,kpol+1)), type -> float
73 components of g on the polynomial basis
74 tensor_tabflux_coag : 5D array (dim = (nbins,nbins,nbins,kpol+1,kpol+1)), type -> float
75 array to evaluate coagulation flux
76
77
78 Returns
79 -------
80 flux : 1D array (dim = nbins), type -> float
81 approximation of the coagulation flux at each bin
82
83 """
84
85 # test to use only einsum, does not work yet !
86 # flux = np.einsum('jlmqp,lq,mp->j', tensor_tabflux_coag, gij, gij)
87 # flux = 0.
88
89 # create 4d array with element gij*gij
90 arr_gij = np.einsum("ac,bd->abcd", gij, gij)
91 flux = np.asarray(
92 [np.sum(tensor_tabflux_coag[j, :, :, :, :] * arr_gij) for j in range(len(gij[:, 0]))]
93 )
94
95 return flux
96
97
98def compute_flux_coag_kdv(gij, tensor_tabflux_coag, dv):
99 """
100 Function to compute the approximation of the coagulation flux with DG scheme k>0
101 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
102 Function for ballistic kernel with differential velocities dv
103
104 Parameters
105 ----------
106 gij : 2D array (dim = (nbins,kpol+1)), type -> float
107 components of g on the polynomial basis
108 tensor_tabflux_coag : 5D array (dim = (nbins,nbins,nbins,kpol+1,kpol+1)), type -> float
109 array to evaluate coagulation flux
110 dv : 2D array (dim = (nbins,nbins)), type -> float
111 array of the differential velocity between grains
112
113
114 Returns
115 -------
116 flux : 1D array (dim = nbins), type -> float
117 approximation of the coagulation flux at each bin
118
119 """
120
121 # test to use only einsum, does not work yet !
122 # flux = np.einsum('jlmqp,lq,mp->j', tensor_tabflux_coag, gij, gij)
123 # flux = 0.
124
125 # create 4d array with element gij*gij
126 # arr_gij_dv = np.einsum('ac,bd,ab->abcd',gij,gij,dv)
127
128 # test
129 nbins = len(gij[:, 0])
130 kpol = len(gij[0, :]) - 1
131 arr_gij_dv = np.zeros((nbins, nbins, kpol + 1, kpol + 1))
132 for lp in range(nbins):
133 for l in range(nbins):
134 for ip in range(kpol + 1):
135 for i in range(kpol + 1):
136 arr_gij_dv[lp, l, ip, i] = gij[lp, ip] * gij[l, i] * dv[lp, l]
137
138 flux = np.asarray(
139 [np.sum(tensor_tabflux_coag[j, :, :, :, :] * arr_gij_dv) for j in range(len(gij[:, 0]))]
140 )
141
142 return flux
143
144
145def compute_intflux_coag(gij, tensor_tabintflux_coag):
146 """
147 Function to compute the approximation of the term including the integral of coagulation flux with DG scheme k>0
148
149 Parameters
150 ----------
151 gij : 2D array (dim = (nbins,kpol+1)), type -> float
152 components of g on the polynomial basis
153 tensor_tabintflux_coag : 6D array (dim = (nbins,kpol+1,nbins,nbins,kpol+1,kpol+1)), type -> float
154 array to evaluate the term including the intgegral of the coagulation flux
155
156
157 Returns
158 -------
159 intflux : 2D array (dim = (nbins,kpol+1)), type -> float
160 approximation of the term including the integral of coagulation flux at each bin
161
162 """
163 # create 4d array with element gij*gij
164 arr_gij = np.einsum("ac,bd->abcd", gij, gij)
165
166 nbins = len(gij[:, 0])
167 dim_kpol = len(gij[0, :])
168 intflux = np.zeros((nbins, dim_kpol))
169 for j in range(nbins):
170 for k in range(1, dim_kpol):
171 intflux[j, k] = np.sum(tensor_tabintflux_coag[j, k, :, :, :, :] * arr_gij)
172
173 return intflux
174
175
176def compute_intflux_coag_kdv(gij, tensor_tabintflux_coag, dv):
177 """
178 Function to compute the approximation of the term including the integral of coagulation flux with DG scheme k>0
179 Function for ballistic kernel with differential velocities dv
180
181 Parameters
182 ----------
183 gij : 2D array (dim = (nbins,kpol+1)), type -> float
184 components of g on the polynomial basis
185 tensor_tabintflux_coag : 6D array (dim = (nbins,kpol+1,nbins,nbins,kpol+1,kpol+1)), type -> float
186 array to evaluate the term including the integral of the coagulation flux
187 dv : 2D array (dim = (nbins,nbins)), type -> float
188 array of the differential velocity between grains
189
190 Returns
191 -------
192 intflux : 2D array (dim = (nbins,kpol+1)), type -> float
193 approximation of the term including the integral of coagulation flux at each bin
194
195 """
196 # create 4d array with element gij*gij
197 arr_gij_dv = np.einsum("ac,bd,ab->abcd", gij, gij, dv)
198
199 nbins = len(gij[:, 0])
200 dim_kpol = len(gij[0, :])
201 intflux = np.zeros((nbins, dim_kpol))
202 for j in range(nbins):
203 for k in range(1, dim_kpol):
204 intflux[j, k] = np.sum(tensor_tabintflux_coag[j, k, :, :, :, :] * arr_gij_dv)
205
206 return intflux
207
208
209# -------------------------------
210# functions for fragmentation
211# -------------------------------
212
213
214def compute_flux_frag_k0(gij, tensor_tabflux_coag, tensor_tabflux_frag_T1, tensor_tabflux_frag_T2):
215 """
216 Function to compute the approximation of the fragmentation flux with DG scheme k=0, for simple collision kernels
217
218 Frag flux is calculated from the expression (similar to coag) dg/dt + dF/dm = 0
219 -> frag flux is defined from small to large masses
220 -> frag flux is defined on the right boundary of bin j, i.e. flux(j) ~ F(m_{j+1/2})
221
222 Parameters
223 ----------
224 gij : 1D array (dim = nbins), type -> float
225 components of g on the polynomial basis
226 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
227 array to evaluate coagulation term in the fragmentation flux
228 tensor_tabflux_frag_T1 : 3D array (dim = (nbins,nbins,nbins)), type -> float
229 array to evaluate the term 1 for fragmentation flux
230 tensor_tabflux_frag_T2 : 3D array (dim = (nbins,nbins,nbins)), type -> float
231 array to evaluate the term 2 for fragmentation flux
232
233
234 Returns
235 -------
236 flux : 1D array (dim = nbins), type -> float
237 approximation of the fragmentation flux at each bin
238
239 """
240
241 # use Einstein summation over all the pair interaction (l,m) for each j.
242 # compared to the definition of tensor_tabflux l <-> lp and m <-> l
243 flux = (
244 np.einsum("jlm,l,m->j", tensor_tabflux_coag, gij, gij)
245 - np.einsum("jlm,l,m->j", tensor_tabflux_frag_T1, gij, gij)
246 - np.einsum("jlm,l,m->j", tensor_tabflux_frag_T2, gij, gij)
247 )
248
249 return flux
250
251
252def compute_flux_frag_k0_kdv(
253 gij, tensor_tabflux_coag, tensor_tabflux_frag_T1, tensor_tabflux_frag_T2, dv
254):
255 """
256 Function to compute the approximation of the coagulation flux with DG scheme k=0, for physical collision kernel
257
258 Frag flux is calculated from the expression (similar to coag) dg/dt + dF/dm = 0
259 -> frag flux is defined from small to large masses
260 -> frag flux is defined on the right boundary of bin j, i.e. flux(j) ~ F(m_{j+1/2})
261
262 Function for ballistic kernel with differential velocities dv
263
264 Parameters
265 ----------
266 gij : 1D array (dim = nbins), type -> float
267 components of g on the polynomial basis
268 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
269 array to evaluate coagulation flux
270 tensor_tabflux_frag_T1 : 3D array (dim = (nbins,nbins,nbins)), type -> float
271 array to evaluate the term 1 for fragmentation flux
272 tensor_tabflux_frag_T2 : 3D array (dim = (nbins,nbins,nbins)), type -> float
273 array to evaluate the term 2 for fragmentation flux
274 dv : 2D array (dim = (nbins,nbins)), type -> float
275 array of the differential velocity between grains
276
277
278 Returns
279 -------
280 flux : 1D array (dim = nbins), type -> float
281 approximation of the fragmentation flux at each bin
282
283 """
284
285 # use Einstein summation over all the pair interaction (l,m) for each j.
286 # compared to the definition of tensor_tabflux l <-> lp and m <-> l
287 flux = (
288 np.einsum("jlm,lm,l,m->j", tensor_tabflux_coag, dv, gij, gij)
289 - np.einsum("jlm,lm,l,m->j", tensor_tabflux_frag_T1, dv, gij, gij)
290 - np.einsum("jlm,lm,l,m->j", tensor_tabflux_frag_T2, dv, gij, gij)
291 )
292
293 return flux
294
295
296# -------------------------------
297# functions for coagulation + fragmentation
298# -------------------------------
299def compute_flux_coag_frag_k0_kdv(
300 gij,
301 tensor_tabflux_coag,
302 tensor_tabflux_frag_T1,
303 tensor_tabflux_frag_T2,
304 dv,
305 proba_coag,
306 proba_frag,
307):
308 """
309 Function to compute the approximation of the coagulation flux with DG scheme k=0, for physical collision kernel
310
311 Frag flux is calculated from the expression (similar to coag) dg/dt + dF/dm = 0
312 -> frag flux is defined from small to large masses
313 -> frag flux is defined on the right boundary of bin j, i.e. flux(j) ~ F(m_{j+1/2})
314
315 Function for ballistic kernel with differential velocities dv
316
317 Parameters
318 ----------
319 gij : 1D array (dim = nbins), type -> float
320 components of g on the polynomial basis
321 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
322 array to evaluate coagulation flux
323 tensor_tabflux_frag_T1 : 3D array (dim = (nbins,nbins,nbins)), type -> float
324 array to evaluate the term 1 for fragmentation flux
325 tensor_tabflux_frag_T2 : 3D array (dim = (nbins,nbins,nbins)), type -> float
326 array to evaluate the term 2 for fragmentation flux
327 dv : 2D array (dim = (nbins,nbins)), type -> float
328 array of the differential velocity between grains
329 proba_coag : 2D array (dim = (nbins,nbins)), type -> float
330 probability to have coagulation (depending on v_frag and dv)
331 proba_frag : 2D array (dim = (nbins,nbins)), type -> float
332 probability to have fragmentation (depending on v_frag and dv)
333
334
335 Returns
336 -------
337 flux : 1D array (dim = nbins), type -> float
338 approximation of the fragmentation flux at each bin
339
340 """
341
342 # use Einstein summation over all the pair interaction (l,m) for each j.
343 # compared to the definition of tensor_tabflux l <-> lp and m <-> l
344 dv_coag = proba_coag * dv
345 dv_frag = proba_frag * dv
346
347 flux = (
348 np.einsum("jlm,lm,l,m->j", tensor_tabflux_coag, dv_coag + dv_frag, gij, gij)
349 - np.einsum("jlm,lm,l,m->j", tensor_tabflux_frag_T1, dv_frag, gij, gij)
350 - np.einsum("jlm,lm,l,m->j", tensor_tabflux_frag_T2, dv_frag, gij, gij)
351 )
352
353 return flux