Skip to content

Commit cbe7bae

Browse files
authored
Avoid the vector method (#166)
Towards #164 Similarly as in Animate and Goalie.
1 parent 7fb9bf3 commit cbe7bae

File tree

7 files changed

+16
-14
lines changed

7 files changed

+16
-14
lines changed

movement/laplacian_smoothing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, mesh, timestep, **kwargs):
4444
super().__init__(mesh, **kwargs)
4545
assert timestep > 0.0
4646
self.dt = timestep
47-
dim = self.mesh.topological_dimension()
47+
dim = self.mesh.topological_dimension
4848
self.displacement = np.zeros((self.mesh.num_vertices(), dim))
4949

5050
def _create_functions(self):

movement/monge_ampere.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def __init__(self, mesh, monitor_function, phi_init=None, H_init=None, **kwargs)
579579
:kwarg dtol: divergence tolerance for the residual
580580
:type dtol: :class:`float`
581581
"""
582-
if mesh.topological_dimension() == 1:
582+
if mesh.topological_dimension == 1:
583583
raise NotImplementedError(
584584
"1D case not implemented for quasi-Newton method."
585585
)

movement/mover.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import firedrake.exceptions as fexc
66
import numpy as np
77
import ufl
8+
from animate.utility import function_data_max, function_data_min, function_data_sum
89
from firedrake.cython.dmcommon import create_section
910
from firedrake.petsc import PETSc
1011

@@ -50,8 +51,8 @@ def __init__(
5051
stacklevel=1,
5152
)
5253
self.raise_convergence_errors = raise_convergence_errors
53-
self.dim = self.mesh.topological_dimension()
54-
self.gdim = self.mesh.geometric_dimension()
54+
self.dim = self.mesh.topological_dimension
55+
self.gdim = self.mesh.geometric_dimension
5556

5657
# DMPlex setup
5758
self.plex = self.mesh.topology_dm
@@ -197,18 +198,19 @@ def volume_ratio(self):
197198
:return: the ratio of the largest and smallest element volumes.
198199
:rtype: :class:`float`
199200
"""
200-
volume_array = self.volume.vector().gather()
201-
return volume_array.max() / volume_array.min()
201+
return function_data_max(self.volume) / function_data_min(self.volume)
202202

203203
@property
204204
def coefficient_of_variation(self):
205205
"""
206206
:return: the coefficient of variation (σ/μ) of element volumes.
207207
:rtype: :class:`float`
208208
"""
209-
volume_array = self.volume.vector().gather()
210-
mean = volume_array.sum() / volume_array.size
211-
return np.sqrt(np.sum((volume_array - mean) ** 2) / volume_array.size) / mean
209+
size = self.volume.dat.dataset.layout_vec.getSizes()[0]
210+
mean = function_data_sum(self.volume) / size
211+
coef = firedrake.Function(self.P0)
212+
coef.interpolate((self.volume - mean) ** 2)
213+
return np.sqrt(function_data_sum(coef) / size) / mean
212214

213215
@abc.abstractmethod
214216
def move(self):

movement/spring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, mesh, timestep, **kwargs):
5353
assert timestep > 0.0
5454
self.dt = timestep
5555
num_vertices = mesh.num_vertices()
56-
self._forcing = np.zeros((num_vertices, mesh.topological_dimension()))
56+
self._forcing = np.zeros((num_vertices, mesh.topological_dimension))
5757
self.displacement = np.zeros(num_vertices)
5858

5959
def _create_function_spaces(self):

test/monitors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def ring_monitor(mesh):
1010
alpha = Constant(10.0) # amplitude
1111
beta = Constant(200.0) # width
1212
gamma = Constant(0.15) # radius
13-
dim = mesh.geometric_dimension()
13+
dim = mesh.geometric_dimension
1414
xyz = ufl.SpatialCoordinate(mesh) - ufl.as_vector([0.5] * dim)
1515
r = ufl.dot(xyz, xyz)
1616
return Constant(1.0) + alpha / ufl.cosh(beta * (r - gamma)) ** 2

test/test_monge_ampere.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_change_monitor(self, dim, method):
236236
happen during timestepping.
237237
"""
238238
mesh = self.mesh(dim=dim)
239-
gdim = mesh.geometric_dimension()
239+
gdim = mesh.geometric_dimension
240240
coords = mesh.coordinates.dat.data.copy()
241241
atol = 1.0e-03
242242
rtol = 1.0e02 * atol**gdim

test/test_monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class TestConstant(BaseClasses.TestMonitorBuilder):
4141
"""
4242

4343
def test_value_get_monitor(self):
44-
mb = ConstantMonitorBuilder(self.mesh.topological_dimension())
44+
mb = ConstantMonitorBuilder(self.mesh.topological_dimension)
4545
self.assertTrue(np.allclose(mb.get_monitor()(self.mesh).dat.data, 1))
4646

4747
def test_value_call(self):
48-
mb = ConstantMonitorBuilder(self.mesh.topological_dimension())
48+
mb = ConstantMonitorBuilder(self.mesh.topological_dimension)
4949
self.assertTrue(np.allclose(mb()(self.mesh).dat.data, 1))
5050

5151

0 commit comments

Comments
 (0)