mfem: Evaluation of grid function coefficient leads to memory corruption
Hello,
I have an issue with the evaluation of VectorGridFunctionCoefficient.
I am trying to solve a simple waveguide problem: $$\vec{\nabla} \times (\vec{\nabla} \times \vec{E} ) - k_0^2 \vec{E} = 0 $$, where $\vec{E}$ is the electric field, and $k_0$ is the wavevector. The geometry is a simple elongated box with Dirichlet boundary conditions on the side walls and a specific Robin boundary conditions at the ends which represent the input and output ports: $$\hat{n} \times (\vec{\nabla} \times \vec{E}) = -i \beta \vec{E} + [\hat{n} \times (-2i\beta E_i \hat{x})]$$, where $\hat{n}$ is the unit vector normal, $\beta$ is the propagation constant, $E_i(x,y)$ is the incident field and $\hat{x}$ is the unit vector along the $x$ coordinate.
As you can see, the Robin boundary condition in this problem depends on the incident field $E_i$. To implement such boundary condition I use a subclass of VectorCoefficient where I hard coded the incident field $E_i$. However, in future I would like to define $E_i$ outside and pass it into the Robin bc class using VectorGridFunctionCoefficient. However, on the way to implementing this approach, I encountered a very strange problem. When I try to evaluate the corresponding VectorGridFunctionCoefficient inside the Robin bc class (without even using the results of this evaluation) I see that the otherwise correct solution becomes distorted. Thus, a simple evaluation of an arbitrary VectorGridFunctionCoefficient leads to some memory corruption. Can you please help me to understand what is going on.
In the attachment I put the MWE (the code, the mesh, and a makefile template to build, run and visualize the solution) which demonstrates the problem. To fix the distorted solution issue, you just need to comment out line 62 in the gfcoef_issue_mwe.cpp. The following figures show the correct solution and the distorted one:
Distorted solution:
Correct solution:
MWE files: gfcoef_issue_mwe.zip
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (10 by maintainers)
This is not a drawback of the the method I’m suggesting but of the existing 2-argument version.
As a solution to the issue with the current two-argument version, we can simply make it trigger an error if used in a case that requires the use of
DofTransformationwith an explanation to switch to using the new 3-argument version that I’m proposing. This will change current behavior, so to preserve backward-compatibility for some time, before introducing the error message, we can just issue a warning instead.Yes, I think that approach should work.
Even better (but a breaking change) would be to just return the
DofTransformationby value in all cases — the pattern of returning a reference/pointer to an internal object that can be invalidated mysteriously in the future seems to cause us a number of problems. Do we have performance regression tests to measure if the performance impact of such a change would be meaningful? (Maybe timing the sample runs, but maybe we have something more systematic).Dear Socratis,
Your hack works. Will wait for a more permanent solution. Thank you.
Hi @vladimir-yu-fedorov, as @pazner said this is a bit of a complicated issue and we will discuss it with the rest of the team to decide on a fix. In the meantime you can fix it by adding this line
just before
https://github.com/mfem/mfem/blob/9a9087e20b214cfb74f684429bd912e21b9381b3/fem/linearform.cpp#L263-L266
This will recompute and overwrite the
doftransagain after it was overwritten inside the integrator. This is not a nice fix, more like a hack, until with figure something else out.I hope this helps.
Best,
Socratis