Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
generate_flux_intflux.py
1import numpy as np
2
3
4def compute_flux_coag_k0(gij, tensor_tabflux_coag):
5 """
6 Function to compute the approximation of the coagulation flux with DG scheme k=0
7 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
8
9 Parameters
10 ----------
11 gij : 1D array (dim = nbins), type -> float
12 components of g on the polynomial basis
13 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
14 array to evaluate coagulation flux
15
16
17 Returns
18 -------
19 flux : 1D array (dim = nbins), type -> float
20 approximation of the coagulation flux at each bin
21
22 """
23
24 # use Einstein summation over all the pair interaction (l,m) for each j.
25 # compared to the definition of tensor_tabflux_coag l <-> lp and m <-> l
26 flux = np.einsum("jlm,l,m->j", tensor_tabflux_coag, gij, gij)
27
28 return flux
29
30
31def compute_flux_coag_k0_kdv(gij, tensor_tabflux_coag, dv):
32 """
33 Function to compute the approximation of the coagulation flux with DG scheme k=0
34 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
35 Function for ballistic kernel with differential velocities dv
36
37 Parameters
38 ----------
39 gij : 1D array (dim = nbins), type -> float
40 components of g on the polynomial basis
41 tensor_tabflux_coag : 3D array (dim = (nbins,nbins,nbins)), type -> float
42 array to evaluate coagulation flux
43 dv : 2D array (dim = (nbins,nbins)), type -> float
44 array of the differential velocity between grains
45
46
47 Returns
48 -------
49 flux : 1D array (dim = nbins), type -> float
50 approximation of the coagulation flux at each bin
51
52 """
53
54 # use Einstein summation over all the pair interaction (l,m) for each j.
55 # compared to the definition of tensor_tabflux_coag l <-> lp and m <-> l
56 flux = np.einsum("jlm,lm,l,m->j", tensor_tabflux_coag, dv, gij, gij)
57
58 return flux
59
60
61def compute_flux_coag(gij, tensor_tabflux_coag):
62 """
63 Function to compute the approximation of the coagulation flux with DG scheme k>0
64 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
65
66 Parameters
67 ----------
68 gij : 2D array (dim = (nbins,kpol+1)), type -> float
69 components of g on the polynomial basis
70 tensor_tabflux_coag : 5D array (dim = (nbins,nbins,nbins,kpol+1,kpol+1)), type -> float
71 array to evaluate coagulation flux
72
73
74 Returns
75 -------
76 flux : 1D array (dim = nbins), type -> float
77 approximation of the coagulation flux at each bin
78
79 """
80
81 # test to use only einsum, does not work yet !
82 # flux = np.einsum('jlmqp,lq,mp->j', tensor_tabflux_coag, gij, gij)
83 # flux = 0.
84
85 # create 4d array with element gij*gij
86 arr_gij = np.einsum("ac,bd->abcd", gij, gij)
87 flux = np.asarray(
88 [np.sum(tensor_tabflux_coag[j, :, :, :, :] * arr_gij) for j in range(len(gij[:, 0]))]
89 )
90
91 return flux
92
93
94def compute_flux_coag_kdv(gij, tensor_tabflux_coag, dv):
95 """
96 Function to compute the approximation of the coagulation flux with DG scheme k>0
97 Flux defined at the right boundary of mass bins, i.e. flux[j] ~ F(m_{j+1/2})
98 Function for ballistic kernel with differential velocities dv
99
100 Parameters
101 ----------
102 gij : 2D array (dim = (nbins,kpol+1)), type -> float
103 components of g on the polynomial basis
104 tensor_tabflux_coag : 5D array (dim = (nbins,nbins,nbins,kpol+1,kpol+1)), type -> float
105 array to evaluate coagulation flux
106 dv : 2D array (dim = (nbins,nbins)), type -> float
107 array of the differential velocity between grains
108
109
110 Returns
111 -------
112 flux : 1D array (dim = nbins), type -> float
113 approximation of the coagulation flux at each bin
114
115 """
116
117 # test to use only einsum, does not work yet !
118 # flux = np.einsum('jlmqp,lq,mp->j', tensor_tabflux_coag, gij, gij)
119 # flux = 0.
120
121 # create 4d array with element gij*gij
122 # arr_gij_dv = np.einsum('ac,bd,ab->abcd',gij,gij,dv)
123
124 # test
125 nbins = len(gij[:, 0])
126 kpol = len(gij[0, :]) - 1
127 arr_gij_dv = np.zeros((nbins, nbins, kpol + 1, kpol + 1))
128 for lp in range(nbins):
129 for l in range(nbins):
130 for ip in range(kpol + 1):
131 for i in range(kpol + 1):
132 arr_gij_dv[lp, l, ip, i] = gij[lp, ip] * gij[l, i] * dv[lp, l]
133
134 flux = np.asarray(
135 [np.sum(tensor_tabflux_coag[j, :, :, :, :] * arr_gij_dv) for j in range(len(gij[:, 0]))]
136 )
137
138 return flux
139
140
141def compute_intflux_coag(gij, tensor_tabintflux_coag):
142 """
143 Function to compute the approximation of the term including the integral of coagulation flux with DG scheme k>0
144
145 Parameters
146 ----------
147 gij : 2D array (dim = (nbins,kpol+1)), type -> float
148 components of g on the polynomial basis
149 tensor_tabintflux_coag : 6D array (dim = (nbins,kpol+1,nbins,nbins,kpol+1,kpol+1)), type -> float
150 array to evaluate the term including the intgegral of the coagulation flux
151
152
153 Returns
154 -------
155 intflux : 2D array (dim = (nbins,kpol+1)), type -> float
156 approximation of the term including the integral of coagulation flux at each bin
157
158 """
159 # create 4d array with element gij*gij
160 arr_gij = np.einsum("ac,bd->abcd", gij, gij)
161
162 nbins = len(gij[:, 0])
163 dim_kpol = len(gij[0, :])
164 intflux = np.zeros((nbins, dim_kpol))
165 for j in range(nbins):
166 for k in range(1, dim_kpol):
167 intflux[j, k] = np.sum(tensor_tabintflux_coag[j, k, :, :, :, :] * arr_gij)
168
169 return intflux
170
171
172def compute_intflux_coag_kdv(gij, tensor_tabintflux_coag, dv):
173 """
174 Function to compute the approximation of the term including the integral of coagulation flux with DG scheme k>0
175 Function for ballistic kernel with differential velocities dv
176
177 Parameters
178 ----------
179 gij : 2D array (dim = (nbins,kpol+1)), type -> float
180 components of g on the polynomial basis
181 tensor_tabintflux_coag : 6D array (dim = (nbins,kpol+1,nbins,nbins,kpol+1,kpol+1)), type -> float
182 array to evaluate the term including the integral of the coagulation flux
183 dv : 2D array (dim = (nbins,nbins)), type -> float
184 array of the differential velocity between grains
185
186 Returns
187 -------
188 intflux : 2D array (dim = (nbins,kpol+1)), type -> float
189 approximation of the term including the integral of coagulation flux at each bin
190
191 """
192 # create 4d array with element gij*gij
193 arr_gij_dv = np.einsum("ac,bd,ab->abcd", gij, gij, dv)
194
195 nbins = len(gij[:, 0])
196 dim_kpol = len(gij[0, :])
197 intflux = np.zeros((nbins, dim_kpol))
198 for j in range(nbins):
199 for k in range(1, dim_kpol):
200 intflux[j, k] = np.sum(tensor_tabintflux_coag[j, k, :, :, :, :] * arr_gij_dv)
201
202 return intflux