Skip to content

Commit

Permalink
fix: 🐛 changes support barycentre to property
Browse files Browse the repository at this point in the history
barycentre() change to barycentre
  • Loading branch information
Lachlan Grose committed Feb 21, 2022
1 parent 2cbdab7 commit d2e1f0b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def add_fold_constraints(
]
# calculate the fold geometry for the elements barycentre
deformed_orientation, fold_axis, dgz = self.fold.get_deformed_orientation(
self.support.barycentre()
self.support.barycentre
)
element_idx = np.arange(self.support.n_elements)
np.random.shuffle(element_idx)
Expand Down
4 changes: 2 additions & 2 deletions LoopStructural/interpolators/piecewiselinear_interpolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ def add_constant_gradient(
"""
if direction_feature is not None:
direction_vector = direction_feature.evaluate_gradient(
self.support.barycentre()
self.support.barycentre
)
if direction_vector is not None:
if direction_vector.shape[0] == 1:
# if using a constant direction, tile array so it works for cg calc
direction_vector = np.tile(
direction_vector, (self.support.barycentre().shape[0], 1)
direction_vector, (self.support.barycentre.shape[0], 1)
)
if direction_vector is not None:
logger.info("Running constant gradient")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
self.regions["everywhere"] = np.ones(self.n_nodes).astype(bool)
self.name = name

@property
def barycentre(self):
return self.cell_centres(np.arange(self.n_elements))

Expand Down
6 changes: 3 additions & 3 deletions LoopStructural/interpolators/supports/_3d_structured_tetra.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def n_elements(self):
def n_cells(self):
return np.product(self.nsteps_cells)

def barycentre(self, elements=None):
@property
def barycentre(self):
"""
Return the barycentres of all tetrahedrons or of specified tetras using
global index
Expand All @@ -53,8 +54,7 @@ def barycentre(self, elements=None):
-------
"""
if elements is None:
elements = np.arange(0, self.ntetra)

tetra = self.get_elements()[elements]
barycentre = np.sum(self.nodes[tetra][:, :, :], axis=1) / 4.0
return barycentre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ def add_data_to_interpolator(
def install_gradient_constraint(self):
for g in self._orthogonal_features.values():
feature, w, region, step, B = g
vector = feature.evaluate_gradient(self.interpolator.support.barycentre())
vector = feature.evaluate_gradient(self.interpolator.support.barycentre)
norm = np.linalg.norm(vector, axis=1)

vector[norm > 0] /= norm[norm > 0, None]
element_idx = np.arange(self.interpolator.support.n_elements)
np.random.shuffle(element_idx)
self.interpolator.add_gradient_orthogonal_constraints(
self.interpolator.support.barycentre()[element_idx[::step], :],
self.interpolator.support.barycentre[element_idx[::step], :],
vector[element_idx[::step], :],
w=w,
B=B,
Expand Down
2 changes: 1 addition & 1 deletion examples/2_fold/plot_adding_folds_to_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
alpha=0.5)
viewer.add_data(stratigraphy)
# viewer.add_isosurface(fold_frame[1],colour='green',alpha=0.5)
# viewer.add_vector_field(fold_frame[0],locations=fold_frame[0].get_interpolator().support.barycentre())
# viewer.add_vector_field(fold_frame[0],locations=fold_frame[0].get_interpolator().support.barycentre)
# viewer.add_data(fold_frame[1])

# viewer.add_data(stratigraphy)
Expand Down
2 changes: 1 addition & 1 deletion examples/2_fold/plot_refolded_folds.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
viewer = LavaVuModelViewer(model)
viewer.add_isosurface(s0,nslices=10,paint_with=s0,cmap='tab20')
# viewer.add_data(s0)
# viewer.add_fold(s0['fold'],locations=s0['support'].barycentre()[::80])
# viewer.add_fold(s0['fold'],locations=s0['support'].barycentre[::80])
viewer.rotate([-69.11979675292969, 15.704944610595703, 6.00014591217041])
viewer.display()

24 changes: 12 additions & 12 deletions tests/unit_tests/interpolator/test_discrete_supports.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def test_evaluate_value():
grid = StructuredGrid()
assert (
np.sum(
grid.barycentre()[:, 0]
- grid.evaluate_value(grid.barycentre(), grid.nodes[:, 0])
grid.barycentre[:, 0]
- grid.evaluate_value(grid.barycentre, grid.nodes[:, 0])
)
== 0
)
Expand All @@ -37,12 +37,12 @@ def test_evaluate_value():
def test_evaluate_gradient():
grid = StructuredGrid()
# test by setting the scalar field to the y coordinate
vector = grid.evaluate_gradient(grid.barycentre(), grid.nodes[:, 1])
vector = grid.evaluate_gradient(grid.barycentre, grid.nodes[:, 1])
assert np.sum(vector - np.array([0, 1, 0])) == 0

# same test but for a bigger grid, making sure scaling for cell is ok
grid = StructuredGrid(step_vector=np.array([100, 100, 100]))
vector = grid.evaluate_gradient(grid.barycentre(), grid.nodes[:, 1])
vector = grid.evaluate_gradient(grid.barycentre, grid.nodes[:, 1])
assert np.sum(vector - np.array([0, 1, 0])) == 0


Expand All @@ -68,7 +68,7 @@ def test_evaluate_gradient2():

def test_get_element():
grid = StructuredGrid()
point = grid.barycentre()[[0], :]
point = grid.barycentre[[0], :]
idc, inside = grid.position_to_cell_corners(point)
bary = np.mean(grid.nodes[idc, :], axis=0)
assert np.sum(point - bary) == 0
Expand Down Expand Up @@ -108,19 +108,19 @@ def test_create_structured_grid2d_origin_nsteps():
def test_evaluate_value_2d():
grid = StructuredGrid2D()
grid.update_property('X',grid.nodes[:,0])
assert np.sum(grid.barycentre()[:,0] -
grid.evaluate_value(grid.barycentre(),'X')) ==0
assert np.sum(grid.barycentre[:,0] -
grid.evaluate_value(grid.barycentre,'X')) ==0

def test_evaluate_gradient_2d():
grid = StructuredGrid2D()
grid.update_property('Y',grid.nodes[:,1])
vector = np.mean(grid.evaluate_gradient(grid.barycentre(),'Y'),axis=0)
vector = np.mean(grid.evaluate_gradient(grid.barycentre,'Y'),axis=0)
# vector/=np.linalg.norm(vector)
assert np.sum(vector-np.array([0,grid.step_vector[1]])) == 0

def test_get_element_2d():
grid = StructuredGrid2D()
point = grid.barycentre()[[0],:]
point = grid.barycentre[[0],:]
idc, inside = grid.position_to_cell_corners(point)
bary = np.mean(grid.nodes[idc,:],axis=0)
assert np.sum(point-bary) == 0
Expand Down Expand Up @@ -169,8 +169,8 @@ def test_evaluate_value_tetmesh():
grid = TetMesh()
assert (
np.sum(
grid.barycentre()[:, 0]
- grid.evaluate_value(grid.barycentre(), grid.nodes[:, 0])
grid.barycentre[:, 0]
- grid.evaluate_value(grid.barycentre, grid.nodes[:, 0])
)
== 0
)
Expand All @@ -179,7 +179,7 @@ def test_evaluate_value_tetmesh():
def test_evaluate_gradient_tetmesh():
grid = TetMesh()
vector = np.mean(
grid.evaluate_gradient(grid.barycentre(), grid.nodes[:, 1]), axis=0
grid.evaluate_gradient(grid.barycentre, grid.nodes[:, 1]), axis=0
)
# vector/=np.linalg.norm(vector)
assert np.sum(vector - np.array([0, grid.step_vector[1], 0])) == 0
Expand Down

0 comments on commit d2e1f0b

Please sign in to comment.