netgen icon indicating copy to clipboard operation
netgen copied to clipboard

Some Features For The Python Wrapper

Open UZerbinati opened this issue 2 years ago • 4 comments

I've added some features to the Python wrapper, in particular:

  • Wrapped SetIndex Function;
  • Added some functions that allow easy export from Netgen to PETSc DMPlex;
  • New name attribute and related function.

UZerbinati avatar Aug 26 '22 23:08 UZerbinati

I think this has a much better chance of being merged if you rebase it first.

StefanBruens avatar Sep 11 '22 18:09 StefanBruens

Nice to get Netgen into firedrake. I would suggest an even better way though:

I've registered the element types to be used in numpy here:

https://github.com/NGSolve/netgen/commit/c18a317702569e09f78108e124b8e73623647b73

With this you get a numpy view on the element nodes:

mesh.ElementsXd().NumPy()["nodes"]

with X = 1,2,3. This gives a number_elements x element_max_points numpy matrix with one based node numbers, zero meaning that the entry is not used, so for a first order trig you would get something like [1,2,3,0,0,0,0,0], since element_max_points is 8 (2nd order quads). It is a view on the internal data so no copying is done here. From there you can use numpy routines to efficiently transform the data to your desired format (cut out the 0s, -1 for 0 based,...)

I think this is the only functionality really needed to get this performant, I think the GetBCIds is fine to be done in python, and you can just give a default "netgen_mesh" name to firedrake.

ChrLackner avatar Sep 13 '22 13:09 ChrLackner

Dear @ChrLackner,

First of all, thank you very much for pointing out this feature of netgen I didn't know. Now let me try to reply feature by feature,

  • Mesh Name, it would be very good to have a mesh parameter within Netgen because in Firedrake you can work with multiple meshes and the name tag is used when exporting to VTK in order to identify the different meshes. Of course, it would possible to import the Netgen mesh into Firedrake and once it has been converted to a PETSc DMPlex object then we use a Python function to assign it a name, but in my mind, a much more natural flow would be if the Netgen mesh has a name and this attributed is "inherited" by the DMPlex object.
  • GetBCIDs, keeping the function on the C++ side seems much faster at least if we copy in Python the same code it is now written in C++. You'll find here an example code where I time the two cases, and the result is the following; Time take using loop on BCIDs in Python is: 9.748511806999886, while keeping loop on C++ side it takes: 0.04197995800132048.
  • Indicies 2D and 3D, I'm familiarizing myself with the new feature you implemented. I tried using this new feaure in my firedrake interface and those are the results: C++ loops 0.0005521774291992188, Numpy 0.00022029876708984375. If I understand correctly this should also be very helpful to implement curved mesh elements in Firedrake. I therefore remove those two functions from the Pybind11 wrap in my PR.

Thanks again for your help :)

P.S: I think there is a typo in your example code it should be a capital D the one for the dimension, i.e. mesh.ElementsXD().NumPy()["nodes"].

P.P.S: I had some problem compiling netgen on a Linux machine, I had to change #include <pybind11/stl/filesystem.h> to #include <filesystem.h>. My understanding is that std::filesystem::path does automatically convert to pathlib.Path when working on a Linux machine.

UZerbinati avatar Sep 14 '22 21:09 UZerbinati

Dear @ChrLackner , I've also added some functions to do mesh refinement of flagged elements directly in NetGen. I didn't find this already in the Python binding, but I probably didn't know where to look. Best, Umberto

UZerbinati avatar Sep 19 '22 08:09 UZerbinati

ping @ChrLackner

UZerbinati avatar Oct 28 '22 11:10 UZerbinati

bump

luzpaz avatar Jul 19 '23 23:07 luzpaz

Why close ?

luzpaz avatar Oct 18 '23 15:10 luzpaz

@luzpaz Most of this stuff can now be done using ngsPETSc :)

UZerbinati avatar Oct 18 '23 15:10 UZerbinati