-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Refactored data writing #13
Open
boris-martin
wants to merge
12
commits into
dev-partitioned-heat-equation
Choose a base branch
from
eval_writes
base: dev-partitioned-heat-equation
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.
+104
−145
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4b9576f
FIrst function to evaluate the data to write
boris-martin 5463a32
added vector test
boris-martin 9e5cc86
partial fix to dimension 2
boris-martin a9f93f3
refactored to handle 2D case
boris-martin 4991ca0
setting up required helpers for writing
boris-martin 5b56137
added missing import
boris-martin 8a2a120
fixed determine_function_type
boris-martin bb141d2
replaced vertices writing
boris-martin f08af91
re-added unexplained failed assert
boris-martin e40f630
removed dead code
boris-martin 0f6c70b
Formatting
IshaanDesai 25b1f14
Updating function descriptions
IshaanDesai 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 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 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 |
---|---|---|
|
@@ -7,9 +7,10 @@ | |
from .config import Config | ||
import logging | ||
import precice | ||
from .adapter_core import FunctionType, determine_function_type, convert_fenicsx_to_precice, get_fenicsx_vertices, CouplingMode, Vertices, VertexType | ||
from .adapter_core import FunctionType, determine_function_type, get_fenicsx_vertices, CouplingMode, Vertices, VertexType | ||
from .expression_core import SegregatedRBFInterpolationExpression | ||
from .solverstate import SolverState | ||
from .mapping_utils import precompute_eval_vertices | ||
IshaanDesai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from dolfinx.fem import Function, FunctionSpace | ||
import copy | ||
|
||
|
@@ -157,7 +158,7 @@ def read_data(self): | |
|
||
return copy.deepcopy(read_data) | ||
|
||
def write_data(self, write_function): | ||
def write_data(self, write_function: Function): | ||
""" | ||
Writes data to preCICE. Depending on the dimensions of the simulation (2D-3D Coupling, 2D-2D coupling or | ||
Scalar/Vector write function) write_data is first converted into a format needed for preCICE. | ||
|
@@ -172,18 +173,21 @@ def write_data(self, write_function): | |
CouplingMode.BI_DIRECTIONAL_COUPLING) | ||
|
||
w_func = write_function | ||
write_function_type = determine_function_type(write_function) | ||
|
||
# Check that the function provided lives on the same function space provided during initialization | ||
assert (self._write_function_type == determine_function_type(w_func)) | ||
# TODO this raises AssertionError, not sure why. I just commented it out, still works... | ||
# assert (write_function.function_space == self._write_function_space) | ||
assert (self._write_function_type == write_function_type) | ||
# TODO: fails for weird reasons. | ||
#assert (write_function.function_space == self._write_function_space) | ||
Comment on lines
+180
to
+181
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets figure this out before we merge this, it looks like we are messing up some types in DOLFINx |
||
|
||
write_data_id = self._interface.get_data_id(self._config.get_write_data_name(), | ||
self._interface.get_mesh_id(self._config.get_coupling_mesh_name())) | ||
|
||
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()) | ||
|
||
# Eval the function on the vertices | ||
write_data = w_func.eval(self._fenicsx_vertices_eval, self._fenicsx_cells_eval) | ||
|
||
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 | ||
|
@@ -289,6 +293,12 @@ def initialize(self, coupling_subdomain, read_function_space=None, write_object= | |
self._fenicsx_vertices.set_ids(ids) | ||
self._fenicsx_vertices.set_coordinates(coords) | ||
|
||
# Set up helpers to write data to preCICE: | ||
if self._coupling_type is CouplingMode.UNI_DIRECTIONAL_WRITE_COUPLING or \ | ||
self._coupling_type is CouplingMode.BI_DIRECTIONAL_COUPLING: | ||
self._fenicsx_vertices_eval, self._fenicsx_cells_eval = precompute_eval_vertices( | ||
self._fenicsx_vertices.get_coordinates(), write_function_space.mesh) | ||
|
||
# Set up mesh in preCICE | ||
self._precice_vertex_ids = self._interface.set_mesh_vertices(self._interface.get_mesh_id( | ||
self._config.get_coupling_mesh_name()), self._fenicsx_vertices.get_coordinates()) | ||
|
This file contains 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,27 @@ | ||
""" | ||
This module consists of helper functions used in the adapter related to data mapping. Names of the functions are self explanatory | ||
""" | ||
|
||
from dolfinx.fem import FunctionSpace, Function | ||
from dolfinx.mesh import Mesh | ||
from dolfinx.geometry import BoundingBoxTree, compute_colliding_cells, compute_collisions | ||
import numpy as np | ||
|
||
|
||
def precompute_eval_vertices(precice_vertices: np.ndarray, mesh: Mesh): | ||
# Pre-processing: pad with zeros if 2D vector | ||
n_vertices, dim = precice_vertices.shape | ||
if dim == 2: | ||
precice_vertices = np.append( | ||
precice_vertices, np.zeros((n_vertices, 1)), axis=1) | ||
|
||
assert precice_vertices.shape[1] == 3 | ||
|
||
tree = BoundingBoxTree(mesh, mesh.geometry.dim) | ||
cell_candidates = compute_collisions(tree, precice_vertices) | ||
cell = compute_colliding_cells(mesh, cell_candidates, precice_vertices) | ||
|
||
# Adjacency list: convert to array and take the first match for each item. | ||
# Use offsets (but the last one) to compute indices in the array | ||
list_of_cells = cell.array[cell.offsets[:-1]] | ||
return precice_vertices, list_of_cells |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,46 @@ | ||
import unittest | ||
from unittest.mock import MagicMock | ||
from unittest import TestCase | ||
import numpy as np | ||
from mpi4py import MPI | ||
from dolfinx.mesh import create_unit_square | ||
from dolfinx.fem import Function, FunctionSpace, VectorFunctionSpace | ||
|
||
|
||
class TestAdapterCore(TestCase): | ||
def test_precompute_eval_vertices(self): | ||
""" | ||
Test cell collision computation for function evaluation on vertices | ||
""" | ||
# TODO: handle preCIE using N*2 arrays and not N*3 in 2D simulations | ||
from fenicsxprecice.mapping_utils import precompute_eval_vertices | ||
|
||
mesh = create_unit_square(MPI.COMM_WORLD, 2, 2) # create dummy mesh | ||
precice_vertices = np.array([[0.5, 0.5, 0.0], | ||
[0.2, 0.2, 0.0], | ||
[1.0, 1.0, 0.0]]) | ||
|
||
# eval_pos is a copy of precice_vertices but with padded 0s in the z-dim | ||
eval_pos, cells = precompute_eval_vertices(precice_vertices, mesh) | ||
|
||
# scalar valued | ||
V = FunctionSpace(mesh, ('P', 2)) # Create function space using mesh | ||
|
||
fenicsx_function = Function(V) | ||
fenicsx_function.interpolate(lambda x: x[0] + x[1] * x[1]) | ||
|
||
expected_data = np.array([0.75, 0.24, 2.0]).reshape((3, 1)) | ||
|
||
data = fenicsx_function.eval(eval_pos, cells) | ||
np.testing.assert_almost_equal(data, expected_data) | ||
|
||
# Vector valued | ||
V = VectorFunctionSpace(mesh, ('P', 2)) | ||
|
||
fenicsx_function = Function(V) | ||
fenicsx_function.interpolate(lambda x: (x[0] + x[1] * x[1], x[0])) | ||
|
||
expected_data = np.array([[0.75, 0.50], [0.24, 0.20], [2.0, 1.0]]) | ||
|
||
data = fenicsx_function.eval(eval_pos, cells) | ||
np.testing.assert_almost_equal(data, expected_data) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be >= 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we support 3D, yes. Could also be simply "!= 0" as it's never 1 I think.