def run(n, base_filename, l=0.5): os.system("rm {}*.vtk".format(base_filename)) # initial conditions domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [1, 1]) positions = [] velocities = [] radius = 0.5 * l / n for y in np.linspace(radius, l - radius, n): for x in np.linspace(0.5 - l / 2 + radius, 0.5 + l / 2 - radius, n): nx = x + 0 * radius * (np.random.rand() - 0.5) ny = y + 0 * radius + 0 * radius * (np.random.rand() - 0.5) if (nx - 0.5)**2 + (ny - 0.5 * l)**2 < (0.5 * l)**2: velocities.append([0, -radius / 5]) positions.append([nx, ny]) masses = np.ones(len(positions)) * l**2 / n**2 # simulation fs = FluidSystem(domain, positions, velocities, masses, base_filename) fs.coeff_centroid_force = 1e-5 fs.display() for num_iter in range(500): print("num_iter:", num_iter, "time:", fs.time) fs.make_step() fs.display()
class TestDerIntegrate_2D_weight_and_positions(unittest.TestCase): def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0, 0], [2, 1]) def test_unit(self): self._test_der([[0.5, 0.5], [1.5, 0.5]], [0, 0], RadialFuncUnit()) self._test_der([[0.5, 0.5], [1.5, 0.5]], [0, 0], RadialFuncUnit()) def _test_der(self, positions, weights, rf): pd = PowerDiagram(self.domain, rf) pd.set_positions(np.array(positions, dtype=np.double)) pd.set_weights(np.array(weights, dtype=np.double)) pd.display_vtk("der_int.vtk") num = pd.der_integrals_wrt_weight_and_positions() print("------------", num.error) if num.error: return ndi = len(positions) mat = np.zeros((ndi, ndi)) for i in range(ndi): for o in range(num.m_offsets[i + 0], num.m_offsets[i + 1]): mat[i, num.m_columns[o]] = num.m_values[o] print("------------", mat)
class TestIntegrate_3D(unittest.TestCase): def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0, 0, 0], [1, 1, 1]) def test_sum_area(self, nb_diracs=100): for _ in range(10): # diracs pd = PowerDiagram(domain=self.domain) pd.set_positions(np.random.rand(nb_diracs, 3)) pd.set_weights(np.ones(nb_diracs)) # integrals areas = pd.integrals() self.assertAlmostEqual(np.sum(areas), 1.0) def test_unit(self): pd = PowerDiagram(domain=self.domain) pd.set_positions([[0.0, 0.0, 0.0]]) pd.set_weights([0.0]) areas = pd.integrals() self.assertAlmostEqual(areas[0], 1.0) centroids = pd.centroids() self.assertAlmostEqual(centroids[0][0], 0.5) self.assertAlmostEqual(centroids[0][1], 0.5) self.assertAlmostEqual(centroids[0][2], 0.5)
class TestPpWmR2_2D(unittest.TestCase): def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0.0, 0.0], [10.0, 10.0]) self.solver = Scipy.Solver() def test_integrals(self): # diracs rd = 2.0 pd = PowerDiagram(domain=self.domain, radial_func=RadialFuncPpWmR2()) pd.set_positions(np.array([[1.0, 0.0], [5.0, 5.0]])) pd.set_weights(np.array([rd**2, rd**2])) # integrals ig = pd.integrals() # Integrate[ Integrate[ ( 4 - ( x * x + y * y ) ) * UnitStep[ 2^2 - x^2 - y^2 ] , { x, -1, 2 } ], { y, 0, 3 } ] self.assertAlmostEqual(ig[0], 10.97565662) self.assertAlmostEqual(ig[1], 8 * np.pi) # centroids ct = pd.centroids() # Integrate[ Integrate[ x * ( 4 - ( x * x + y * y ) ) * UnitStep[ 2^2 - x^2 - y^2 ] , { x, -1, 2 } ], { y, 0, 3 } ] self.assertAlmostEqual(ct[0][0], 1.18937008) self.assertAlmostEqual(ct[0][1], 0.69699702) self.assertAlmostEqual(ct[1][0], 5) self.assertAlmostEqual(ct[1][1], 5) def test_derivatives(self): # diracs rd = 2.0 weights = np.array([rd**2, rd**2]) pd = PowerDiagram(domain=self.domain, radial_func=RadialFuncPpWmR2()) pd.set_positions(np.array([[1.0, 0.0], [5.0, 5.0]])) pd.set_weights(weights.copy()) # derivatives num = pd.der_integrals_wrt_weights() ndi = pd.weights.shape[0] mat = np.zeros((ndi, ndi)) for i in range(ndi): for o in range(num.m_offsets[i + 0], num.m_offsets[i + 1]): mat[i, num.m_columns[o]] = num.m_values[o] # numerical eps = 1e-6 res = pd.integrals() delta = np.max(np.abs(mat)) * 100 * eps for i in range(ndi): pd.set_weights( np.array([weights[j] + eps * (i == j) for j in range(ndi)])) des = pd.integrals() der = (des - res) / eps for j in range(ndi): #self.assertAlmostEqual(mat[i, j], der[j], delta=delta) print(mat[i, j], der[j])
class TestIntegrate_2D(unittest.TestCase): def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0, 0], [1, 1]) def test_sum_area(self, nb_diracs=100): for _ in range(10): # diracs pd = PowerDiagram(domain=self.domain) pd.set_positions(np.random.rand(nb_diracs, 2)) pd.set_weights(np.ones(nb_diracs)) # integrals areas = pd.integrals() self.assertAlmostEqual(np.sum(areas), 1.0) def test_unit(self): pd = PowerDiagram(domain=self.domain) pd.set_positions([[0.0, 0.0]]) pd.set_weights([0.0]) areas = pd.integrals() self.assertAlmostEqual(areas[0], 1.0) centroids = pd.centroids() self.assertAlmostEqual(centroids[0][0], 0.5) self.assertAlmostEqual(centroids[0][1], 0.5) def test_gaussian(self): # wolfram: N[ Integrate[ Integrate[ Exp[ ( 0 - x*x - y*y ) / 1 ], { x, -0.5, 0.5 } ], { y, -0.5, 0.5 } ] ] # wolfram: N[ Integrate[ Integrate[ x * Exp[ ( 0 - x*x - y*y ) / 0.1 ], { x, 0, 1 } ], { y, 0, 1 } ] ] / N[ Integrate[ Integrate[ Exp[ ( 0 - x*x - y*y ) / 0.1 ], { x, 0, 1 } ], { y, 0, 1 } ] ] def tg(position, w, eps, ei, ec): pd = PowerDiagram(radial_func=RadialFuncEntropy(eps), domain=self.domain) pd.set_positions([position]) pd.set_weights([w]) res = pd.integrals() self.assertAlmostEqual(res[0], ei, 5) res = pd.centroids() self.assertAlmostEqual(res[0][0], ec[0], 5) self.assertAlmostEqual(res[0][1], ec[1], 5) tg([0.5, 0.5], w=0.0, eps=1.0, ei=0.851121, ec=[0.500000, 0.500000]) tg([0.5, 0.5], w=1.0, eps=1.0, ei=2.313590, ec=[0.500000, 0.500000]) tg([0.5, 0.5], w=0.0, eps=2.0, ei=0.921313, ec=[0.500000, 0.500000]) tg([0.5, 0.5], w=1.0, eps=2.0, ei=1.518990, ec=[0.500000, 0.500000]) tg([0.0, 0.0], w=0.0, eps=1.0, ei=0.557746, ec=[0.423206, 0.423206]) tg([0.0, 0.0], w=1.0, eps=1.0, ei=1.516110, ec=[0.423206, 0.423206]) tg([0.0, 0.0], w=0.0, eps=2.0, ei=0.732093, ec=[0.459862, 0.459862]) tg([0.0, 0.0], w=1.0, eps=2.0, ei=1.207020, ec=[0.459862, 0.459862]) tg([0.0, 0.0], w=0.0, eps=0.1, ei=.0785386, ec=[0.178406, 0.178406])
def make_square(box=[0, 0, 1, 1]): """ Constructs a square domain with uniform measure (source measure 'rho'). To be passed to the 'newton_ot' and 'PowerDiagram' functions. Args: box (list): coordinates of the bottom-left and top-right corners Returns: domain (pysdot.domain_types): domain """ domain = ConvexPolyhedraAssembly() domain.add_box([box[0], box[1]], [box[2], box[3]]) return domain
def run(n, base_filename, l=0.5): # domain domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [1, 1]) # initial positions, weights and masses positions = [] if n == 1: radius = 0.3 mass = 3.14159 * radius**2 positions.append([0.5, radius]) else: radius = l / (2 * (n - 1)) mass = l**2 / n**2 for y in np.linspace(radius, l - radius, n): for x in np.linspace(0.5 - l / 2 + radius, 0.5 + l / 2 - radius, n): nx = x + 0.0 * radius * (np.random.rand() - 0.5) ny = y + 0.0 * radius * (np.random.rand() - 0.5) + 0.5 * radius positions.append([nx, ny]) positions = np.array(positions) nb_diracs = positions.shape[0] # dim = positions.shape[ 1 ] # OptimalTransport ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_weights(np.ones(nb_diracs) * radius**2) ot.set_masses(np.ones(nb_diracs) * mass) ot.set_positions(positions) ot.max_iter = 100 ot.adjust_weights() ot.display_vtk(base_filename + "0.vtk", points=True, centroids=True) # history of centroids ce = ot.get_centroids() ce[:, 1] += radius / 10 bh = [ce] dt = 1.0 for num_iter in range(200): print("num_iter", num_iter) bh.append(ot.get_centroids()) fit_positions(ot, bh, dt) # display n1 = int(num_iter / 1) + 1 ot.display_vtk(base_filename + "{}.vtk".format(n1), points=True, centroids=True)
def run(n, base_filename, l=0.5): # domain domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [1, 1]) # initial positions, weights and masses positions = [] if n == 1: radius = 0.3 mass = 3.14159 * radius**2 positions.append([0.5, radius]) else: radius = l / (2 * (n - 1)) mass = l**2 / n**2 for y in np.linspace(radius, l - radius, n): for x in np.linspace(radius, l - radius, n): nx = x # + 0.2 * radius * (np.random.rand() - 0.5) ny = y # + 0.2 * radius * (np.random.rand() - 0.5) positions.append([nx, ny]) positions = np.array(positions) nb_diracs = positions.shape[0] dim = positions.shape[1] # OptimalTransport ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_weights(np.ones(nb_diracs) * radius**2) ot.set_masses(np.ones(nb_diracs) * mass) ot.set_positions(positions) ot.adjust_weights() ot.display_vtk(base_filename + "0.vtk", points=True) g = np.zeros((nb_diracs, dim)) g[:, 1] = -0.001 bh = [ot.get_centroids()] # history of centroids for num_iter in range(50): bh.append(ot.get_centroids()) print("num_iter", num_iter) # proposition for centroids bn = 2 * bh[-1] - bh[-2] + g # find a new set of diracs parameters (position and weight) # to be as close to the new centroids as possible update_positions_to_get_centroids(ot, bn) # display n1 = num_iter + 1 ot.display_vtk(base_filename + "{}.vtk".format(n1), points=True)
class TestDerIntegrate_2D_weights(unittest.TestCase): def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0, 0], [2, 1]) def test_unit(self): self._test_der([[0.5, 0.5], [1.5, 0.5]], [0, 0], RadialFuncUnit()) def test_gaussian(self): # wolfram: x=-0.5; y=(1-u)*(0.5)-0.5; N[ Integrate[ Exp[ ( 0 - x*x - y*y ) / 1 ], { u, 0, 1 } ]] -> 0.718492 self._test_der([[0.4, 0.5], [1.5, 0.5]], [1, 0], RadialFuncEntropy(1)) self._test_der([[0.4, 0.5], [1.5, 0.5]], [1, 2], RadialFuncEntropy(1)) N = 10 for _ in range(50): positions = [[np.random.rand(), np.random.rand()] for i in range(N)] weights = [np.random.rand() * 0.1 for i in range(N)] self._test_der(positions, weights, RadialFuncEntropy(2)) def _test_der(self, positions, weights, rf): pd = PowerDiagram(self.domain, rf) pd.set_positions(np.array(positions, dtype=np.double)) pd.set_weights(np.array(weights, dtype=np.double)) num = pd.der_integrals_wrt_weights() if num.error: return ndi = len(positions) mat = np.zeros((ndi, ndi)) for i in range(ndi): for o in range(num.m_offsets[i + 0], num.m_offsets[i + 1]): mat[i, num.m_columns[o]] = num.m_values[o] eps = 1e-6 res = pd.integrals() delta = np.max(np.abs(mat)) * 200 * eps for i in range(ndi): pd.set_weights( np.array([weights[j] + eps * (i == j) for j in range(ndi)])) des = pd.integrals() der = (des - res) / eps for j in range(ndi): self.assertAlmostEqual(mat[i, j], der[j], delta=delta)
class TestOptimalTransport(unittest.TestCase): def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0, 0], [1, 1]) def test_base_ot(self, nb_diracs=1000): for _ in range(10): ot = OptimalTransport(self.domain) # diracs ot.set_positions(np.random.rand(nb_diracs, 2)) ot.set_weights(np.ones(nb_diracs)) # optimal weights ot.adjust_weights() # integrals areas = ot.pd.integrals() self.assertAlmostEqual(np.min(areas), 1.0 / nb_diracs, places=6) self.assertAlmostEqual(np.max(areas), 1.0 / nb_diracs, places=6) # ot.pd.display_vtk("results/vtk/pd.vtk") def test_ball_cut(self, nb_diracs=100): for _ in range(10): ot = OptimalTransport(self.domain, RadialFuncInBall()) positions = np.random.rand(nb_diracs, 2) positions[:, 1] *= 0.5 radius = 0.25 / nb_diracs**0.5 mass = np.pi * radius**2 # diracs ot.set_positions(positions) ot.set_weights(np.ones(nb_diracs) * radius**2) ot.set_masses(np.ones(nb_diracs) * mass) # optimal weights ot.adjust_weights() # integrals areas = ot.pd.integrals() self.assertAlmostEqual(np.min(areas), mass, places=6) self.assertAlmostEqual(np.max(areas), mass, places=6)
def setUp(self): rf = RadialFuncArfd( lambda r: ((1 - r * r) * (r < 1))**2.5, # value lambda w: 1 / w**0.5, # input scaling lambda w: w**2.5, # output scaling lambda r: 2.5 * ((1 - r * r) * (r < 1))**1.5, # value for the der wrt weight lambda w: 1 / w**0.5, # input scaling for the der wrt weight lambda w: w**1.5, # output scaling for the der wrt weight [1], # stops (radii values where we may have discontinuities) 1e-8 # precision ) # set up a domain, with only 1 dirac domain = ConvexPolyhedraAssembly() domain.add_box([0.0, 0.0], [2.0, 1.0]) self.pd = PowerDiagram(domain=domain, radial_func=rf)
def setUp(self): rf = RadialFuncArfd( lambda r: (1 - r * r) * (r < 1), # value lambda w: 1 / w**0.5, # input (radius) scaling lambda w: w, # output scaling lambda r: r < 1, # value for the der wrt weight lambda w: 1 / w**0.5, # input scaling for the der wrt weight lambda w: 1, # output scaling for the der wrt weight [1] # stops (radii value where we may have discontinuities) ) # should use only 2 polynomials self.assertEqual(rf.nb_polynomials(), 2) # set up a domain, with only 1 dirac domain = ConvexPolyhedraAssembly() domain.add_box([0.0, 0.0], [2.0, 1.0]) self.pd = PowerDiagram(domain=domain, radial_func=rf)
def run(n, base_filename): domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [1, 1]) domain.add_box([0.2, -0.5], [0.8, 0]) positions = [] radius = 0.5 / (2 * (n - 1)) for y in np.linspace(radius, 0.5 + radius, n): for x in np.linspace(radius, 0.5 + radius, n): positions.append([x, y]) nb_diracs = len(positions) # ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_masses(np.ones(nb_diracs) * 0.8 * 0.5**2 / nb_diracs) ot.set_weights(np.ones(nb_diracs) * radius**2) ot.set_positions(np.array(positions)) b_old = ot.pd.centroids() ot.adjust_weights() ot.display_vtk(base_filename + "0.vtk") nb_timesteps = int(20 / radius) v = np.zeros((nb_diracs, 2)) dt = 0.003 * radius for i in range(nb_timesteps): print(i, "/", nb_timesteps) # first trial v[:, 1] -= 1 p_old = ot.get_positions() p_tst = p_old + dt * v ot.set_positions(p_tst) ot.adjust_weights() # display d = int(n / 5) if i % d == 0: ot.display_vtk(base_filename + "{:03}.vtk".format(1 + int(i / d))) # corrections b_new = ot.pd.centroids() v = (b_new - b_old) / dt ot.set_positions(b_new) b_old = b_new
def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0.0, 0.0], [10.0, 10.0]) self.solver = Scipy.Solver()
from pysdot.domain_types import ConvexPolyhedraAssembly from pysdot import PowerDiagram import numpy as np positions = np.random.rand( 30, 2 ) domain = ConvexPolyhedraAssembly() domain.add_box([-1, -1], [2, 2]) # diracs pd = PowerDiagram( positions, domain = domain ) pd.add_replication( [ +1, 0 ] ) pd.add_replication( [ -1, 0 ] ) pd.display_vtk( "pb.vtk" )
def run(n, base_filename, l=0.5): # domain domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [1, 1]) # initial positions, weights and masses positions = [] radius = l / (2 * (n - 1)) mass = l**2 / n**2 for y in np.linspace(radius, l - radius, n): for x in np.linspace(0.5 - l / 2 + radius, 0.5 + l / 2 - radius, n): nx = x + 0.0 * radius * (np.random.rand() - 0.5) ny = y + 0.0 * radius * (np.random.rand() - 0.5) positions.append([nx, ny]) positions = np.array(positions) nb_diracs = positions.shape[0] dim = positions.shape[1] # OptimalTransport ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_weights(np.ones(nb_diracs) * radius**2) ot.set_masses(np.ones(nb_diracs) * mass) ot.set_positions(positions) ot.max_iter = 100 ot.adjust_weights() ot.display_vtk(base_filename + "0.vtk", points=True, centroids=True) # gravity G = np.zeros((nb_diracs, dim)) G[:, 1] = -9.81 # eps = 0.5 dt = radius * 0.1 V = np.zeros((nb_diracs, dim)) M = np.stack([ot.get_masses() for d in range(dim)]).transpose() for num_iter in range(500): print("num_iter:", num_iter, "dt:", dt) C = ot.get_centroids() X = ot.get_positions() A = G + (C - ot.get_positions()) / (M * eps**2) while True: dV = dt * A dX = dt * (V + dV) if np.max(np.linalg.norm(dX, axis=1, ord=2)) < 0.2 * radius: dt *= 1.05 V += dV X += dX break dt *= 0.5 ot.set_positions(X) ot.adjust_weights() # display n1 = int(num_iter / 1) + 1 ot.display_vtk(base_filename + "{}.vtk".format(n1), points=True, centroids=True)
def make_square(box=[0, 0, 1, 1]): density = ConvexPolyhedraAssembly() density.add_box([box[0], box[1]], [box[2], box[3]]) return density
x, y = np.meshgrid(t, t) positions = np.hstack((np.reshape(x, (n * n, 1)), np.reshape(y, (n * n, 1)))) R2 = positions[:, 0]**2 + positions[:, 1]**2 positions = positions[R2 <= 4] positions = positions[positions[:, 0] + positions[:, 1] > 1.0 / n] positions = positions[-positions[:, 0] + positions[:, 1] > 1.0 / n] N = positions.shape[0] rho0 = 1 / np.pi mass = 0.25 * rho0 * np.pi * 4 / N target_radius = (mass / np.pi)**0.5 # iterations weights = np.ones(positions.shape[0]) * target_radius**2 domain = ConvexPolyhedraAssembly() domain.add_convex_polyhedron([ [0, 0, +1, -1], [9, 9, 0, +1], [-9, 9, -1, -1], ]) domain.display_boundaries_vtk(directory + "/bounds.vtk") color_values = 0.5 * np.linalg.norm( positions, axis=1, keepdims=True, ord=2) color_values = (color_values - np.min(color_values)) / ( np.max(color_values) - np.min(color_values)) ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_weights(weights) ot.set_masses(np.ones(positions.shape[0]) * mass)
for na in [80,]: #[ 20, 40, 80, 160]: directory = "results/bimodal_crowd_{}".format( na ) # constants alpha = 2*np.sqrt(1/np.pi) target_radius = 0.4* alpha / na timestep = target_radius/2 epsilon = target_radius # positions t = np.linspace( 0 + target_radius, alpha - target_radius, na ) x, y = np.meshgrid( t, t ) positions = np.hstack( ( x.reshape( ( -1, 1 ) ), y.reshape( ( -1, 1 ) ) ) ) # domain domain = ConvexPolyhedraAssembly() bb1min = [ 0, 0 ] bb1max = [ alpha, alpha ] bb2min = [ alpha, alpha/3 ] bb2max = [ 4*alpha/3, 2*alpha/3 ] bb3min = [ 4*alpha/3, 0 ] bb3max = [ 7*alpha/3, alpha ] domain.add_box( bb1min, bb1max ) domain.add_box( bb2min, bb2max ) domain.add_box( bb3min, bb3max ) domain.display_boundaries_vtk( directory+"/bounds.vtk" ) all_in_boxes = lambda Y: np.all(np.logical_or(in_box(bb1min, bb1max), np.logical_or(in_box(Y, bb2min, bb2max), in_box(Y, bb3min, bb3max))))
dws = 5 img = imageio.imread("clay.jpg") img = img[:, :, 1] beg = int(img.shape[1] / 2 - img.shape[0] / 2) end = beg + img.shape[0] img = img[:, beg:end] img = downscale_local_mean(img, (dws, dws)) img = np.max(img) * 1.05 - img img /= np.sum(img) # plt.imshow( img ) # plt.show() # domain domain = ConvexPolyhedraAssembly() domain.add_img([0, 0], [1, 1], img) # diracs nd = 100000 ot = OptimalTransport(domain) ot.set_positions(np.random.rand(nd, 2)) ot.set_weights(np.ones(nd) / nd) ot.obj_max_dw = 1e-5 ot.verbosity = True # solve ot.adjust_weights(relax=1.0) # display ot.pd.display_vtk("results/pd.vtk", centroids=True)
def run(n, base_filename, l=0.5): # domain domain = ConvexPolyhedraAssembly() # domain.add_box([0, 0], [1, 1]) domain.add_convex_polyhedron([ # x y Nx Ny (ext) [0, 0, -1, -0.2], [0, 0, -0.2, -1], [1, 1, +1, 0], [1, 1, 0, +1], ]) # initial positions, weights and masses positions = [] if n == 1: radius = 0.2 else: radius = l / (2 * (n - 1)) for y in np.linspace(radius, l - radius, n): for x in np.linspace(radius, l - radius, n): nx = x + 0.2 * radius * (np.random.rand() - 0.5) ny = y + 0.2 * radius * (np.random.rand() - 0.5) positions.append([nx, ny]) positions = np.array(positions) nb_diracs = positions.shape[0] # OptimalTransport ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_weights(np.ones(nb_diracs) * radius**2) ot.set_masses(np.ones(nb_diracs) * l**2 / nb_diracs) ot.set_positions(positions) ot.adjust_weights() ot.display_vtk(base_filename + "0.vtk") ot.set_positions(ot.get_centroids()) velocity = 0.0 * positions for num_iter in range(200): print("num_iter", num_iter) # barycenters at the beginning ot.adjust_weights() b_o = ot.get_centroids() # trial for the new barycenters velocity[:, 0] -= 0.05 * radius * 0.707 velocity[:, 1] -= 0.05 * radius * 0.707 b_n = b_o + velocity # optimisation of positions to go to the target barycenters ropt = scipy.optimize.minimize(obj, b_n.flatten(), (ot, b_n), tol=1e-4, method='BFGS', options={'eps': 1e-4 * radius}) positions = ropt.x.reshape((-1, 2)) ot.set_positions(positions) ot.adjust_weights() # new barycenters, corrected (minimize have update the weights) b_n = ot.get_centroids() velocity = b_n - b_o print(positions, velocity) # display ot.pd.display_vtk_points(base_filename + "pts_{}.vtk".format(num_iter + 1)) ot.display_vtk(base_filename + "{}.vtk".format(num_iter + 1))
import numpy as np # constants for na in [20, 40, 80, 160, 200]: # directory = "results/bimodal_crowd_{}".format(na) # constants target_radius = 3 * 0.45 / na # positions t = np.linspace(0 + target_radius, 3 - target_radius, na) x, y = np.meshgrid(t, t) positions = np.hstack((x.reshape((-1, 1)), y.reshape((-1, 1)))) # domain domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [3, 3]) domain.add_box([3, 1], [4, 2]) domain.add_box([4, 0], [7, 3]) domain.display_boundaries_vtk(directory + "/bound.vtk") domain_asy = "draw((0,0)--(3,0)--(3,1)--(4,1)--(4,0)--(7,0)--(7,3)--(4,3)--(4,2)--(3,2)--(3,3)--(0,3)--cycle);\n" s = 0.02 g = GradGrid(domain, [[7 - s, 3 - s], [7 - s, 0 + s]], s) # iterations ot = OptimalTransport(domain, RadialFuncInBall()) ot.set_weights(np.ones(positions.shape[0]) * target_radius**2) ot.set_masses(np.ones(positions.shape[0]) * np.pi * target_radius**2)
from pysdot.domain_types import ConvexPolyhedraAssembly from pysdot.radial_funcs import RadialFuncEntropy from pysdot import OptimalTransport import numpy as np # constants for n in [10]: directory = "results/diffusion_{}".format(n) # constants eps = n**-0.5 # domain domain = ConvexPolyhedraAssembly() domain.add_convex_polyhedron([[-1, -1, -1, 0], [-1, -1, 0, -1], [+2, -1, 1, -1], [-1, +2, 0, +1]]) domain.display_boundaries_vtk(directory + "/bounds.vtk") # positions = [] for y in np.linspace(0, 1, n): for x in np.linspace(0, 1, n): positions.append([x, y]) positions = np.array(positions) # iterations max_w = -1 min_w = 0 ot = OptimalTransport(domain, RadialFuncEntropy(eps))
def test_measure(self): domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [2, 1]) self.assertAlmostEqual(domain.measure(), 2.0)
# positions t = np.linspace( 0 + target_radius, 1 - target_radius, na ) x, y = np.meshgrid( t, t ) positions = np.hstack( ( x.reshape( ( -1, 1 ) ), y.reshape( ( -1, 1 ) ) ) ) limx = pos_door + 1.2 / opened_width # pon = [] for i in range( 16 ): a = 2 * np.pi * i / 16 pon.append( [ 1.5 + obstacle_radius * np.cos( a ), 0.5 + obstacle_radius * np.sin( a ), np.cos( a ), np.sin( a ) ] ) # domain domain = ConvexPolyhedraAssembly() domain.add_box( [ 0, 0 ], [ 2, 1 ] ) domain.add_box( [ pos_door, 0.5 - opened_width / 2 ], [ limx, 0.5 + opened_width / 2 ] ) if obstacle_radius: domain.add_convex_polyhedron( np.array( pon ), -1.0 ) domain.display_boundaries_vtk( directory + "/bound.vtk" ) # domain_asy = "draw((0,0)--(3,0)--(3,1)--(4,1)--(4,0)--(7,0)--(7,3)--(4,3)--(4,2)--(3,2)--(3,3)--(0,3)--cycle);\n" s = 0.5 * target_radius g = GradGrid( domain, [ [ limx - 2 * s, 0.5 ] ], s ) # iterations color_values = positions[ :, 1 ] color_values = ( color_values - np.min( color_values ) ) / np.ptp( color_values )
def setUp(self): self.domain = ConvexPolyhedraAssembly() self.domain.add_box([0, 0], [1, 1])
positions.append([r * np.cos(a), r * np.sin(a)]) ot.set_positions(np.array(positions)) if cpt == 0: ot.set_weights(np.array(masses) / np.pi * 0.25) ot.set_masses(np.array(masses)) ot.max_iter = 100 ot.adjust_weights(relax=0.5) ot.display_vtk("lc_{}.vtk".format(cpt)) return ot.pd.der_boundary_integral() domain = ConvexPolyhedraAssembly() domain.add_box([0, 0], [10, 10]) crb = {} for n in range(30, 31): ot = OptimalTransport(domain, RadialFuncInBall()) k = "{}".format(n) crb[k] = [] cpt = 0 for coeff_r in [0.9875]: # np.linspace( 0.9875, 0.4, 120 ): e = test_discr(ot, n, coeff_r, cpt) print(e) # crb[ k ].append( [ coeff_r, e ] ) # cpt += 1 # for key, val in crb.items():
import numpy as np import scipy.interpolate as sinter from pysdot.domain_types import ConvexPolyhedraAssembly from pysdot.radial_funcs import RadialFuncInBall from agd import Eikonal from Energy import Energy from Trajectories import Trajectories from Trajectories import Situation_Init domain = ConvexPolyhedraAssembly() domain.add_box([0,0], [8, 8]) domain.add_box([8, 3.5], [11, 4.5]) domain.add_box([11,0],[19,8]) eps_density=1e-3 domain.add_box(min_pos=[8,0], max_pos=[11,3.5], coeff=eps_density) domain.add_box(min_pos=[8,4.5], max_pos=[11,8], coeff=eps_density) point_infinity_1 = np.array([18, 7]) point_infinity_2=np.array([18,1]) hfmInput = Eikonal.dictIn({ 'model': 'Isotropic2', 'order': 2., 'cost':1. }) gridScale = 0.1 hfmInput.SetRect(sides=[[-0.5,19.5],[-0.5,8.5]], gridScale=gridScale) hfmInput.update({'seeds': [point_infinity_1,point_infinity_2]}) hfmInput['arrayOrdering'] = 'RowMajor' X,Y = hfmInput.Grid()