Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[WIP] Dealing with custom grids
  • Loading branch information
Leguark committed May 25, 2025
commit 39ac3de015286d45327b7c1b75630a2e1016da96
7 changes: 4 additions & 3 deletions gempy/API/compute_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
dotenv.load_dotenv()


def compute_model(gempy_model: GeoModel, engine_config: Optional[GemPyEngineConfig] = None) -> Solutions:
def compute_model(gempy_model: GeoModel, engine_config: Optional[GemPyEngineConfig] = None,
**kwargs) -> Solutions:
"""
Compute the geological model given the provided GemPy model.

Expand Down Expand Up @@ -61,7 +62,7 @@ def compute_model(gempy_model: GeoModel, engine_config: Optional[GemPyEngineConf
case _:
raise ValueError(f'Backend {engine_config} not supported')

if os.getenv("VALIDATE_SERIALIZATION", False):
if os.getenv("VALIDATE_SERIALIZATION", False) and kwargs.get("validate_serialization", True):
from ..modules.serialization.save_load import save_model
import tempfile
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as tmp:
Expand Down Expand Up @@ -90,7 +91,7 @@ def compute_model_at(gempy_model: GeoModel, at: np.ndarray,
xyz_coord=at
)

sol = compute_model(gempy_model, engine_config)
sol = compute_model(gempy_model, engine_config, validate_serialization=False)
return sol.raw_arrays.custom


Expand Down
2 changes: 1 addition & 1 deletion gempy/API/grid_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def set_topography_from_file(grid: Grid, filepath: str, crop_to_extent: Union[Se


def set_custom_grid(grid: Grid, xyz_coord: np.ndarray):
custom_grid = CustomGrid(xyx_coords=xyz_coord)
custom_grid = CustomGrid(values=xyz_coord)
grid.custom_grid = custom_grid

set_active_grid(grid, [Grid.GridTypes.CUSTOM])
Expand Down
34 changes: 15 additions & 19 deletions gempy/core/data/grid_modules/grid_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def plot_rotation(regular_grid, pivot, point_x_axis, point_y_axis):
plt.show()


@dataclasses.dataclass
class Sections:
"""
Object that creates a grid of cross sections between two points.
Expand Down Expand Up @@ -364,6 +365,7 @@ def get_section_grid(self, section_name: str):
return self.values[l0:l1]


@dataclasses.dataclass
class CustomGrid:
"""Object that contains arbitrary XYZ coordinates.

Expand All @@ -374,26 +376,20 @@ class CustomGrid:
values (np.ndarray): XYZ coordinates
"""

def __init__(self, xyx_coords: np.ndarray):
self.values = np.zeros((0, 3))
self.set_custom_grid(xyx_coords)

def set_custom_grid(self, custom_grid: np.ndarray):
"""
Give the coordinates of an external generated grid

Args:
custom_grid (numpy.ndarray like): XYZ (in columns) of the desired coordinates

Returns:
numpy.ndarray: Unraveled 3D numpy array where every row correspond to the xyz coordinates of a regular
grid
"""
custom_grid = np.atleast_2d(custom_grid)
values: np.ndarray = Field(
exclude=True,
default_factory=lambda: np.zeros((0, 3)),
repr=False
)


def __post_init__(self):
custom_grid = np.atleast_2d(self.values)
assert type(custom_grid) is np.ndarray and custom_grid.shape[1] == 3, \
'The shape of new grid must be (n,3) where n is the number of' \
' points of the grid'

self.values = custom_grid
self.length = self.values.shape[0]
return self.values

@property
def length(self):
return self.values.shape[0]
8 changes: 5 additions & 3 deletions gempy/modules/serialization/save_load.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from typing import Literal

import warnings
Expand Down Expand Up @@ -142,15 +144,15 @@ def _validate_serialization(original_model, model_deserialized):
o_b = hash(model_deserialized.structural_frame.orientations_copy.data.tobytes())
assert a == b, "Hashes for surface points are not equal"
assert o_a == o_b, "Hashes for orientations are not equal"
original_model___str__ = original_model.__str__()
deserialized___str__ = model_deserialized.__str__()
original_model___str__ = re.sub(r'\s+', ' ', original_model.__str__())
deserialized___str__ = re.sub(r'\s+', ' ', model_deserialized.__str__())
if original_model___str__ != deserialized___str__:
# Find first char that is not the same
for i in range(min(len(original_model___str__), len(deserialized___str__))):
if original_model___str__[i] != deserialized___str__[i]:
break
print(f"First difference at index {i}:")
i1 = 50
i1 = 10
print(f"Original: {original_model___str__[i - i1:i + i1]}")
print(f"Deserialized: {deserialized___str__[i - i1:i + i1]}")

Expand Down
9 changes: 8 additions & 1 deletion test/test_modules/test_grids/test_custom_grid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

from gempy.modules.serialization.save_load import verify_model_serialization
from test.conftest import TEST_SPEED, TestSpeed
import gempy as gp
from gempy.core.data.enumerators import ExampleModel
Expand Down Expand Up @@ -32,7 +33,13 @@ def test_custom_grid():
xyz_coord=xyz_coord
)

sol: gp.data.Solutions = gp.compute_model(geo_model)
verify_model_serialization(
model=geo_model,
verify_moment="after",
file_name=f"verify/{geo_model.meta.name}"
)

sol: gp.data.Solutions = gp.compute_model(geo_model, validate_serialization=False)
np.testing.assert_array_equal(
sol.raw_arrays.custom,
np.array([3., 3., 3., 3., 1., 1., 1., 1.])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"meta": {
"name": "fold",
"creation_date": "<DATE_IGNORED>",
"last_modification_date": null,
"owner": null
},
"structural_frame": {
"structural_groups": [
{
"name": "Strat_Series",
"elements": [
{
"name": "rock2",
"is_active": true,
"_color": "#9f0052",
"surface_points": {
"name_id_map": {
"rock1": 67239155,
"rock2": 117776925
},
"_model_transform": null
},
"orientations": {
"name_id_map": {
"rock1": 67239155,
"rock2": 117776925
},
"_model_transform": null
},
"scalar_field_at_interface": null,
"_id": 117776925
},
{
"name": "rock1",
"is_active": true,
"_color": "#015482",
"surface_points": {
"name_id_map": {
"rock1": 67239155,
"rock2": 117776925
},
"_model_transform": null
},
"orientations": {
"name_id_map": {
"rock1": 67239155,
"rock2": 117776925
},
"_model_transform": null
},
"scalar_field_at_interface": null,
"_id": 67239155
}
],
"structural_relation": 1,
"fault_relations": null,
"faults_input_data": null,
"solution": null
}
],
"is_dirty": true,
"basement_color": "#ffbe00",
"binary_meta_data": {
"sp_binary_length": 1296
}
},
"grid": {
"_octree_grid": {
"resolution": [
32,
32,
32
],
"extent": [
0.0,
1000.0,
0.0,
1000.0,
0.0,
1000.0
],
"_transform": null
},
"_dense_grid": null,
"_custom_grid": {},
"_topography": null,
"_sections": null,
"_centered_grid": null,
"_transform": null,
"_octree_levels": -1,
"active_grids": 1029
},
"geophysics_input": null,
"input_transform": {
"position": [
-500.0,
-500.0,
-510.0
],
"rotation": [
0.0,
0.0,
0.0
],
"scale": [
0.0005,
0.0005,
0.0005
],
"_is_default_transform": false,
"_cached_pivot": null
},
"_interpolation_options": {
"kernel_options": {
"range": 1.7,
"c_o": 10.0,
"uni_degree": 1,
"i_res": 4.0,
"gi_res": 2.0,
"number_dimensions": 3,
"kernel_function": "cubic",
"kernel_solver": 1,
"compute_condition_number": false,
"optimizing_condition_number": false,
"condition_number": null
},
"evaluation_options": {
"_number_octree_levels": 2,
"_number_octree_levels_surface": 4,
"octree_curvature_threshold": -1.0,
"octree_error_threshold": 1.0,
"octree_min_level": 2,
"mesh_extraction": true,
"mesh_extraction_masking_options": 3,
"mesh_extraction_fancy": true,
"evaluation_chunk_size": 500000,
"compute_scalar_gradient": false,
"verbose": false
},
"debug": true,
"cache_mode": 3,
"cache_model_name": "fold",
"block_solutions_type": 1,
"sigmoid_slope": 5000000,
"debug_water_tight": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"_octree_levels": -1,
"active_grids": 1026
},
"geophysics_input": null,
"input_transform": {
"position": [
-500.0,
Expand Down