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

numerical stability in cylindrical coordinate #2644

Open
mochen4 opened this issue Sep 19, 2023 · 8 comments
Open

numerical stability in cylindrical coordinate #2644

mochen4 opened this issue Sep 19, 2023 · 8 comments

Comments

@mochen4
Copy link
Collaborator

mochen4 commented Sep 19, 2023

RuntimeError: meep: simulation fields are NaN or Inf occured at the beginning of field updates when the source includes $r=0$ point at $|m| > 1$. I used a custom amplitude function such that the amplitude was 0 at $r=0$, and this check did pass.

This issue doesn't occur at $m = -1, 0, 1$. And since the amplitude should be 0 at $r=0$ for $|m| > 1$, we could just exclude the point when we define sources for now.

@mochen4 mochen4 changed the title source at r=0 in cylindrical coordinate source at r=0 in cylindrical coordinate for |m|>1 Sep 19, 2023
@stevengj
Copy link
Collaborator

stevengj commented Sep 21, 2023

One thing to try would be to add a line after this line:

if (amps_array[idx_vol] == 0) continue;

Then you have to be careful because this check:

if (idx_vol > npts)
looks insufficient. If idx_vol < npts, we should really resize the two arrays to be smaller (length = idx_vol).

@mochen4
Copy link
Collaborator Author

mochen4 commented Sep 24, 2023

Here is a simple script that shows the issue:

import numpy as np
import meep as mp

resolution = 25
frq, dfrq = 1, 0.2
dpml, dair = 1, 1
sr, sz = dair + dpml, dpml + dair + dpml
pml_layers = [mp.PML(thickness=dpml)]
cell_size = mp.Vector3(sr, 0, sz)

Jr = lambda v3: np.sin(v3.x + 0.5*dair)
offset = 0

sources = [mp.Source(mp.GaussianSource(frq, fwidth=dfrq), component=mp.Er,
        center=mp.Vector3(0.5 * dair, 0, -0.5 * sz + dpml + 0.25*dair), size=mp.Vector3(dair-offset),
        amp_func = Jr)]
sim = mp.Simulation(cell_size=cell_size, boundary_layers=pml_layers,
    resolution=resolution, sources=sources,dimensions=mp.CYLINDRICAL, m=2,)
sim.run(until_after_sources=10)

The issue goes aways if there is some offset between the source and r=0, e.g. offset = 0.15

@mochen4
Copy link
Collaborator Author

mochen4 commented Oct 2, 2023

It also appears that more pixels need to be excluded (larger offset) for larger $|m|$.

@stevengj
Copy link
Collaborator

stevengj commented Dec 8, 2023

You could also just try a smaller Courant number following this comment: https://github.com/NanoComp/meep/blob/7e747d2c4de03329506dd2d31bfcc53e12460029/src/step_db.cpp#L436C30-L441

@stevengj
Copy link
Collaborator

stevengj commented Dec 8, 2023

See also the accurate_fields_near_cylorigin option in the Python API, which documents this behavior.

@mochen4
Copy link
Collaborator Author

mochen4 commented Dec 15, 2023

Numerical instability does seem to be the issue, and smaller Courant factor indeed solves the problem. Maybe we need to change the default behavior for $|m|&gt;1$: either zero more pixels near $r=0$ or use smaller Courant factors.

@mochen4
Copy link
Collaborator Author

mochen4 commented Jan 4, 2024

Here is the paper I found https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7696718, which suggested a suspiciously large small factor.

I will do some more numerical experiments based on #2644 (comment) and find the largest Courant factor at each m value.

@mochen4
Copy link
Collaborator Author

mochen4 commented Jan 10, 2024

Here is what I found in Taflove's Computational Electrodynamic. He didn't give any theoretical proof, but his results are consistent with what I found with my test script.

Screen Shot 2024-01-09 at 19 24 19

@mochen4 mochen4 changed the title source at r=0 in cylindrical coordinate for |m|>1 numerical stability in cylindrical coordinate Jan 10, 2024
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