PyMFEM icon indicating copy to clipboard operation
PyMFEM copied to clipboard

Wrapper for mfem.tmop.TMOPMatrixCoefficient and

Open silverrose-llnl opened this issue 1 year ago • 3 comments

Hello!

I'm trying to run some tmop functions using PyMFEM. I was translating the follwing miniapp mesh-optimizer.cpp but ran into a problem using the mfem.tmop.TMOPMatrixCoefficient:

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [15], in 31 target_t = mfem.tmop.TargetConstructor.GIVEN_FULL 32 tc = mfem.tmop.AnalyticAdaptTC(target_t) ---> 33 adapt_coeff = HessianCoefficient() 34 adapt_coeff.SetMetric(metric_id) 35 tc.SetAnalyticTargetSpec(None, None, adapt_coeff)

File ~/.local/lib/python3.9/site-packages/mfem/_ser/tmop.py:2153, in TMOPMatrixCoefficient.init(self, *args, **kwargs) 2152 def init(self, *args, **kwargs): -> 2153 raise AttributeError("No constructor defined - class is abstract")

AttributeError: No constructor defined - class is abstract

/g/g20/rosaplat/.local/lib/python3.9/site-packages/mfem/_ser/tmop.py(2153)init() 2151 2152 def init(self, *args, **kwargs): -> 2153 raise AttributeError("No constructor defined - class is abstract") 2154 repr = _swig_repr 2155

`

I tried seeing if I could go into the .i files as fix it, but since that class depends on the MatrixCoefficient class which seems to also have a special wrapper I'm out of my depth as someone who has never used swig before. Any help would be greatly appreciated.

silverrose-llnl avatar Aug 02 '24 18:08 silverrose-llnl

Hi @silverrose-llnl This is in fact complicated issue. HessianCoefficient is defined in mesh-optimizer.hpp The routine in mesh-optimizer.hpp is not built as a part of mfem, and they don't exit in libmfem.so., nor installed under (prefix)/include/mfem

There are two options:

  1. Create Python version of routines in mesh-optimizer.hpp. In this scenario, since HessianCoefficient is subclass of TMOPMatrixCoefficient. We need to let SWIG to know that TMOPMatrixCoefficient could be subclassed as Python class. This can be done by using SWIG director.

  2. Add mesh-optimizer.i and load mesh-optimizer.hpp in it. This approach keeps the routine in mesh-optimizer.hpp written as C++. Thus, it is more efficient. However you lose the option to modify the behavior by subclassing it by Python.

One downside of option 2 is that since mesh-optimizer.hpp is not installed under include/mfem, we need MFEM source when building PyMFEM from source., although we already did this with the distant solver (see https://github.com/mfem/PyMFEM/blob/master/mfem/_par/dist_solver.i) I am guessing that, in the future, this could be improved if *.hpp under miniapp directory is also installed. But I haven't had a chance to look into it.

sshiraiwa avatar Aug 04 '24 15:08 sshiraiwa

Hello @sshiraiwa !

I was thinking of going through option 1 since I have already translated mesh-optimizer.hpp to python. So for modifying the .i scripts, would it be the same pattern used for mfem.PyCoefficient?

silverrose-llnl avatar Aug 13 '24 17:08 silverrose-llnl

Yes. But, I think the following set of lines in bilininteg.i https://github.com/mfem/PyMFEM/blob/master/mfem/_ser/bilininteg.i#L1 https://github.com/mfem/PyMFEM/blob/master/mfem/_ser/bilininteg.i#L40C1-L44C38 are a simpler demonstration showing how to turn on director class in Python wrapper.

sshiraiwa avatar Aug 14 '24 00:08 sshiraiwa

Thanks! Following those I was able to properly wrap it!

silverrose-llnl avatar Nov 08 '24 16:11 silverrose-llnl