|
7 | 7 | # (see LICENSE file at the root of this source code package). ' |
8 | 8 | # ' |
9 | 9 | # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
10 | | -import os |
11 | | -from pathlib import Path |
12 | | -from uuid import UUID |
13 | 10 |
|
14 | 11 | import numpy as np |
15 | 12 | from geoh5py import Workspace |
|
26 | 23 | from simpeg_drivers.plate_simulation.models.options import ModelOptions |
27 | 24 | from simpeg_drivers.plate_simulation.options import MeshOptions |
28 | 25 | from simpeg_drivers.potential_fields.gravity.options import GravityForwardOptions |
29 | | - |
30 | | -from . import get_survey, get_topography |
31 | | - |
32 | | - |
33 | | -# pylint: disable=duplicate-code |
34 | | - |
35 | | - |
36 | | -def get_simulation_group(workspace: Workspace, survey: ObjectBase, topography: Surface): |
37 | | - tem_inversion = SimPEGGroup.create(workspace) |
38 | | - options = TDEMForwardOptions.model_construct() |
39 | | - ifile = InputFile.read_ui_json(options.default_ui_json) |
40 | | - options_dict = ifile.ui_json |
41 | | - options_dict["inversion_type"] = "tdem" |
42 | | - options_dict["forward_only"] = True |
43 | | - options_dict["geoh5"] = str(workspace.h5file) |
44 | | - options_dict["topography_object"]["value"] = str(topography.uid) |
45 | | - options_dict["data_object"]["value"] = str(survey.uid) |
46 | | - options_dict["x_channel_bool"] = True |
47 | | - options_dict["y_channel_bool"] = True |
48 | | - options_dict["z_channel_bool"] = True |
49 | | - tem_inversion.options = options_dict |
50 | | - |
51 | | - return tem_inversion |
52 | | - |
53 | | - |
54 | | -def get_input_file(filepath: Path) -> InputFile: |
55 | | - with Workspace(filepath / "test.geoh5") as ws: |
56 | | - with Workspace( |
57 | | - assets_path() / "plate_simulation_demo.geoh5", mode="r" |
58 | | - ) as demo_workspace: |
59 | | - survey = demo_workspace.get_entity("Simulation rx")[0].copy( |
60 | | - parent=ws, copy_children=False |
61 | | - ) |
62 | | - topography = demo_workspace.get_entity("Topography")[0].copy(parent=ws) |
63 | | - mask = np.zeros(survey.n_vertices, dtype=bool) |
64 | | - mask[::10] = True |
65 | | - new_survey = survey.copy(vertices=survey.vertices[mask, :], cells=None) |
66 | | - |
67 | | - simulation = get_simulation_group(ws, new_survey, topography) |
68 | | - ifile = InputFile.read_ui_json( |
69 | | - assets_path() / "uijson" / "plate_simulation.ui.json", validate=False |
70 | | - ) |
71 | | - ifile.set_data_value("name", "test_tem_plate_simulation") |
72 | | - ifile.set_data_value("geoh5", ws) |
73 | | - ifile.set_data_value("simulation", simulation) |
74 | | - ifile.set_data_value("u_cell_size", 50.0) |
75 | | - ifile.set_data_value("v_cell_size", 50.0) |
76 | | - ifile.set_data_value("w_cell_size", 50.0) |
77 | | - ifile.set_data_value("depth_core", 600.0) |
78 | | - ifile.set_data_value("max_distance", 200.0) |
79 | | - ifile.set_data_value("padding_distance", 1000.0) |
80 | | - ifile.set_data_value("background", 2000.0) |
81 | | - ifile.set_data_value("overburden", 7500.0) |
82 | | - ifile.set_data_value("thickness", 50.0) |
83 | | - ifile.set_data_value("plate", 20.0) |
84 | | - ifile.set_data_value("width", 100.0) |
85 | | - ifile.set_data_value("strike_length", 1000.0) |
86 | | - ifile.set_data_value("dip_length", 300.0) |
87 | | - ifile.set_data_value("dip", 65.0) |
88 | | - ifile.set_data_value("dip_direction", 65.0) |
89 | | - ifile.set_data_value("number", 2) |
90 | | - ifile.set_data_value("spacing", 600.0) |
91 | | - ifile.set_data_value("relative_locations", True) |
92 | | - ifile.set_data_value("easting", 100.0) |
93 | | - ifile.set_data_value("northing", 100.0) |
94 | | - ifile.set_data_value("elevation", -100.0) |
95 | | - ifile.set_data_value("reference_surface", "overburden") |
96 | | - ifile.set_data_value("reference_type", "min") |
97 | | - |
98 | | - return ifile |
99 | | - |
100 | | - |
101 | | -def test_plate_simulation(tmp_path): |
102 | | - mon_dir = tmp_path / "monitoring" |
103 | | - mon_dir.mkdir(parents=True, exist_ok=True) |
104 | | - |
105 | | - ifile = get_input_file(tmp_path) |
106 | | - ifile.set_data_value("monitoring_directory", str(mon_dir)) |
107 | | - |
108 | | - ifile.write_ui_json("test_plate_simulation.ui.json", path=tmp_path) |
109 | | - result = PlateSimulationDriver.start( |
110 | | - Path(tmp_path / "test_plate_simulation.ui.json") |
111 | | - ) |
112 | | - with Workspace(result.out_group.options["geoh5"]) as ws: |
113 | | - out_group = ws.get_entity(UUID(result.out_group.options["out_group"]["value"]))[ |
114 | | - 0 |
115 | | - ] |
116 | | - data = next( |
117 | | - obj for obj in out_group.children if isinstance(obj, AirborneTEMReceivers) |
118 | | - ) |
119 | | - mesh = next(obj for obj in out_group.children if isinstance(obj, Octree)) |
120 | | - model = next(k for k in mesh.children if k.name == "starting_model") |
121 | | - assert data.property_groups is not None |
122 | | - assert len(data.property_groups) == 3 |
123 | | - assert all( |
124 | | - k.name in [f"Iteration_0_{i}" for i in "xyz"] for k in data.property_groups |
125 | | - ) |
126 | | - assert all( |
127 | | - k.properties is not None and len(k.properties) == 20 |
128 | | - for k in data.property_groups |
129 | | - ) |
130 | | - assert mesh.n_cells == 16263 |
131 | | - assert len(np.unique(model.values)) == 4 |
132 | | - assert all(k in np.unique(model.values) for k in [7500, 2000, 20]) |
133 | | - assert any(np.isnan(np.unique(model.values))) |
134 | | - |
135 | | - # Test the monitoring directory |
136 | | - files = [f for f in os.listdir(mon_dir) if f.endswith("geoh5")] |
137 | | - assert len(files) == 1 |
138 | | - with Workspace(mon_dir / files[0]) as ws: |
139 | | - fwr_group = ws.get_entity("Tdem Forward")[0] |
140 | | - |
141 | | - # Should only contain octree, files and a survey |
142 | | - assert len(fwr_group.children) == 5 |
| 26 | +from tests.testing_utils import setup_inversion_workspace |
143 | 27 |
|
144 | 28 |
|
145 | 29 | # pylint: disable=too-many-statements |
146 | 30 | def test_plate_simulation_params_from_input_file(tmp_path): |
147 | | - with Workspace(tmp_path / "test.geoh5") as ws: |
148 | | - topography = get_topography(ws) |
149 | | - survey = get_survey(ws, 10, 10) |
| 31 | + geoh5, mesh, model, survey, topography = setup_inversion_workspace( |
| 32 | + tmp_path, |
| 33 | + background=0.0, |
| 34 | + anomaly=0.0, |
| 35 | + n_electrodes=8, |
| 36 | + n_lines=8, |
| 37 | + inversion_type="gravity", |
| 38 | + flatten=False, |
| 39 | + ) |
150 | 40 |
|
| 41 | + with geoh5.open() as ws: |
151 | 42 | ifile = InputFile.read_ui_json( |
152 | 43 | assets_path() / "uijson" / "plate_simulation.ui.json", validate=False |
153 | 44 | ) |
@@ -197,44 +88,42 @@ def test_plate_simulation_params_from_input_file(tmp_path): |
197 | 88 | ifile.data["reference_surface"] = "topography" |
198 | 89 | ifile.data["reference_type"] = "mean" |
199 | 90 |
|
200 | | - params = PlateSimulationOptions.build(ifile) |
201 | | - assert isinstance(params.simulation, SimPEGGroup) |
202 | | - |
203 | | - simulation_parameters = params.simulation_parameters() |
204 | | - |
205 | | - assert simulation_parameters.inversion_type == "gravity" |
206 | | - assert simulation_parameters.forward_only |
207 | | - assert simulation_parameters.geoh5.h5file == ws.h5file |
208 | | - assert ( |
209 | | - simulation_parameters.active_cells.topography_object.uid == topography.uid |
210 | | - ) |
211 | | - assert simulation_parameters.data_object.uid == survey.uid |
212 | | - |
213 | | - assert isinstance(params.mesh, MeshOptions) |
214 | | - assert params.mesh.u_cell_size == 10.0 |
215 | | - assert params.mesh.v_cell_size == 10.0 |
216 | | - assert params.mesh.w_cell_size == 10.0 |
217 | | - assert params.mesh.depth_core == 400.0 |
218 | | - assert params.mesh.max_distance == 200.0 |
219 | | - assert params.mesh.padding_distance == 1500.0 |
220 | | - assert params.mesh.minimum_level == 8 |
221 | | - assert not params.mesh.diagonal_balance |
222 | | - |
223 | | - assert isinstance(params.model, ModelOptions) |
224 | | - assert params.model.plate_model.name == "test_gravity_plate_simulation" |
225 | | - assert params.model.background == 1000.0 |
226 | | - assert params.model.overburden_model.thickness == 50.0 |
227 | | - assert params.model.overburden_model.overburden == 5.0 |
228 | | - assert params.model.plate_model.plate == 2.0 |
229 | | - assert params.model.plate_model.width == 100.0 |
230 | | - assert params.model.plate_model.strike_length == 100.0 |
231 | | - assert params.model.plate_model.dip_length == 100.0 |
232 | | - assert params.model.plate_model.dip == 0.0 |
233 | | - assert params.model.plate_model.dip_direction == 0.0 |
234 | | - |
235 | | - assert params.model.plate_model.number == 9 |
236 | | - assert params.model.plate_model.spacing == 10.0 |
237 | | - assert params.model.plate_model.relative_locations |
238 | | - assert params.model.plate_model.easting == 10.0 |
239 | | - assert params.model.plate_model.northing == 10.0 |
240 | | - assert params.model.plate_model.elevation == -250.0 |
| 91 | + params = PlateSimulationOptions.build(ifile) |
| 92 | + assert isinstance(params.simulation, SimPEGGroup) |
| 93 | + |
| 94 | + simulation_parameters = params.simulation_parameters() |
| 95 | + |
| 96 | + assert simulation_parameters.inversion_type == "gravity" |
| 97 | + assert simulation_parameters.forward_only |
| 98 | + assert simulation_parameters.geoh5.h5file == ws.h5file |
| 99 | + assert simulation_parameters.active_cells.topography_object.uid == topography.uid |
| 100 | + assert simulation_parameters.data_object.uid == survey.uid |
| 101 | + |
| 102 | + assert isinstance(params.mesh, MeshOptions) |
| 103 | + assert params.mesh.u_cell_size == 10.0 |
| 104 | + assert params.mesh.v_cell_size == 10.0 |
| 105 | + assert params.mesh.w_cell_size == 10.0 |
| 106 | + assert params.mesh.depth_core == 400.0 |
| 107 | + assert params.mesh.max_distance == 200.0 |
| 108 | + assert params.mesh.padding_distance == 1500.0 |
| 109 | + assert params.mesh.minimum_level == 8 |
| 110 | + assert not params.mesh.diagonal_balance |
| 111 | + |
| 112 | + assert isinstance(params.model, ModelOptions) |
| 113 | + assert params.model.plate_model.name == "test_gravity_plate_simulation" |
| 114 | + assert params.model.background == 1000.0 |
| 115 | + assert params.model.overburden_model.thickness == 50.0 |
| 116 | + assert params.model.overburden_model.overburden == 5.0 |
| 117 | + assert params.model.plate_model.plate == 2.0 |
| 118 | + assert params.model.plate_model.width == 100.0 |
| 119 | + assert params.model.plate_model.strike_length == 100.0 |
| 120 | + assert params.model.plate_model.dip_length == 100.0 |
| 121 | + assert params.model.plate_model.dip == 0.0 |
| 122 | + assert params.model.plate_model.dip_direction == 0.0 |
| 123 | + |
| 124 | + assert params.model.plate_model.number == 9 |
| 125 | + assert params.model.plate_model.spacing == 10.0 |
| 126 | + assert params.model.plate_model.relative_locations |
| 127 | + assert params.model.plate_model.easting == 10.0 |
| 128 | + assert params.model.plate_model.northing == 10.0 |
| 129 | + assert params.model.plate_model.elevation == -250.0 |
0 commit comments