Skip to content

Commit

Permalink
Use absolute values to calculate normalizing factor for relative norm…
Browse files Browse the repository at this point in the history
…s in adaptivity (#125)

* When taking relative norm differences in similarity calculation, use absolute values to get the normalizing factor

* Fix comments with respect to earlier changes

* Add CHANGELOG entry
  • Loading branch information
IshaanDesai authored Aug 14, 2024
1 parent 9c16bfb commit 490ae43
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
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)

0 comments on commit 490ae43

Please sign in to comment.