Passing variables in ini file to mako kernels

Dear developers,

How can I pass a new constant variable created in ini file to the mako kernels? Can some constants be global constants?

Best regards,
Will

Hi Will,

You can use an .ini file constant in the kernels via ${c[‘yourvariable’]} syntax.

Cheers,

Niki

Dear Niki,

I tried that before, but failed in pyfr:kernel file. I create some constants and quote em with ${[‘variable’]}. However, it said “TypeError: ‘Undefined’ object is not subscriptable”

Best regards,

Dear Niki,

To be specific, I am modifying negdivconf.mako which is with some header like “pyfr:kernel”. I create some constants in ini file [constants] and quote em with ${[‘variable’]}. However, it said “TypeError: ‘Undefined’ object is not subscriptable”. The total error is shown below,

Traceback (most recent call last):
File “/usr/local/lib/python3.5/dist-packages/pyfr/util.py”, line 33, in call
res = cache[key]
KeyError: (<function BasePointwiseKernelProvider._render_kernel at 0x7f59b53abea0>, b’\x80\x03X\n\x00\x00\x00negdivconfq\x00X)\x00\x00\x00pyfr.solvers.baseadvec.kernels.negdivconfq\x01}q\x02(X\x05\x00\x00\x00ndimsq\x03K\x02X\x05\x00\x00\x00srcexq\x04]q\x05(X\x04\x00\x00\x00(0.)q\x06X\x04\x00\x00\x00(0.)q\x07X\x04\x00\x00\x00(0.)q\x08eX\x05\x00\x00\x00nvarsq\tK\x03u\x87q\n.’, b’\x80\x03}q\x00.’)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/local/bin/pyfr”, line 11, in
sys.exit(main())
File “/usr/local/lib/python3.5/dist-packages/pyfr/main.py”, line 110, in main
args.process(args)
File “/usr/local/lib/python3.5/dist-packages/pyfr/main.py”, line 235, in process_run
args, NativeReader(args.mesh), None, Inifile.load(args.cfg)
File “/usr/local/lib/python3.5/dist-packages/pyfr/main.py”, line 216, in _process_common
solver = get_solver(backend, rallocs, mesh, soln, cfg)
File “/usr/local/lib/python3.5/dist-packages/pyfr/solvers/init.py”, line 16, in get_solver
return get_integrator(backend, systemcls, rallocs, mesh, initsoln, cfg)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/init.py”, line 46, in get_integrator
return integrator(backend, systemcls, rallocs, mesh, initsoln, cfg)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/dual/controllers.py”, line 48, in init
super().init(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/dual/controllers.py”, line 15, in init
super().init(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/dual/steppers.py”, line 8, in init
super().init(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/dual/base.py”, line 10, in init
super().init(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/base.py”, line 58, in init
self._init_system(systemcls, backend, rallocs, mesh, initsoln)
File “/usr/local/lib/python3.5/dist-packages/pyfr/integrators/base.py”, line 88, in _init_system
self.system = systemcls(*args, nreg=self.nreg, cfg=self.cfg)
File “/usr/local/lib/python3.5/dist-packages/pyfr/solvers/base/system.py”, line 65, in init
self._gen_kernels(eles, int_inters, mpi_inters, bc_inters)
File “/usr/local/lib/python3.5/dist-packages/pyfr/solvers/base/system.py”, line 174, in _gen_kernels
kernels[pn, kn].append(kgetter())
File “/usr/local/lib/python3.5/dist-packages/pyfr/solvers/baseadvec/elements.py”, line 116, in
rcpdjac=self.rcpdjac_at(‘upts’), ploc=plocupts, u=solnupts
File “/usr/local/lib/python3.5/dist-packages/pyfr/backends/base/backend.py”, line 166, in kernel
return kern(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/backends/base/kernels.py”, line 158, in kernel_meth
src, ndim, argn, argt = self._render_kernel(name, mod, tplargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/util.py”, line 35, in call
res = cache[key] = self.func(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/backends/base/kernels.py”, line 78, in _render_kernel
src = tpl.render(**tplargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/template.py”, line 36, in render
return super().render(*args, **dict(self.dfltargs, **kwargs))
File “/home/ginn/.local/lib/python3.5/site-packages/mako/template.py”, line 462, in render
return runtime.render(self, self.callable, args, data)
File “/home/ginn/.local/lib/python3.5/site-packages/mako/runtime.py”, line 838, in _render
**kwargs_for_callable(callable, data))
File “/home/ginn/.local/lib/python3.5/site-packages/mako/runtime.py”, line 873, in _render_context
exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File “/home/ginn/.local/lib/python3.5/site-packages/mako/runtime.py”, line 899, in exec_template
callable
(context, *args, **kwargs)
File “memory:0x7f59a0a0fd30”, line 39, in render_body
File “memory:0x7f59b6bcea20”, line 61, in render_body
File “/home/ginn/.local/lib/python3.5/site-packages/mako/runtime.py”, line 692, in wrap_stackframe
return func(context, *args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/pyfr/backends/base/makoutil.py”, line 117, in kernel
body = capture(context, context[‘caller’].body)
File “/home/ginn/.local/lib/python3.5/site-packages/mako/runtime.py”, line 713, in capture
callable
(*args, **kwargs)
File “memory:0x7f59b6bcea20”, line 55, in body
TypeError: ‘Undefined’ object is not subscriptable

Regards,
Will

All constants are stored in the dictionary c. You are missing the c.

c[‘variable’], c[‘nu’], c[‘mu’]. etc.

Niki

Sorry, typo. I did have ‘c’. What I wrote is like:

tdivtconf[${c[‘orient’]}] = tdivtconf[${c[‘orient’]}] - ${c[‘dpdz’]};

Then I create some variables:

orient = 1 ;x=1 y=2 z=3 is the direction of the flow
dpdz = -4.0 ;pressure gradient

When to run, errors shown above occurs.

Regards,

In the Python side:

Template parameters for the flux kernel

tplargs = dict(ndims=self.ndims, nvars=self.nvars,
c=self.cfg.items_as(‘constants’, float))

All constants are converted to floats, so you cannot use them for indexing as it is.

Cheers,
Niki

Dear Niki,

I made the index as integer, while it is still the same error. I also try to pass ‘ac-zeta’ to the kernel file. It is still the same error.

Regards,
Will

Excluding ini.file, should I declare those new constants in some other files before quoting them in </%pyfr:kernel> such as negdivconf.mako?

Dear Niki,

I am actually a rookie of mako python language. I tried add ${c[‘nu’]} to the baseadvec kernel to test whether constant variables in .ini file can be quoted. It looks like:

% for i, ex in enumerate(srcex):
tdivtconf[${i}] = -rcpdjactdivtconf[${i}] + ${ex};
% endfor
tdivtconf[1] = tdivtconf[1] - 4.0
${c[‘nu’]};

When to run, the error showed again “‘Undefined’ object is not subscriptable”. It seems that the kernel file cannot quote variables in ‘constants’ dict. There is “c=self.cfg.items_as(’
constants’, float)” in base folder and I tried to import base folder to baseadvec, but nothing helped.

I dont understand why the constants cannot be quoted by baseadvec kernel. Is there anyway to solve it?

Best regards,
Will

Okay, it seems that the constants section is not passed to the negdivconf kernel. In other pointwise kernels the approach would work as it is.

Now you need to add the following line to baseadvec/elements.py to pass the constants to the template:

@@ -45,6 +45,8 @@ class BaseAdvectionElements(BaseElements):
‘srcex’: self._src_exprs
}

  • srctplargs = {**srctplargs, **dict(c=self.cfg.items_as(‘constants’, float))}
1 Like

Thank you so much !

Best regards,
Will