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

Use absolute values to calculate normalizing factor for relative norms in adaptivity #125

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## latest

- Use absolute values to calculate normalizing factor for relative norms in adaptivity https://github.com/precice/micro-manager/pull/125
- Add option to use only one micro simulation object in the snapshot computation https://github.com/precice/micro-manager/pull/123
- Explicitly check if time window has converged using the API function `is_time_window_complete()` https://github.com/precice/micro-manager/pull/118
- Add `MicroManagerSnapshot` enabling snapshot computation and storage of microdata in HDF5 format https://github.com/precice/micro-manager/pull/101
Expand Down
24 changes: 17 additions & 7 deletions micro_manager/adaptivity/adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _get_similarity_dists(
for name in data.keys():
data_vals = data[name]
if data_vals.ndim == 1:
# If the adaptivity-data is a scalar for each simulation,
# If the adaptivity data is a scalar for each simulation,
# expand the dimension to make it a 2D array to unify the calculation.
# The axis is later reduced with a norm.
data_vals = np.expand_dims(data_vals, axis=1)
Expand Down Expand Up @@ -274,7 +274,7 @@ def _l2(self, data: np.ndarray) -> np.ndarray:
def _l1rel(self, data: np.ndarray) -> np.ndarray:
"""
Calculate L1 norm of relative difference of data.
The relative difference is calculated by dividing the difference of two data points by the maximum of the two data points.
The relative difference is calculated by dividing the difference of two data points by the maximum of the absolute value of the two data points.

Parameters
----------
Expand All @@ -288,16 +288,21 @@ def _l1rel(self, data: np.ndarray) -> np.ndarray:
"""
pointwise_diff = data[np.newaxis, :] - data[:, np.newaxis]
# divide by data to get relative difference
# divide i,j by max(data[i],data[j]) to get relative difference
# divide i,j by max(abs(data[i]),abs(data[j])) to get relative difference
relative = np.nan_to_num(
(pointwise_diff / np.maximum(data[np.newaxis, :], data[:, np.newaxis]))
(
pointwise_diff
/ np.maximum(
np.absolute(data[np.newaxis, :]), np.absolute(data[:, np.newaxis])
)
)
)
return np.linalg.norm(relative, ord=1, axis=-1)

def _l2rel(self, data: np.ndarray) -> np.ndarray:
"""
Calculate L2 norm of relative difference of data.
The relative difference is calculated by dividing the difference of two data points by the maximum of the two data points.
The relative difference is calculated by dividing the difference of two data points by the maximum of the absolute value of the two data points.

Parameters
----------
Expand All @@ -311,8 +316,13 @@ def _l2rel(self, data: np.ndarray) -> np.ndarray:
"""
pointwise_diff = data[np.newaxis, :] - data[:, np.newaxis]
# divide by data to get relative difference
# divide i,j by max(data[i],data[j]) to get relative difference
# divide i,j by max(abs(data[i]),abs(data[j])) to get relative difference
relative = np.nan_to_num(
(pointwise_diff / np.maximum(data[np.newaxis, :], data[:, np.newaxis]))
(
pointwise_diff
/ np.maximum(
np.absolute(data[np.newaxis, :]), np.absolute(data[:, np.newaxis])
)
)
)
return np.linalg.norm(relative, ord=2, axis=-1)