Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign spatially varying matrix coefficient #57

Closed
kaigaox opened this issue Mar 5, 2021 · 4 comments
Closed

Assign spatially varying matrix coefficient #57

kaigaox opened this issue Mar 5, 2021 · 4 comments
Assignees

Comments

@kaigaox
Copy link

kaigaox commented Mar 5, 2021

Hi,

Is there an example on how to assign matrix coefficient to each of the elements in the mesh?

@sshiraiwa sshiraiwa self-assigned this Mar 6, 2021
@sshiraiwa
Copy link
Member

May I ask how your coefficient is defined? If it is a regular function of position, x, y, z, then
one can use MatrixFunctionCoefficient. This can be done in a similar way as VectorPyCoefficient shown in this example https://github.com/mfem/PyMFEM/blob/master/examples/ex3.py#L25. If you are looking for a way to define it differently for example by using finite element index, we need to write it using more low-level MFEM API. It is probably doable, but more involved.

@kaigaox
Copy link
Author

kaigaox commented Mar 6, 2021

May I ask how your coefficient is defined? If it is a regular function of position, x, y, z, then
one can use MatrixFunctionCoefficient. This can be done in a similar way as VectorPyCoefficient shown in this example https://github.com/mfem/PyMFEM/blob/master/examples/ex3.py#L25. If you are looking for a way to define it differently for example by using finite element index, we need to write it using more low-level MFEM API. It is probably doable, but more involved.

Thanks for the instruction. My coefficients are defined in an n1xn2xn3 discrete grid points in space (regularly sampled, loaded from external file), not an analytical function, and the coefficient is a 3x3 matrix at each grid point. I didn't figure out how to define such a coefficient and apply it to the Poisson during pymfem. What is the appropriate mfem API for this and does pymfem include an example of this kind? Is it MatrixCoefficient or something similar? Thank you very much!

@sshiraiwa
Copy link
Member

My apology for slow response.

Assuming it is okay to interpolate each of 3x3 matrix element separately, one way is
to use RegularGridInterpolator. For example, you can generate an interpolation function in a global scope, like

from scipy.interpolate import RegularGridInterpolator as RGI
func = RGI((x, y, z), data)

I added a sample matrix_coefficient.
https://github.com/mfem/PyMFEM/blob/master/examples/wrapper_example/matrix_coefficient.py

Although the example returns a fixed 2x2 matrix (it is a 2D example), one can call the above function from inside MatrixPyCoefficient::EvalValue

@sshiraiwa
Copy link
Member

Closing for now. Feel free to open if you have further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants