Skip to content

Commit dbc94f9

Browse files
authored
Merge pull request #281 from MiraGeoscience/GEOPY-2156
GEOPY-2156: Pydantic error on auto-mesh creation with Grid2D topography object
2 parents 63326ca + 8f4231b commit dbc94f9

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

simpeg_drivers/components/meshes.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from discretize import TensorMesh, TreeMesh
1919
from geoh5py import Workspace
2020
from geoh5py.groups import UIJsonGroup
21-
from geoh5py.objects import DrapeModel, Octree
21+
from geoh5py.objects import DrapeModel, Grid2D, Octree, Points
2222
from grid_apps.octree_creation.driver import OctreeDriver
2323
from grid_apps.octree_creation.options import OctreeOptions
2424
from grid_apps.utils import octree_2_treemesh, treemesh_2_octree
@@ -113,9 +113,19 @@ def get_entity(self) -> Octree | DrapeModel:
113113
def _auto_mesh(self):
114114
"""Automate meshing based on data and topography objects."""
115115

116+
topography = self.params.active_cells.topography_object
117+
if isinstance(topography, Grid2D):
118+
with Workspace() as ws:
119+
vertices = topography.centroids.copy()
120+
if self.params.active_cells.topography is not None:
121+
vertices = np.column_stack(
122+
[vertices[:, :2], self.params.active_cells.topography.values]
123+
)
124+
topography = Points.create(ws, vertices=vertices)
125+
116126
params = auto_mesh_parameters(
117-
self.params.data_object,
118-
self.params.active_cells.topography_object,
127+
survey=self.params.data_object,
128+
topography=topography,
119129
inversion_type=self.params.inversion_type,
120130
)
121131
driver = OctreeDriver(params)

tests/meshes_test.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import pytest
1717
from discretize import TreeMesh
1818
from geoh5py import Workspace
19-
from geoh5py.objects import Octree
19+
from geoh5py.objects import Grid2D, Octree
2020
from grid_apps.utils import treemesh_2_octree
2121

2222
from simpeg_drivers.components import InversionMesh
2323
from simpeg_drivers.options import ActiveCellsOptions
2424
from simpeg_drivers.potential_fields import MVIInversionOptions
25+
from simpeg_drivers.potential_fields.magnetic_vector.driver import MVIInversionDriver
2526
from simpeg_drivers.utils.synthetics.driver import SyntheticsComponents
2627
from simpeg_drivers.utils.synthetics.options import (
2728
MeshOptions,
@@ -32,7 +33,7 @@
3233
from tests.utils.targets import get_workspace
3334

3435

35-
def get_mvi_params(tmp_path: Path) -> MVIInversionOptions:
36+
def get_mvi_params(tmp_path: Path, updates=None) -> MVIInversionOptions:
3637
opts = SyntheticsComponentsOptions(
3738
method="magnetic_vector",
3839
survey=SurveyOptions(n_stations=4, n_lines=4),
@@ -52,20 +53,22 @@ def get_mvi_params(tmp_path: Path) -> MVIInversionOptions:
5253
{"elevation": {"values": components.topography.vertices[:, 2]}}
5354
)
5455

55-
params = MVIInversionOptions.build(
56-
geoh5=geoh5,
57-
data_object=components.survey,
58-
tmi_channel=tmi_channel,
59-
tmi_uncertainty=0.01,
60-
active_cells=ActiveCellsOptions(
61-
topography_object=components.topography, topography=elevation
62-
),
63-
inducing_field_strength=50000.0,
64-
inducing_field_inclination=60.0,
65-
inducing_field_declination=30.0,
66-
mesh=mesh,
67-
starting_model=components.model,
68-
)
56+
kwargs = {
57+
"geoh5": geoh5,
58+
"data_object": components.survey,
59+
"tmi_channel": tmi_channel,
60+
"tmi_uncertainty": 0.01,
61+
"topography_object": components.topography,
62+
"topography": elevation,
63+
"inducing_field_strength": 50000.0,
64+
"inducing_field_inclination": 60.0,
65+
"inducing_field_declination": 30.0,
66+
"mesh": mesh,
67+
"starting_model": components.model,
68+
}
69+
if updates is not None:
70+
kwargs.update(updates)
71+
params = MVIInversionOptions.build(**kwargs)
6972
return params
7073

7174

@@ -214,3 +217,27 @@ def test_raise_on_rotated_negative_cell_size(tmp_path):
214217
msg = "Cannot convert negative cell sizes for rotated mesh."
215218
with pytest.raises(ValueError, match=msg):
216219
InversionMesh.ensure_cell_convention(mesh)
220+
221+
222+
def test_handle_grid2d(tmp_path):
223+
with Workspace(tmp_path / "test.geoh5") as ws:
224+
topo = Grid2D.create(
225+
ws,
226+
name="topography",
227+
u_cell_size=25.0,
228+
v_cell_size=25.0,
229+
u_count=16,
230+
v_count=16,
231+
origin=(-200.0, -200.0, 0.0),
232+
)
233+
elev = topo.add_data(
234+
{
235+
"elevation": {
236+
"values": np.zeros(len(topo.centroids)),
237+
}
238+
}
239+
)
240+
241+
updates = {"mesh": None, "topography_object": topo, "topography": elev}
242+
params = get_mvi_params(tmp_path, updates=updates)
243+
MVIInversionDriver(params) # Doesn't crash

0 commit comments

Comments
 (0)