66
77import numpy as np
88
9+ from .base_structured_3d_support import BaseStructuredSupport
10+
11+
912from LoopStructural .utils import getLogger
1013logger = getLogger (__name__ )
1114
1215
13- class StructuredGrid :
16+ class StructuredGrid ( BaseStructuredSupport ) :
1417 """
1518
1619 """
1720 def __init__ (self ,
1821 origin = np .zeros (3 ),
1922 nsteps = np .array ([10 , 10 , 10 ]),
2023 step_vector = np .ones (3 ),
24+ name = None
2125 ):
2226 """
2327
@@ -27,57 +31,15 @@ def __init__(self,
2731 nsteps - 3d list or numpy array of ints
2832 step_vector - 3d list or numpy array of int
2933 """
30-
31- self .nsteps = np .array (nsteps )
32- self .step_vector = np .array (step_vector )
33- self .origin = np .array (origin )
34- self .maximum = origin + self .nsteps * self .step_vector
35-
36- # self.nsteps+=1
37- self .n_nodes = self .nsteps [0 ] * self .nsteps [1 ] * self .nsteps [2 ]
38- # self.nsteps-=1
39- self .dim = 3
40- self .nsteps_cells = self .nsteps - 1
41- self .n_cell_x = self .nsteps [0 ] - 1
42- self .n_cell_y = self .nsteps [1 ] - 1
43- self .n_cell_z = self .nsteps [2 ] - 1
44- self .properties = {}
45- self .n_elements = self .n_cell_x * self .n_cell_y * self .n_cell_z
46-
47- # calculate the node positions using numpy (this should probably not
48- # be stored as it defeats
49- # the purpose of a structured grid
50-
51- # self.barycentre = self.cell_centres(np.arange(self.n_elements))
52-
34+ BaseStructuredSupport .__init__ (self ,origin ,nsteps ,step_vector )
5335 self .regions = {}
5436 self .regions ['everywhere' ] = np .ones (self .n_nodes ).astype (bool )
55-
56-
57- @property
58- def nodes (self ):
59- max = self .origin + self .nsteps_cells * self .step_vector
60- x = np .linspace (self .origin [0 ], max [0 ], self .nsteps [0 ])
61- y = np .linspace (self .origin [1 ], max [1 ], self .nsteps [1 ])
62- z = np .linspace (self .origin [2 ], max [2 ], self .nsteps [2 ])
63- xx , yy , zz = np .meshgrid (x , y , z , indexing = 'ij' )
64- return np .array ([xx .flatten (order = 'F' ), yy .flatten (order = 'F' ),
65- zz .flatten (order = 'F' )]).T
37+ self .name = name
6638
6739 def barycentre (self ):
6840 return self .cell_centres (np .arange (self .n_elements ))
6941
70- # @property
71- # def barycentre(self):
72- # return self.cell_centres(np.arange(self.n_elements))
7342
74- def print_geometry (self ):
75- print ('Origin: %f %f %f' % (
76- self .origin [0 ], self .origin [1 ], self .origin [2 ]))
77- print ('Cell size: %f %f %f' % (
78- self .step_vector [0 ], self .step_vector [1 ], self .step_vector [2 ]))
79- max = self .origin + self .nsteps_cells * self .step_vector
80- print ('Max extent: %f %f %f' % (max [0 ], max [1 ], max [2 ]))
8143
8244 def update_property (self , propertyname , values ):
8345 """[summary]
@@ -373,7 +335,7 @@ def position_to_cell_corners(self, pos):
373335 globalidx [~ inside ] = - 1
374336 return globalidx , inside
375337
376- def evaluate_value (self , evaluation_points , property_name ):
338+ def evaluate_value (self , evaluation_points , property_array ):
377339 """
378340 Evaluate the value of of the property at the locations.
379341 Trilinear interpolation dot corner values
@@ -387,25 +349,33 @@ def evaluate_value(self, evaluation_points, property_name):
387349 -------
388350
389351 """
352+ if property_array .shape [0 ] != self .n_nodes :
353+ logger .error ("Property array does not match grid" )
354+ raise BaseException
390355 idc , inside = self .position_to_cell_corners (evaluation_points )
391356 v = np .zeros (idc .shape )
392357 v [:, :] = np .nan
393358
394359 v [inside , :] = self .position_to_dof_coefs (
395360 evaluation_points [inside , :]).T
396- v [inside , :] *= self .properties [property_name ][idc [inside , :]]
361+
362+ v [inside , :] *= property_array [idc [inside , :]]
363+
397364 return np .sum (v , axis = 1 )
398365
399- def evaluate_gradient (self , evaluation_points , property_name ):
366+ def evaluate_gradient (self , evaluation_points , property_array ):
367+ if property_array .shape [0 ] != self .n_nodes :
368+ logger .error ("Property array does not match grid" )
369+ raise BaseException
400370 idc , inside = self .position_to_cell_corners (evaluation_points )
401371 T = np .zeros ((idc .shape [0 ], 3 , 8 ))
402372 T [inside , :, :] = self .calcul_T (evaluation_points [inside , :])
403373 # indices = np.array([self.position_to_cell_index(evaluation_points)])
404374 # idc = self.global_indicies(indices.swapaxes(0,1))
405375 # print(idc)
406- T [inside , 0 , :] *= self . properties [ property_name ] [idc [inside , :]]
407- T [inside , 1 , :] *= self . properties [ property_name ] [idc [inside , :]]
408- T [inside , 2 , :] *= self . properties [ property_name ] [idc [inside , :]]
376+ T [inside , 0 , :] *= property_array [idc [inside , :]]
377+ T [inside , 1 , :] *= property_array [idc [inside , :]]
378+ T [inside , 2 , :] *= property_array [idc [inside , :]]
409379 return np .array (
410380 [np .sum (T [:, 0 , :], axis = 1 ), np .sum (T [:, 1 , :], axis = 1 ) ,
411381 np .sum (T [:, 2 , :], axis = 1 ) ]).T
0 commit comments