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

Test on both nightly and stable #34

Merged
merged 4 commits into from
Sep 25, 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
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ on:
push:
branches: [main]


jobs:
test-code:
runs-on: ubuntu-22.04
container: ghcr.io/fenics/dolfinx/dolfinx:v0.8.0
name: Test on ${{ matrix.container }}
runs-on: ubuntu-24.04
container: ${{ matrix.container }}
env:
DEB_PYTHON_INSTALL_LAYOUT: deb_system


strategy:
fail-fast: false
matrix:
container: [
"ghcr.io/fenics/dolfinx/dolfinx:stable",
"ghcr.io/fenics/dolfinx/dolfinx:nightly"
]
steps:
- uses: actions/checkout@v4

Expand Down
11 changes: 11 additions & 0 deletions src/cardiac_geometries/fibers/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pathlib import Path
from typing import NamedTuple, Sequence

Expand Down Expand Up @@ -43,6 +44,16 @@ def save_microstructure(
attributes=attributes,
)

def json_serial(obj):
if isinstance(obj, (np.ndarray)):
return obj.tolist()
raise TypeError("Type %s not serializable" % type(obj))

if mesh.comm.rank == 0:
(Path(outdir) / "microstructure.json").write_text(
json.dumps(attributes, indent=4, default=json_serial)
)


def normalize(u):
return u / np.linalg.norm(u, axis=0)
Expand Down
17 changes: 13 additions & 4 deletions src/cardiac_geometries/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,22 @@ def from_folder(cls, comm: MPI.Intracomm, folder: str | Path) -> "Geometry":
else:
markers = {}

if (folder / "microstructure.json").exists():
if comm.rank == 0:
microstructure = json.loads((folder / "microstructure.json").read_text())
else:
microstructure = {}
microstructure = comm.bcast(microstructure, root=0)
else:
microstructure = {}

functions = {}
microstructure_path = folder / "microstructure.bp"
if microstructure_path.exists():
function_space = adios4dolfinx.read_attributes(
comm=MPI.COMM_WORLD, filename=microstructure_path, name="function_space"
)
for name, el in function_space.items():
# function_space = adios4dolfinx.read_attributes(
# comm=MPI.COMM_WORLD, filename=microstructure_path, name="function_space"
# )
for name, el in microstructure.items():
element = utils.array2element(el)
V = dolfinx.fem.functionspace(mesh, element)
f = dolfinx.fem.Function(V, name=name)
Expand Down
37 changes: 29 additions & 8 deletions src/cardiac_geometries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ class GMshModel(NamedTuple):
vertex_tags: dolfinx.mesh.MeshTags


def distribute_entity_data(
mesh: dolfinx.mesh.Mesh,
tdim: int,
marked_entities: np.ndarray,
entity_values: np.ndarray,
) -> tuple[np.ndarray, np.ndarray]:
if dolfinx.__version__ >= "0.9.0":
local_entities, local_values = dolfinx.io.utils.distribute_entity_data(
mesh, tdim, marked_entities, entity_values
)
else:
local_entities, local_values = dolfinx.io.utils.distribute_entity_data(
mesh._cpp_object, tdim, marked_entities, entity_values
)
return local_entities, local_values


# copied from https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/io/gmshio.py
def model_to_mesh(
model,
Expand Down Expand Up @@ -160,9 +177,10 @@ def model_to_mesh(
)

# Create MeshTags for cells
local_entities, local_values = dolfinx.io.utils.distribute_entity_data(
mesh._cpp_object, mesh.topology.dim, cells, cell_values
local_entities, local_values = distribute_entity_data(
mesh, mesh.topology.dim, cells, cell_values
)

mesh.topology.create_connectivity(mesh.topology.dim, 0)
adj = dolfinx.cpp.graph.AdjacencyList_int32(local_entities)
ct = dolfinx.mesh.meshtags_from_entities(
Expand All @@ -188,11 +206,13 @@ def model_to_mesh(
gmsh_facet_perm = dolfinx.io.gmshio.cell_perm_array(facet_type, num_facet_nodes)
marked_facets = marked_facets[:, gmsh_facet_perm]

local_entities, local_values = dolfinx.io.utils.distribute_entity_data(
mesh._cpp_object, tdim - 1, marked_facets, facet_values
local_entities, local_values = distribute_entity_data(
mesh, mesh.topology.dim - 1, marked_facets, facet_values
)

mesh.topology.create_connectivity(topology.dim - 1, tdim)
adj = dolfinx.cpp.graph.AdjacencyList_int32(local_entities)

ft = dolfinx.io.gmshio.meshtags_from_entities(
mesh, tdim - 1, adj, local_values.astype(np.int32, copy=False)
)
Expand All @@ -210,8 +230,8 @@ def model_to_mesh(
gmsh_edge_perm = dolfinx.io.gmshio.cell_perm_array(edge_type, num_edge_nodes)
marked_edges = marked_edges[:, gmsh_edge_perm]

local_entities, local_values = dolfinx.io.utils.distribute_entity_data(
mesh._cpp_object, tdim - 2, marked_edges, edge_values
local_entities, local_values = distribute_entity_data(
mesh, tdim - 2, marked_edges, edge_values
)
mesh.topology.create_connectivity(topology.dim - 2, tdim)
adj = dolfinx.cpp.graph.AdjacencyList_int32(local_entities)
Expand All @@ -232,8 +252,8 @@ def model_to_mesh(
gmsh_vertex_perm = dolfinx.io.gmshio.cell_perm_array(vertex_type, num_vertex_nodes)
marked_vertices = marked_vertices[:, gmsh_vertex_perm]

local_entities, local_values = dolfinx.io.utils.distribute_entity_data(
mesh._cpp_object, tdim - 3, marked_vertices, vertex_values
local_entities, local_values = distribute_entity_data(
mesh, tdim - 3, marked_vertices, vertex_values
)
mesh.topology.create_connectivity(topology.dim - 3, tdim)
adj = dolfinx.cpp.graph.AdjacencyList_int32(local_entities)
Expand Down Expand Up @@ -340,6 +360,7 @@ def array2element(arr: np.ndarray) -> basix.finite_element.FiniteElement:
degree=degree,
discontinuous=discontinuous,
shape=(3,),
lagrange_variant=basix.LagrangeVariant.unset,
)


Expand Down
4 changes: 3 additions & 1 deletion tests/test_save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import basix
import dolfinx
import numpy as np
import pytest

import cardiac_geometries
import cardiac_geometries.geometry
Expand Down Expand Up @@ -48,6 +49,7 @@ def test_save_load_mesh_and_tags(tmp_path):
assert geo2.ffun is not None


@pytest.mark.xfail(reason="Bug in adios2")
def test_save_load_mesh_and_function(tmp_path):
comm = MPI.COMM_WORLD
mesh = dolfinx.mesh.create_unit_cube(comm, 3, 3, 3)
Expand All @@ -65,7 +67,7 @@ def test_save_load_mesh_and_function(tmp_path):
)
f = dolfinx.fem.Function(V)
f.interpolate(lambda x: x)
geo = cardiac_geometries.geometry.Geometry(mesh=mesh, f0=f)
geo = cardiac_geometries.geometry.Geometry(mesh=mesh, f0=f, markers={"ENDO": [1, 2]})

folder = comm.bcast(tmp_path, root=0)
path = folder / "geo.bp"
Expand Down
Loading