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

Moved mask computation into adapter initialization #25

Open
wants to merge 3 commits into
base: develop
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
15 changes: 3 additions & 12 deletions fenicsxprecice/adapter_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ def determine_function_type(input_obj):
raise Exception("Error determining type of given dolfin FunctionSpace")


def convert_fenicsx_to_precice(fenicsx_function, local_ids):
def convert_fenicsx_to_precice(fenicsx_function, mask, local_ids):
"""
Converts data of type dolfin.Function into Numpy array for all x and y coordinates on the boundary.

Parameters
----------
fenicsx_function : FEniCSx function
A FEniCSx function referring to a physical variable in the problem.
mask : numpy array
Array of Function degrees of freedom on which the data gets sampled
local_ids: numpy array
Array of local indices of vertices on the coupling interface and owned by this rank.

Expand All @@ -106,18 +108,7 @@ def convert_fenicsx_to_precice(fenicsx_function, local_ids):
raise Exception("Cannot handle data type {}".format(type(fenicsx_function)))

precice_data = []
# sampled_data = fenicsx_function.x.array # that works only for 1st order elements where dofs = grid points
# TODO begin dirty fix. See https://github.com/precice/fenicsx-adapter/issues/17 for details.
x_mesh = fenicsx_function.function_space.mesh.geometry.x
x_dofs = fenicsx_function.function_space.tabulate_dof_coordinates()
mask = [] # where dof coordinate == mesh coordinate
for i in range(x_dofs.shape[0]):
for j in range(x_mesh.shape[0]):
if np.allclose(x_dofs[i, :], x_mesh[j, :], 1e-15):
mask.append(i)
break
sampled_data = fenicsx_function.x.array[mask]
# end dirty fix

if len(local_ids):
if fenicsx_function.function_space.num_sub_spaces > 0: # function space is VectorFunctionSpace
Expand Down
14 changes: 13 additions & 1 deletion fenicsxprecice/fenicsxprecice.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(self, mpi_comm, adapter_config_filename='precice-adapter-config.jso
# coupling mesh related quantities
self._fenicsx_vertices = Vertices()
self._precice_vertex_ids = None # initialized later
self._mask = None # initialized later

# read data related quantities (read data is read from preCICE and applied in FEniCSx)
self._read_function_type = None # stores whether read function is scalar or vector valued
Expand Down Expand Up @@ -181,7 +182,17 @@ def write_data(self, write_function):

write_function_type = determine_function_type(write_function)
assert (write_function_type in list(FunctionType))
write_data = convert_fenicsx_to_precice(write_function, self._fenicsx_vertices.get_ids())

x_mesh = write_function.function_space.mesh.geometry.x
x_dofs = write_function.function_space.tabulate_dof_coordinates()
if (self._mask is None):
self._mask = [] # where dof coordinate == mesh coordinate
for i in range(x_dofs.shape[0]):
for j in range(x_mesh.shape[0]):
if np.allclose(x_dofs[i, :], x_mesh[j, :], 1e-15):
self._mask.append(i)
break
write_data = convert_fenicsx_to_precice(write_function, self._mask, self._fenicsx_vertices.get_ids())
if write_function_type is FunctionType.SCALAR:
assert (write_function.function_space.num_sub_spaces == 0)
write_data = np.squeeze(write_data) # TODO dirty solution
Expand Down Expand Up @@ -218,6 +229,7 @@ def initialize(self, coupling_subdomain, read_function_space=None, write_object=
if isinstance(write_object, Function): # precice.initialize_data() will be called using this Function
write_function_space = write_object.function_space
write_function = write_object

elif isinstance(write_object, FunctionSpace): # preCICE will use default zero values for initialization.
write_function_space = write_object
write_function = None
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/test_adapter_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def __call__(self, x):
manual_sampling.append([fun_lambda(v[0], v[1])])
manual_sampling = np.array(manual_sampling).squeeze()

data = convert_fenicsx_to_precice(fenicsx_function, local_ids)
x_mesh = fenicsx_function.function_space.mesh.geometry.x
x_dofs = fenicsx_function.function_space.tabulate_dof_coordinates()
mask = [] # where dof coordinate == mesh coordinate
for i in range(x_dofs.shape[0]):
for j in range(x_mesh.shape[0]):
if np.allclose(x_dofs[i, :], x_mesh[j, :], 1e-15):
mask.append(i)
break

data = convert_fenicsx_to_precice(fenicsx_function, mask, local_ids)

np.testing.assert_allclose(data, manual_sampling, atol=10**-16)