-
Notifications
You must be signed in to change notification settings - Fork 37
fix: improve Poisson solver IVP robustness (#162) #303
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
Open
alok-108
wants to merge
2
commits into
theochem:master
Choose a base branch
from
alok-108:fix-162-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−15
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import numpy as np | ||
| import pytest | ||
| from numpy.testing import assert_allclose | ||
| import warnings | ||
| from unittest.mock import patch | ||
|
|
||
| from grid.atomgrid import AtomGrid | ||
| from grid.onedgrid import Trapezoidal | ||
| from grid.rtransform import LinearFiniteRTransform, InverseRTransform | ||
| from grid.poisson import solve_poisson_bvp, solve_poisson_ivp, _solve_poisson_ivp_atomgrid | ||
|
|
||
| def gauss_density(pts, alpha=1.0): | ||
| r = np.linalg.norm(pts, axis=1) | ||
| return np.exp(-alpha * r**2) | ||
|
|
||
| def exp_density(pts, alpha=1.0): | ||
| r = np.linalg.norm(pts, axis=1) | ||
| return np.exp(-alpha * r) | ||
|
|
||
| def setup_grid(): | ||
| oned = Trapezoidal(250) | ||
| btf = LinearFiniteRTransform(1e-3, 20.0) | ||
| radial = btf.transform_1d_grid(oned) | ||
| atgrid = AtomGrid(radial, center=np.array([0.0, 0.0, 0.0]), degrees=[11]) | ||
| return atgrid, btf | ||
|
|
||
| def test_ivp_gaussian_vs_bvp(): | ||
| atgrid, btf = setup_grid() | ||
| density = gauss_density(atgrid.points, alpha=1.0) | ||
|
|
||
| pot_ivp = solve_poisson_ivp( | ||
| atgrid, | ||
| density, | ||
| InverseRTransform(btf), | ||
| r_interval=(20.0, 1e-3) | ||
| ) | ||
|
|
||
| pot_bvp = solve_poisson_bvp( | ||
| atgrid, | ||
| density, | ||
| InverseRTransform(btf), | ||
| include_origin=True | ||
| ) | ||
|
|
||
| pts = atgrid.points | ||
| val_ivp = pot_ivp(pts) | ||
| val_bvp = pot_bvp(pts) | ||
|
|
||
| # Check that they match | ||
| assert_allclose(val_ivp, val_bvp, rtol=1e-2, atol=1e-2) | ||
|
|
||
| def test_ivp_fallback_warning(): | ||
| atgrid, btf = setup_grid() | ||
| density = exp_density(atgrid.points, alpha=100.0) | ||
|
|
||
| import grid.ode | ||
| original_solve_ivp = grid.ode.solve_ivp | ||
|
|
||
| call_count = 0 | ||
| def mock_solve_ivp(fun, t_span, y0, method, **kwargs): | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| if call_count == 1: | ||
| class DummyRes: | ||
| status = -1 | ||
| message = "Failed" | ||
| return DummyRes() | ||
| return original_solve_ivp(fun, t_span, y0, method=method, **kwargs) | ||
|
|
||
| with patch("grid.ode.solve_ivp", side_effect=mock_solve_ivp): | ||
| with pytest.warns(UserWarning, match="ODE solver method.*failed.*Trying next method"): | ||
| pot_ivp = solve_poisson_ivp( | ||
| atgrid, | ||
| density, | ||
| InverseRTransform(btf), | ||
| r_interval=(20.0, 1e-3) | ||
| ) | ||
|
|
||
| val = pot_ivp(atgrid.points) | ||
| assert not np.any(np.isnan(val)) | ||
|
|
||
| def test_adaptive_r_interval(): | ||
| atgrid, btf = setup_grid() | ||
|
|
||
| density_compact = exp_density(atgrid.points, alpha=5.0) | ||
| density_diffuse = exp_density(atgrid.points, alpha=0.1) | ||
|
|
||
| used_r_intervals = [] | ||
|
|
||
| original_solve = _solve_poisson_ivp_atomgrid | ||
|
|
||
| def mock_solve(atomgrid, func_vals, transform, r_interval, ode_params=None): | ||
| r_pts = atomgrid.rgrid.points | ||
| density_abs = np.abs(func_vals) | ||
| n_radial = len(r_pts) | ||
| if len(density_abs) % n_radial == 0: | ||
| n_angular = len(density_abs) // n_radial | ||
| density_abs = density_abs.reshape(n_radial, n_angular).sum(axis=1) | ||
| cumsum = np.cumsum(density_abs) | ||
| if cumsum[-1] > 0: | ||
| idx = np.searchsorted(cumsum, 0.99 * cumsum[-1]) | ||
| r_99 = r_pts[min(idx, len(r_pts) - 1)] | ||
| r_max = r_99 * 10 | ||
| if r_max > r_interval[0]: | ||
| r_max = r_interval[0] | ||
| if transform is not None and r_max > transform.domain[1]: | ||
| r_max = transform.domain[1] | ||
| used_r_intervals.append(r_max) | ||
| else: | ||
| used_r_intervals.append(r_interval[0]) | ||
| return original_solve(atomgrid, func_vals, transform, r_interval, ode_params) | ||
|
|
||
| import grid.poisson | ||
| grid.poisson._solve_poisson_ivp_atomgrid = mock_solve | ||
|
|
||
| try: | ||
| solve_poisson_ivp(atgrid, density_compact, InverseRTransform(btf), r_interval=(20.0, 1e-3)) | ||
| solve_poisson_ivp(atgrid, density_diffuse, InverseRTransform(btf), r_interval=(20.0, 1e-3)) | ||
| finally: | ||
| grid.poisson._solve_poisson_ivp_atomgrid = original_solve | ||
|
|
||
| assert len(used_r_intervals) == 2 | ||
| r_max_compact, r_max_diffuse = used_r_intervals | ||
| assert r_max_compact < r_max_diffuse | ||
|
alok-108 marked this conversation as resolved.
Outdated
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.