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

Update gather_data function #226

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/underworld3/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def view(self):
else:
i = 0

ii = uw.utilities.gather_data(np.array([i]), dtype="int")
ii = uw.utilities.gather_data(np.array([i]))

if uw.mpi.rank == 0:
print(
Expand Down
50 changes: 50 additions & 0 deletions src/underworld3/maths/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sympy import sympify
from typing import Optional, Callable
from underworld3 import function
from underworld3.cython.petsc_maths import Integral


def delta(
Expand All @@ -15,3 +16,52 @@ def delta(
delta_fn = sympy.exp(-(x**2) / (2 * epsilon**2)) / (epsilon * sqrt_2pi)

return delta_fn



def L2_norm(U_exp, A_exp, mesh):
'''
Compute the L2 norm between the computed and analytical fields, both of which are sympy.Matrix objects.

The L2-norm is defined as:

.. math::
L_2\text{-norm} = \left( \int_{\Omega} |u_{\text{numeric}}(x) - u_{\text{analytic}}(x)|^2 \, dx \right)^{1/2}

Where:
.. math::
- \( u_{\text{numeric}}(x) \) is the numerical solution.
- \( u_{\text{analytic}}(x) \) is the analytical solution.
- \( \Omega \) is the domain.
- The integral computes the total squared error across the entire domain, and the square root is applied after the integration to give the L2-norm.

Parameters:
U_exp : sympy.Matrix representing a scalar (1x1) or vector field (Nx1 or 1xN)
A_exp : sympy.Matrix representing a scalar (1x1) or vector field (Nx1 or 1xN)
mesh : the mesh over which to integrate

Returns:
L2_norm : the computed L2 norm
'''
# Check if the inputs are matrices
if isinstance(U_exp, sympy.Matrix) and isinstance(A_exp, sympy.Matrix):
# Ensure that the dimensions of U_exp and A_exp match
assert U_exp.shape == A_exp.shape, "U_exp and A_exp must have the same shape."

# Initialize the squared difference
squared_diff = 0

# Loop over the components of the matrices (scalar or vector case)
for i in range(U_exp.shape[0]):
squared_diff += (U_exp[i] - A_exp[i])**2

else:
raise TypeError("U_exp and A_exp must be sympy.Matrix objects.")

# Perform the integral over the mesh
I = Integral(mesh, squared_diff)

# Compute the L2 norm (sqrt of the integral result)
L2_norm = sympy.sqrt( I.evaluate() )

return L2_norm
23 changes: 14 additions & 9 deletions src/underworld3/utilities/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ def mem_footprint():
return python_process.memory_info().rss // 1000000


def gather_data(val, bcast=False, dtype="float64"):
def gather_data(val, masked_value=-999999, bcast=False):

"""
gather values on root (bcast=False) or all (bcast = True) processors
Parameters:
vals : Values to combine into a single array on the root or all processors
masked_value : value to use as mask value
bcast : broadcast array/value to all processors

returns:
val_global : combination of values form all processors
Expand All @@ -106,12 +108,15 @@ def gather_data(val, bcast=False, dtype="float64"):
rank = uw.mpi.rank
size = uw.mpi.size

dtype = val.dtype


### make sure all data comes in the same order
with uw.mpi.call_pattern(pattern="sequential"):
if len(val > 0):
val_local = np.ascontiguousarray(val.copy())
else:
val_local = np.array([np.nan], dtype=dtype)
# with uw.mpi.call_pattern(pattern="sequential"):
Copy link
Member

Choose a reason for hiding this comment

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

Why comment out the uw.mpi.call_pattern part? This was useful for dealing with sequential or parallel hdf5 implementations on various HPCs.

Copy link
Member Author

Choose a reason for hiding this comment

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

From tests I've done it hasn't made a difference to the order of the array. If anything needed to be sequential it would be the gathering of the data on the root processor not the creation of the array on each processor. I'm not sure how Gatherv gathers the data but from those tests thee array is in the same order each time.

if len(val > 0):
val_local = np.ascontiguousarray(val.copy(), dtype=dtype)
else:
val_local = np.ascontiguousarray([masked_value], dtype=dtype)


comm.barrier()
Expand All @@ -127,14 +132,14 @@ def gather_data(val, bcast=False, dtype="float64"):

comm.barrier()

## gather x values, can't do them together
## gather the data on the root proc
comm.Gatherv(sendbuf=val_local, recvbuf=(val_global, sendcounts), root=0)

comm.barrier()

if uw.mpi.rank == 0:
### remove rows with NaN
val_global = val_global[~np.isnan(val_global)]
### remove rows with mask value
val_global = val_global[val_global != masked_value]

comm.barrier()

Expand Down
Loading