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

WI add tests #54

Merged
merged 8 commits into from
Mar 3, 2022
Merged

WI add tests #54

merged 8 commits into from
Mar 3, 2022

Conversation

torbjornstoro
Copy link
Collaborator

Description

Summary of changes

  • Added test for creating the FDTD grid
  • Added test for running FDTD simulation
  • Added validation for FDTD parameters in config file

Resolved Issues

How Has This Been Tested?

  • pytest

@gomezzz gomezzz changed the base branch from main to waveregime-internship February 24, 2022 12:37
@gomezzz
Copy link
Collaborator

gomezzz commented Feb 24, 2022

Old tests don't work for me, I think in plot_spectra.py we are still using wrong imports (..trcwa.compute_spectrum)

(Works when that is fixed)

@gomezzz
Copy link
Collaborator

gomezzz commented Feb 24, 2022

Running the tests (after fixing the mentioned import) I get this warning

nidn/tests/fdtd_test.py::test_fdtd_simulation
  D:\Arbeit\Code\radiflector\NIDN\nidn\fdtd_integration\compute_spectrum_fdtd.py:123: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
    sqrt(tensor(signal[i][0] ** 2 + signal[i][1] ** 2 + signal[i][2] ** 2))

Please fix that :) (just follow the instructions from the warning I think)

@gomezzz
Copy link
Collaborator

gomezzz commented Feb 24, 2022

I think you are missing a test for calculate_transmission_reflection_coefficients ideally for both RMS and FFT to make sure they still return the expected values

Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments, we are on the right way 💪 but need to check a bit more thoroughly to make sure we notice when things break

nidn/utils/resources/default_config.toml Outdated Show resolved Hide resolved
nidn/training/utils/validate_config.py Outdated Show resolved Hide resolved
Comment on lines 92 to 93
"FDTD_use_pulsesource",
"FDTD_use_pointsource",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't these mutually exclusive? If so, shouldn't rather be something like FDTD_source_type = "pulse" or "point" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the pulse is about continuous wave or not. point is about pointsource or linesource, both which can be continuous or have a pulse. But I can change the naming/structure for clarity

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, well depends on the logic behind. "use_pointsource" to me sounds as if it is true I use a point source, otherwise I use none.

If it is like that fine, otherwise it should probably be sourcetype with FDTD_source_type="point" or "line"

Analogously for pulse

nidn/training/utils/validate_config.py Outdated Show resolved Hide resolved
from ..utils.compute_spectrum import compute_spectrum


def test_fdtd_simulation():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please write brief docstrings for each test describing what is tested, see e.g. here

"""Tests a single uniform layer at two frequencies. TRCWA vs. GRCWA"""

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite the difference between this test and the init_fdtd_grid_test?

Maybe just make it one test called fdtd_test?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fdtd_test can then have several subtests
test_init
uniform layer
4 uni
patterned

nidn/tests/init_fdtd_grid_test.py Outdated Show resolved Hide resolved
nidn/tests/init_fdtd_grid_test.py Outdated Show resolved Hide resolved
nidn/tests/init_fdtd_grid_test.py Outdated Show resolved Hide resolved
nidn/tests/init_fdtd_grid_test.py Outdated Show resolved Hide resolved
nidn/tests/init_fdtd_grid_test.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments, we are on the right way 💪 but need to check a bit more thoroughly to make sure we notice when things break

…th updating validate config and added some more logging. Also fixed unnoticed errors in transmission detector placement
Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments / changes, also tests are currently failing: see here https://github.com/esa/NIDN/runs/5371947251?check_suite_focus=true

nidn/fdtd_integration/init_fdtd.py Outdated Show resolved Hide resolved
nidn/tests/calculate_coefficient_test.py Outdated Show resolved Hide resolved
signal_array, signal_array, "mean square", cfg
)
# TODO: Add test for fdtd, when the method is complete
assert transmission_coefficient_ms - 0.25 == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for completeness also check reflection_coefficient.

Would it make sense to check here also if they are all >= 0 and <= 1.0? ( We ought to have some way to deal with this in the code as well btw 🤔 But maybe can happen when you look into the FFT? (Does that have an issue btw? Then could add a todo there for checking that spectrum values are between 0 and 1 and throwing at least a warning if they aren't.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this should be done in the test, or in the compute_spectrum_fdtd function? (The check that all values are betweenn 0 and 1)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still assert it here that they are all between 0 and 1. Doesn't hurt and serves as a sanity check if the check inside the codebase is accidentally circumvented

nidn/tests/fdtd_test.py Outdated Show resolved Hide resolved
nidn/tests/fdtd_test.py Outdated Show resolved Hide resolved
transmission_spectrum, reflection_spectrum = compute_spectrum(eps_grid, cfg)
validated_transmission_spectrum = [
tensor(0.0),
tensor(0.5564),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is not a lot of digits. Why not more precise values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the output from running the compute_spectrum in my notebook, which I just copied. Can probably set higher precision for the output?

nidn/tests/fdtd_test.py Outdated Show resolved Hide resolved
nidn/tests/fdtd_test.py Outdated Show resolved Hide resolved
nidn/tests/fdtd_test.py Outdated Show resolved Hide resolved
Comment on lines 92 to 93
"FDTD_use_pulsesource",
"FDTD_use_pointsource",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, well depends on the logic behind. "use_pointsource" to me sounds as if it is true I use a point source, otherwise I use none.

If it is like that fine, otherwise it should probably be sourcetype with FDTD_source_type="point" or "line"

Analogously for pulse

Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor changes, feel free to merge once fixed (assuming unit tests are still passing and all ;) )

@@ -76,7 +76,7 @@ def compute_spectrum_fdtd(permittivity, cfg: DotMap):
logger.debug("Reflection spectrum")
logger.debug(reflection_spectrum)

return transmission_spectrum, reflection_spectrum
return torch.tensor(transmission_spectrum), torch.tensor(reflection_spectrum)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it was not a torch tensor before then you already have lost the gradients somewhere along the way :) You can leave it for now, just pointing out

nidn/fdtd_integration/init_fdtd.py Outdated Show resolved Hide resolved
else:
grid[source_x, :, 0] = fdtd.LineSource(period=period, name="linesource")

raise ValueError(f'FDTD_source_type must be either "line" or "point"')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potentially as above with assert may be easier

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is also fine

signal_array, signal_array, "mean square", cfg
)
# TODO: Add test for fdtd, when the method is complete
assert transmission_coefficient_ms - 0.25 == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still assert it here that they are all between 0 and 1. Doesn't hurt and serves as a sanity check if the check inside the codebase is accidentally circumvented

nidn/training/utils/validate_config.py Show resolved Hide resolved
nidn/utils/resources/default_config.toml Outdated Show resolved Hide resolved
@torbjornstoro torbjornstoro merged commit d80c39f into waveregime-internship Mar 3, 2022
@torbjornstoro torbjornstoro deleted the WI_add_tests branch March 3, 2022 09:42
@gomezzz gomezzz mentioned this pull request Mar 3, 2022
3 tasks
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

Successfully merging this pull request may close these issues.

Add tests for FDTD
2 participants