1313ELEMENTS_PER_EQUATION = 25
1414MAX_SURFACES = 10
1515SURFACE_STEP = 0.1
16+ MAX_EXTENT_FACTOR = 2.0
1617
1718
1819@dataclass
1920class DataSummary :
2021 """The measures of the data of one feature.
2122
2223 `spread` is 0 for parallel planes and 1 for orientations in all
23- directions. `spacing ` is the median distance from a data point to the
24- nearest other data point (0 if there are fewer than two points ).
24+ directions. `area ` is the map (XY) area of the box around the data points
25+ (0 if it is not known ).
2526 """
2627
2728 n_value : int = 0
2829 n_orientation : int = 0
2930 n_surfaces : int = 0
3031 spread : float = 0.0
31- spacing : float = 0.0
32+ area : float = 0.0
3233
3334
3435def _normals_from_strike_dip (strike , dip ) -> np .ndarray :
@@ -67,21 +68,14 @@ def orientation_spread(normals) -> float:
6768 return float (np .clip ((1.0 - largest ) / (2.0 / 3.0 ), 0.0 , 1.0 ))
6869
6970
70- def point_spacing (points ) -> float :
71- """Return the median distance from each point to the nearest other point.
72-
73- Points at the same location count as one point. Returns 0 for fewer than
74- two points.
75- """
76- from scipy .spatial import cKDTree
77-
78- points = np .asarray (points , dtype = float ).reshape (- 1 , 3 )
71+ def xy_area (points ) -> float :
72+ """Return the map (XY) area of the box around the points, or 0."""
73+ points = np .asarray (points , dtype = float ).reshape (- 1 , 2 )
7974 points = points [np .all (np .isfinite (points ), axis = 1 )]
80- points = np .unique (points , axis = 0 )
8175 if len (points ) < 2 :
8276 return 0.0
83- distances , _ = cKDTree ( points ). query ( points , k = 2 )
84- return float (np . median ( distances [:, 1 ]) )
77+ width , height = points . max ( axis = 0 ) - points . min ( axis = 0 )
78+ return float (width * height )
8579
8680
8781def _has (df , columns ) -> np .ndarray :
@@ -128,32 +122,33 @@ def summarise_data(df) -> DataSummary:
128122 n_surfaces = int (df .loc [_has (df , ['val' ]), 'val' ].nunique ())
129123 if 'interface' in df .columns and df ['interface' ].notna ().any ():
130124 n_surfaces = max (n_surfaces , int (df ['interface' ].nunique ()))
131- spacing = 0.0
132- if all ( column in df .columns for column in ( 'X' , 'Y' , 'Z' )) :
133- spacing = point_spacing (df [['X' , 'Y' , 'Z ' ]].to_numpy (dtype = float ))
125+ area = 0.0
126+ if 'X' in df .columns and 'Y' in df . columns :
127+ area = xy_area (df [['X' , 'Y' ]].to_numpy (dtype = float ))
134128 return DataSummary (
135129 n_value = int (value .sum ()),
136130 n_orientation = int (orientation .sum ()),
137131 n_surfaces = n_surfaces ,
138132 spread = spread ,
139- spacing = spacing ,
133+ area = area ,
140134 )
141135
142136
143- def suggest_nelements (summary : DataSummary , volume : float = 0.0 ) -> int :
137+ def suggest_nelements (summary : DataSummary , model_area : float = 0.0 ) -> int :
144138 """Return the number of elements for a feature with the data `summary`.
145139
146- From the number of data rows: elements = equations x 25 x surface factor
147- x spread factor, where an orientation is two equations, the surface
148- factor is 1 + 0.1 for each surface after the first (at most 10 surfaces)
149- and the spread factor is 1 + spread.
140+ elements = equations x 25 x surface factor x spread factor x extent
141+ factor, where an orientation is two equations, the surface factor is
142+ 1 + 0.1 for each surface after the first (at most 10 surfaces) and the
143+ spread factor is 1 + spread.
150144
151- From the model extent: if `volume` (the volume of the model box) and the
152- data spacing are known, elements = volume / spacing^3, so that an element
153- is not larger than the distance between the data points.
145+ The extent factor is the map area of the model (`model_area`) divided by
146+ the map area of the data, limited to 1 .. 2. When the data cover only a
147+ part of the model, the elements are spread over a larger area, so more
148+ elements keep the resolution near the data. The limit keeps the solve
149+ time low. The factor is 1 if an area is not known.
154150
155- The result is the larger of the two, rounded to 1 000 and limited to
156- 5 000 .. 250 000.
151+ The result is rounded to 1 000 and limited to 5 000 .. 250 000.
157152 """
158153 equations = summary .n_value + 2 * summary .n_orientation
159154 if equations <= 0 :
@@ -162,7 +157,7 @@ def suggest_nelements(summary: DataSummary, volume: float = 0.0) -> int:
162157 surface_factor = 1 + SURFACE_STEP * (surfaces - 1 )
163158 spread_factor = 1 + min (max (summary .spread , 0.0 ), 1.0 )
164159 elements = equations * ELEMENTS_PER_EQUATION * surface_factor * spread_factor
165- if volume > 0 and summary .spacing > 0 :
166- elements = max (elements , min ( volume / summary .spacing ** 3 , MAX_NELEMENTS ) )
160+ if model_area > 0 and summary .area > 0 :
161+ elements *= min ( max (model_area / summary .area , 1.0 ), MAX_EXTENT_FACTOR )
167162 elements = int (round (elements / ROUND_TO )) * ROUND_TO
168163 return int (min (max (elements , MIN_NELEMENTS ), MAX_NELEMENTS ))
0 commit comments