Hello there,

Assuming I have a continuous function F represented as a Python function, which can be evaluated at any point (x, y, z) in space and returns the value F(x, y, z) — i.e., point evaluation is availabe, but nothing else.

In theory, that’s enough to do quadrature, integration, etc… . But I don’t know how to do that in NgSolve with an object other than a CoefficientFunction…

So, is there a way in NgSolve to have a GridFunction (or CoefficientFunction) represent (a projection of) this function F?

- It’s quite easy to get the desired nodal interpolant of that function F in NgSolve for first order elements H1(mesh, order=1) by just setting the nodal values accordingly.
- For general order > 1, it’s not that obvious anymore; and potentially gets very messy.
- I guess there is some tool for this in NgSolve, which I just don’t know about → so I am thankful for any hints

Example code:

F = lambda px, py, pz: some_non_standard_formula(px, py, pz) # supports point evaluation

V = H1(mesh, order=5) # general order p > 1

Fh = GridFunction(V) # should represent a discretization of F

Best regards,

Carl

You cannot use python functions (As the evaluation would not be possible parallel and because of Python very slow). There are 3 options:

I guess you cannot create the function from the existing coefficientfunctions? Even not with the extension GitHub - NGSolve/ngs-special-functions: Add special functions of SLATEC (http://www.netlib.org/slatec/) to NGSolve ?

This is an extension that makes it possible to easily put any function that is available in SLATEC into a NGSolve coefficient function.

Option 2 is to use a fine voxel grid as a representation of your function and use the “VoxelCoefficient” CoefficientFunction. This expects a bounding box and values on a voxel grid as a numpy array.

Option 3 is to write the function in C++ and export it to python. How to do this is described here

https://ngsolve.org/docu/latest/mylittlengs/1_Basic/1_myFEM/coefficient.html

python export here

https://ngsolve.org/docu/latest/mylittlengs/1_Basic/pythonExport.html

Best

Christopher

Dear Christopher,

thanks for the quick reply.

Option 1 is not possible. The function F is the result of some BEM computation.

Therefore, also option 3 is also out.

I will try option 2: As I see in git, this “VoxelCoefficient” was added just recently.

I can’t find “VoxelCoefficient” in the Documentation; is there some MWE on how to use it?

Best, Carl

Yep it was. Attached is a small example

Attachment: voxel.py