6
6
from typing import Optional
7
7
8
8
from gempy_engine .core .data .centered_grid import CenteredGrid
9
+ from gempy_engine .core .data .options import EvaluationOptions
9
10
from gempy_engine .core .data .transforms import Transform
10
11
from .grid_modules import RegularGrid , CustomGrid , Sections
11
12
from .grid_modules .topography import Topography
@@ -23,7 +24,6 @@ class GridTypes(enum.Flag):
23
24
NONE = 2 ** 10
24
25
25
26
# ? What should we do with the extent?
26
- _extent : Optional [np .ndarray ] # * Model extent should be cross grid
27
27
28
28
_octree_grid : Optional [RegularGrid ] = None
29
29
_dense_grid : Optional [RegularGrid ] = None
@@ -41,11 +41,25 @@ class GridTypes(enum.Flag):
41
41
_octree_levels : int = - 1
42
42
43
43
def __init__ (self , extent = None , resolution = None ):
44
- self .extent = extent
45
44
# Init basic grid empty
46
45
if extent is not None and resolution is not None :
47
46
self .dense_grid = RegularGrid (extent , resolution )
48
47
48
+ @classmethod
49
+ def init_octree_grid (cls , extent , octree_levels ):
50
+ grid = cls ()
51
+ grid ._octree_grid = RegularGrid (
52
+ extent = extent ,
53
+ resolution = np .array ([2 ** octree_levels ] * 3 ),
54
+ )
55
+ grid .active_grids |= grid .GridTypes .OCTREE
56
+ grid ._update_values ()
57
+ return grid
58
+
59
+ @classmethod
60
+ def init_dense_grid (cls , extent , resolution ):
61
+ return cls (extent , resolution )
62
+
49
63
def __str__ (self ):
50
64
active_grid_types_str = [g_type for g_type in self .GridTypes if self .active_grids & g_type ]
51
65
@@ -54,44 +68,32 @@ def __str__(self):
54
68
grid_summary_str = "\n " .join (grid_summary )
55
69
return f"Grid Object:\n { grid_summary_str } "
56
70
57
-
58
-
59
71
@property
60
72
def transform (self ) -> Transform :
61
- if self ._transform is None :
62
- if self .dense_grid is not None :
63
- return self .dense_grid .transform
64
- elif self .octree_grid is not None :
65
- return self .octree_grid .transform
66
- else :
67
- return Transform .init_neutral ()
68
- return self ._transform
73
+ if self .dense_grid is not None :
74
+ return self .dense_grid .transform
75
+ elif self .octree_grid is not None :
76
+ return self .octree_grid .transform
77
+ else :
78
+ return Transform .init_neutral ()
69
79
70
80
@transform .setter
71
81
def transform (self , value : Transform ):
72
82
self ._transform = value
73
-
83
+
74
84
@property
75
85
def extent (self ):
76
- if self ._extent is None :
77
- # Try to get the extent from the dense or octree grid if those are also none raise an error
78
- if self .dense_grid is not None :
79
- return self .dense_grid .extent
80
- elif self .octree_grid is not None :
81
- return self .octree_grid .extent
82
- else :
83
- raise AttributeError ('Extent is not defined' )
86
+ if self .dense_grid is not None :
87
+ return self .dense_grid .extent
88
+ elif self .octree_grid is not None :
89
+ return self .octree_grid .extent
84
90
else :
85
- return self . _extent
91
+ raise AttributeError ( 'Extent is not defined' )
86
92
87
- @extent .setter
88
- def extent (self , value ):
89
- self ._extent = value
90
-
91
93
@property
92
94
def corner_min (self ):
93
95
return self .extent [::2 ]
94
-
96
+
95
97
@property
96
98
def corner_max (self ):
97
99
return self .extent [1 ::2 ]
@@ -135,6 +137,18 @@ def octree_grid(self):
135
137
136
138
@octree_grid .setter
137
139
def octree_grid (self , value ):
140
+ raise AttributeError ('Octree grid is not allowed to be set directly. Use init_octree_grid instead' )
141
+
142
+ def set_octree_grid (self , value : RegularGrid , evaluation_options : EvaluationOptions ):
143
+ regular_grid_resolution = value .resolution
144
+ # Check all directions has the same res
145
+ if not np .all (regular_grid_resolution == regular_grid_resolution [0 ]):
146
+ raise AttributeError ('Octree resolution must be isotropic' )
147
+ octree_levels = int (np .log2 (regular_grid_resolution [0 ]))
148
+ # Check if octrree levels are the same
149
+ if octree_levels != evaluation_options .number_octree_levels :
150
+ raise AttributeError ('Regular grid resolution does not match octree levels. Resolution must be 2^n' )
151
+
138
152
self ._octree_grid = value
139
153
self .active_grids |= self .GridTypes .OCTREE
140
154
self ._update_values ()
@@ -181,8 +195,10 @@ def centered_grid(self, value):
181
195
182
196
@property
183
197
def regular_grid (self ):
184
- warnings .warn ('This property is deprecated. Use the dense_grid or octree_grid instead' , DeprecationWarning )
185
- if self .dense_grid is not None and self .octree_grid is not None :
198
+ dense_grid_exists_and_active = self .dense_grid is not None and self .GridTypes .DENSE in self .active_grids
199
+ octree_grid_exists_and_active = self .octree_grid is not None and self .GridTypes .OCTREE in self .active_grids
200
+
201
+ if dense_grid_exists_and_active and octree_grid_exists_and_active :
186
202
raise AttributeError ('Both dense_grid and octree_grid are active. This is not possible.' )
187
203
elif self .dense_grid is not None :
188
204
return self .dense_grid
@@ -191,41 +207,27 @@ def regular_grid(self):
191
207
else :
192
208
return None
193
209
194
- @regular_grid .setter
195
- def regular_grid (self , value ):
196
- raise AttributeError ('This property is deprecated. Use the dense_grid or octree_grid instead' )
197
-
198
- @property
199
- def octree_levels (self ):
200
- return self ._octree_levels
201
-
202
- @property
203
- def octree_levels (self ):
204
- return self ._octree_levels
205
-
206
- @octree_levels .setter
207
- def octree_levels (self , value ):
208
- self ._octree_levels = value
209
- self .octree_grid = RegularGrid (
210
- extent = self .extent ,
211
- resolution = np .array ([2 ** value ] * 3 ),
212
- )
213
- self .active_grids |= self .GridTypes .OCTREE
214
-
210
+ # noinspection t
215
211
def _update_values (self ):
216
212
values = []
217
213
218
214
if self .GridTypes .OCTREE in self .active_grids :
215
+ if self .octree_grid is None : raise AttributeError ('Octree grid is active but not defined' )
219
216
values .append (self .octree_grid .values )
220
217
if self .GridTypes .DENSE in self .active_grids :
218
+ if self .dense_grid is None : raise AttributeError ('Dense grid is active but not defined' )
221
219
values .append (self .dense_grid .values )
222
220
if self .GridTypes .CUSTOM in self .active_grids :
221
+ if self .custom_grid is None : raise AttributeError ('Custom grid is active but not defined' )
223
222
values .append (self .custom_grid .values )
224
223
if self .GridTypes .TOPOGRAPHY in self .active_grids :
224
+ if self .topography is None : raise AttributeError ('Topography grid is active but not defined' )
225
225
values .append (self .topography .values )
226
226
if self .GridTypes .SECTIONS in self .active_grids :
227
+ if self .sections is None : raise AttributeError ('Sections grid is active but not defined' )
227
228
values .append (self .sections .values )
228
229
if self .GridTypes .CENTERED in self .active_grids :
230
+ if self .centered_grid is None : raise AttributeError ('Centered grid is active but not defined' )
229
231
values .append (self .centered_grid .values )
230
232
231
233
self .values = np .concatenate (values )
0 commit comments