|
13 | 13 |
|
14 | 14 | import numpy as np |
15 | 15 | from discretize import TreeMesh |
16 | | -from geoapps_utils.utils.numerical import traveling_salesman |
17 | 16 | from geoh5py import Workspace |
18 | | -from geoh5py.objects import PotentialElectrode |
| 17 | +from geoh5py.data import IntegerData |
| 18 | +from geoh5py.objects import DrapeModel, PotentialElectrode |
| 19 | +from geoh5py.shared.merging.drape_model import DrapeModelMerger |
19 | 20 | from scipy.sparse import csgraph, csr_matrix |
20 | 21 | from scipy.spatial import cKDTree |
21 | 22 | from simpeg.survey import BaseSurvey |
22 | 23 |
|
| 24 | +from simpeg_drivers.options import ( |
| 25 | + DrapeModelOptions, |
| 26 | +) |
| 27 | +from simpeg_drivers.utils.utils import get_drape_model |
| 28 | + |
23 | 29 |
|
24 | 30 | def station_spacing( |
25 | 31 | locations: np.ndarray, |
@@ -63,47 +69,6 @@ def counter_clockwise_sort(segments: np.ndarray, vertices: np.ndarray) -> np.nda |
63 | 69 | return segments |
64 | 70 |
|
65 | 71 |
|
66 | | -def compute_alongline_distance(points: np.ndarray, ordered: bool = True): |
67 | | - """ |
68 | | - Convert from cartesian (x, y, values) points to (distance, values) locations. |
69 | | -
|
70 | | - :param: points: Cartesian coordinates of points lying either roughly within a |
71 | | - plane or a line. |
72 | | - """ |
73 | | - if not ordered: |
74 | | - order = traveling_salesman(points) |
75 | | - points = points[order, :] |
76 | | - |
77 | | - distances = np.cumsum( |
78 | | - np.r_[0, np.linalg.norm(np.diff(points[:, :2], axis=0), axis=1)] |
79 | | - ) |
80 | | - if points.shape[1] > 2: |
81 | | - distances = np.c_[distances, points[:, 2:]] |
82 | | - |
83 | | - return distances |
84 | | - |
85 | | - |
86 | | -def copy_potentials_from_mask( |
87 | | - workspace: Workspace, survey: PotentialElectrode, cell_mask: np.ndarray |
88 | | -): |
89 | | - """ |
90 | | - Returns a survey containing data from a single line. |
91 | | -
|
92 | | - :param workspace: geoh5py workspace containing a valid DCIP survey. |
93 | | - :param survey: PotentialElectrode object. |
94 | | - :param cell_mask: Boolean array of M-N pairs to include in the new survey. |
95 | | - """ |
96 | | - |
97 | | - if not np.any(cell_mask): |
98 | | - raise ValueError("No cells found in the mask.") |
99 | | - |
100 | | - active_poles = np.zeros(survey.n_vertices, dtype=bool) |
101 | | - active_poles[survey.cells[cell_mask, :].ravel()] = True |
102 | | - potentials = survey.copy(parent=workspace, mask=active_poles, cell_mask=cell_mask) |
103 | | - |
104 | | - return potentials |
105 | | - |
106 | | - |
107 | 72 | def get_intersecting_cells(locations: np.ndarray, mesh: TreeMesh) -> np.ndarray: |
108 | 73 | """ |
109 | 74 | Find cells that intersect with a set of segments. |
@@ -166,7 +131,9 @@ def get_parts_from_electrodes(survey: PotentialElectrode) -> np.ndarray: |
166 | 131 | ) |
167 | 132 |
|
168 | 133 | connections = csgraph.connected_components(edge_array)[1] |
169 | | - return connections[survey.cells[:, 0]] |
| 134 | + parts = connections[survey.cells[:, 0]] |
| 135 | + _, u_part = np.unique(parts, return_inverse=True) |
| 136 | + return u_part |
170 | 137 |
|
171 | 138 |
|
172 | 139 | def compute_em_projections(locations, simulation): |
@@ -206,3 +173,102 @@ def compute_dc_projections(locations, cells, simulation): |
206 | 173 | proj_mn -= projection[cells[indices, 1], :] |
207 | 174 |
|
208 | 175 | receiver.spatialP = proj_mn # pylint: disable=protected-access |
| 176 | + |
| 177 | + |
| 178 | +def create_mesh_by_line_id( |
| 179 | + workspace: Workspace, |
| 180 | + line_ids: IntegerData, |
| 181 | + drape_options: DrapeModelOptions, |
| 182 | + **object_kwargs, |
| 183 | +) -> DrapeModel: |
| 184 | + """ |
| 185 | + Create a drape mesh for the dc resistivity survey lines. |
| 186 | +
|
| 187 | + :param workspace: Workspace to create the drape mesh in. |
| 188 | + :param line_ids: IntegerData object containing the line IDs for each vertex. |
| 189 | + :param drape_options: DrapeModelOptions containing the parameters for the drape mesh |
| 190 | + :param object_kwargs: Additional keyword arguments to pass to the DrapeModelMerger.create_object method. |
| 191 | +
|
| 192 | + :return: A DrapeModel object containing the merged drape mesh for all survey lines. |
| 193 | + """ |
| 194 | + drape_models = [] |
| 195 | + temp_work = Workspace() |
| 196 | + |
| 197 | + relief = get_max_line_relief(line_ids, drape_options.v_cell_size) |
| 198 | + |
| 199 | + for line_id in np.unique(line_ids.values): |
| 200 | + poles = get_poles_by_line_id(line_ids, line_id) |
| 201 | + poles = np.unique(poles, axis=0) |
| 202 | + poles = normalize_vertically(poles, relief) |
| 203 | + |
| 204 | + drape_model = get_drape_model( |
| 205 | + temp_work, |
| 206 | + poles, |
| 207 | + [ |
| 208 | + drape_options.u_cell_size, |
| 209 | + drape_options.v_cell_size, |
| 210 | + ], |
| 211 | + drape_options.depth_core, |
| 212 | + [drape_options.horizontal_padding] * 2 |
| 213 | + + [drape_options.vertical_padding, 1], |
| 214 | + drape_options.expansion_factor, |
| 215 | + ) |
| 216 | + drape_models.append(drape_model) |
| 217 | + |
| 218 | + entity = DrapeModelMerger.create_object(workspace, drape_models, **object_kwargs) |
| 219 | + |
| 220 | + return entity |
| 221 | + |
| 222 | + |
| 223 | +def get_max_line_relief(line_ids: IntegerData, z_cell_size: float) -> float: |
| 224 | + """ |
| 225 | + Get the maximum relief across all survey lines, rounded to the nearest cell thickness. |
| 226 | +
|
| 227 | + :param line_ids: IntegerData object containing the line IDs for each vertex. |
| 228 | + :param z_cell_size: Cell size in the vertical direction for the drape mesh. |
| 229 | + """ |
| 230 | + max_relief = 0 |
| 231 | + for line_id in np.unique(line_ids.values): |
| 232 | + poles = get_poles_by_line_id(line_ids, line_id) |
| 233 | + max_relief = np.maximum(poles[:, 2].max() - poles[:, 2].min(), max_relief) |
| 234 | + |
| 235 | + return (max_relief // z_cell_size + 2) * z_cell_size |
| 236 | + |
| 237 | + |
| 238 | +def get_poles_by_line_id(line_ids: IntegerData, uid: int) -> np.ndarray: |
| 239 | + """Get the vertices associated with a given line ID.""" |
| 240 | + mn_mask = line_ids.values == uid |
| 241 | + |
| 242 | + unique_tx = np.unique(line_ids.parent.ab_cell_id.values[mn_mask]) |
| 243 | + |
| 244 | + ab_mask = np.isin(line_ids.parent.complement.ab_cell_id.values, unique_tx) |
| 245 | + |
| 246 | + return np.vstack( |
| 247 | + [ |
| 248 | + line_ids.parent.vertices[line_ids.parent.cells[mn_mask].flatten()], |
| 249 | + line_ids.parent.current_electrodes.vertices[ |
| 250 | + line_ids.parent.current_electrodes.cells[ab_mask].flatten() |
| 251 | + ], |
| 252 | + ] |
| 253 | + ) |
| 254 | + |
| 255 | + |
| 256 | +def normalize_vertically(poles: np.ndarray, relief: float) -> np.ndarray: |
| 257 | + """ |
| 258 | + Given a set of pole locations, normalize the vertical component to the maximum relief across all lines. |
| 259 | +
|
| 260 | + This ensures that the drape mesh has uniform vertical discretization across all survey lines. |
| 261 | +
|
| 262 | + :param poles: Array of pole locations to normalize. |
| 263 | + :param relief: Maximum relief across all survey lines, rounded to the nearest cell thickness. |
| 264 | +
|
| 265 | + :return: Array of pole locations with normalized vertical component. |
| 266 | + """ |
| 267 | + min_poles_z = poles[:, 2].min() |
| 268 | + poles[:, 2] -= min_poles_z |
| 269 | + poles[:, 2] *= relief / np.maximum(poles[:, 2].max(), 1e-3) |
| 270 | + |
| 271 | + # Shift back vertically |
| 272 | + poles[:, 2] += min_poles_z |
| 273 | + |
| 274 | + return poles |
0 commit comments