CustomCodeNode should be treated like SympyAssignment in transformations (currently RNG numbers are calculated too often)
I don't know if it is a bug or if I am using it wrong, but when using mutliple rng symbols via next
on e.g. fluxes in different directions, the generated code will calculate each random number when only one is used. so basically for a D3Q19 stencil Philox is called 5 times more then is needed, which slows down the program drastically.
import pystencils as ps
import sympy as sp
from pystencils.rng import random_symbol
# Setup
stencil = 19
L=(10,10,10)
dh = ps.create_data_handling(domain_size=L, periodicity=True)
c_field = dh.add_array('c', values_per_cell=1)
j_field = dh.add_array('j', values_per_cell=stencil // 2,
field_type=ps.FieldType.STAGGERED_FLUX)
def grad(f):
return sp.Matrix([ps.fd.diff(f, i) for i in range(dh.dim)])
# genergate flux eq. and set them to 0
fvm_eq = ps.fd.FVM1stOrder(c_field, flux=grad(c_field))
flux=[]
for diff in fvm_eq.discrete_flux(j_field):
flux.append(ps.Assignment(diff.lhs, 0))
flux = ps.AssignmentCollection(flux)
# define rng
rng_symbol_gen = random_symbol(flux.subexpressions, dim=dh.dim)
for i in range(len(flux.main_assignments)):
fluct = next(rng_symbol_gen)
flux.main_assignments[i] = ps.Assignment(flux.main_assignments[i].lhs, fluct)
print(flux)
flux_kernel = ps.create_staggered_kernel(flux).compile()
ps.show_code(flux_kernel)