From 1d0c2993beacf1f59d133e94b877c95419989dd8 Mon Sep 17 00:00:00 2001 From: SamTov Date: Wed, 15 May 2024 16:42:16 +0200 Subject: [PATCH] Update growing particle number example --- examples/growing_sphere_number.py | 59 +++++++++++++++++++++++++++++++ znvis/particle/particle.py | 6 ++-- znvis/visualizer/visualizer.py | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 examples/growing_sphere_number.py diff --git a/examples/growing_sphere_number.py b/examples/growing_sphere_number.py new file mode 100644 index 0000000..28d6a9f --- /dev/null +++ b/examples/growing_sphere_number.py @@ -0,0 +1,59 @@ +""" +ZnVis: A Zincwarecode package. +License +------- +This program and the accompanying materials are made available under the terms +of the Eclipse Public License v2.0 which accompanies this distribution, and is +available at https://www.eclipse.org/legal/epl-v20.html +SPDX-License-Identifier: EPL-2.0 +Copyright Contributors to the Zincwarecode Project. +Contact Information +------------------- +email: zincwarecode@gmail.com +github: https://github.com/zincware +web: https://zincwarecode.com/ +Citation +-------- +If you use this module please cite us with: + +Summary +------- +Tutorial script to visualize simple spheres over a random trajectory. +""" + +import numpy as np + +import znvis as vis + +if __name__ == "__main__": + """ + Run the simple spheres example. + """ + material_1 = vis.Material(colour=np.array([30, 144, 255]) / 255, alpha=0.6) + # Define the first particle. + trajectory = np.random.uniform(-100, 100, (100, 1000, 3)) + trajectory = [] + p_number = 1 + for t in range(1000): + trajectory.append(np.random.uniform(-100, 100, (p_number, 3))) + + p_number = np.random.randint(p_number, 5 * p_number) + + if p_number > 20000: + p_number = 20000 + + mesh = vis.Sphere(radius=2.0, resolution=3, material=material_1) + particle = vis.Particle( + name="Blue", mesh=mesh, position=trajectory, smoothing=False + ) + + # Create a bounding box + bounding_box = vis.BoundingBox( + center=np.array([0, 0, 0]), box_size=np.array([100, 100, 100]) + ) + + # Construct the visualizer and run + visualizer = vis.Visualizer( + particles=[particle], frame_rate=20, bounding_box=bounding_box + ) + visualizer.run_visualization() diff --git a/znvis/particle/particle.py b/znvis/particle/particle.py index 52fef30..3540880 100644 --- a/znvis/particle/particle.py +++ b/znvis/particle/particle.py @@ -105,13 +105,13 @@ def construct_mesh_list(self): """ self.mesh_list = [] try: - n_particles = int(self.position.shape[1]) - n_time_steps = int(self.position.shape[0]) + # n_particles = int(self.position.shape[1]) + n_time_steps = int(len(self.position)) except ValueError: raise ValueError("There is no data for these particles.") for i in track(range(n_time_steps), description=f"Building {self.name} Mesh"): - for j in range(n_particles): + for j in range(np.shape(self.position[i])[0]): if j == 0: if self.director is not None: mesh = self._create_mesh( diff --git a/znvis/visualizer/visualizer.py b/znvis/visualizer/visualizer.py index bfa88f3..00f5502 100644 --- a/znvis/visualizer/visualizer.py +++ b/znvis/visualizer/visualizer.py @@ -93,7 +93,7 @@ def __init__( self.bounding_box = bounding_box() if bounding_box else None if number_of_steps is None: - number_of_steps = particles[0].position.shape[0] + number_of_steps = len(particles[0].position) self.number_of_steps = number_of_steps self.output_folder = pathlib.Path(output_folder).resolve()