def test_rotation(): t = pg.Transformation(rotation=np.array([0, 0, np.pi / 2])) p = np.array([1, 0, 0]) pp = t.apply_to_point(p) np.testing.assert_allclose(pp, [0, 1, 0], **TOLERANCE) t = pg.Transformation(rotation=np.array([np.pi, 0, np.pi / 2])) p = np.array([1, 0, 0]) pp = t.apply_to_point(p) np.testing.assert_allclose(pp, [0, 1, 0], **TOLERANCE)
def initialize( self, simulation_kernel: 'pg.SimulationKernel', init_stage: 'pg.InitStages' ): super().initialize(simulation_kernel, init_stage) if init_stage == pg.InitStages.CHECK_ARGUMENTS: shift = pg.Transformation( translation=np.array([0, 0, -self.inlet_zone]) ) self._transformation = ( self._transformation.apply_to_transformation(shift) ) if (max(self._transformation.scaling) > 1.001 or min(self._transformation.scaling) < 0.999): LOG.warning( "You are trying to scale a tube mesh by a factor of " f"{self._transformation.scaling}. Keep in mind that " "this mesh already has an inherent scale from the " "OpenFOAM simulation." ) if self.length + self.inlet_zone > 0.15: # TODO: determine this dynamically based on available cases raise ValueError( "No path to OpenFOAM simulation files is known for a tube " "that is longer than 0.15 m. " f"Selected length: {self.length} m. " f"Keep in mind that the inlet zone of {self.inlet_zone} m " "is not usable." ) self._is_active = not np.isclose(self.flow_rate, 0, atol=1e-20) elif init_stage == pg.InitStages.SET_UP_FLOW_SYSTEM: self.process_changed_inlet_flow_rate(simulation_kernel, "inlet", self.flow_rate)
def get_outlet_area(self, outlet_name: str) -> ( 'pg.Geometry', 'pg.Transformation' ): if outlet_name != 'outlet': raise ValueError( "Outlet not found! " "This pipe only has one outlet named 'outlet'." ) geometry = pg.Geometry(pg.Shapes.CYLINDER) # TODO(jdrees): calculate the proper transformation for the # outlet based on the tube length and radius local_transformation = pg.Transformation( translation=np.array(( 0, 0, self.inlet_zone + self.length - self.outlet_zone / 2 )), scaling=np.array(( self.radius * 2, self.radius * 2, self.outlet_zone )) ) return ( geometry, self._transformation.apply_to_transformation(local_transformation) )
def initialize(self, simulation_kernel: 'pg.SimulationKernel', init_stage: 'pg.InitStages'): super().initialize(simulation_kernel, init_stage) if init_stage == pg.InitStages.CHECK_ARGUMENTS: if self.seed == 'random': self._rng = np.random.RandomState(seed=None) elif self.seed != '': self._rng = np.random.RandomState(seed=int(self.seed)) else: self._rng = simulation_kernel.get_random_number_generator() self._geometry = pg.Geometry(shape=pg.Shapes[self.shape]) self._transformation = pg.Transformation( translation=np.array(self.translation), rotation=np.array(self.rotation), scaling=np.array(self.scale)) if (self.attached_object != '' and self.attached_object not in simulation_kernel.get_components()): raise ValueError( "No object component with the name " f"{self.attached_object} attached to the simulation " "kernel could be found.") elif init_stage == pg.InitStages.BUILD_SCENE: self._attached_object = cast( 'pg.Object', simulation_kernel.get_components()[self.attached_object])
def __init__(self): super().__init__() self._turned_on = False self._burst_on = False self._rng = np.random.RandomState(seed=None) self._transformation = pg.Transformation() """Transformation of this sensor in the scene."""
def test_inverse(): t = pg.Transformation(translation=np.array([1, 2, 3]), rotation=np.deg2rad([45, 135, 90]), scaling=np.array([1.5, 2, 2.5])) p = np.array([42, 1337, 2]) np.testing.assert_allclose(p, t.apply_inverse_to_point(t.apply_to_point(p)), **TOLERANCE)
def test_translation_rotation_scale(): t = pg.Transformation(translation=np.array([1, 2, 3]), rotation=np.deg2rad([45, 135, 90]), scaling=np.array([1.5, 2, 2.5])) p = np.array([1, 0, 0]) pp = t.apply_to_point(p) # Reference from Blender: np.testing.assert_allclose(pp, [1, 0.93934, 1.93934], **TOLERANCE)
def test_chaining(): """ Concatenation of transformations. Test values verified/obtained with Blender. """ p = np.array([1, 2, 3]) t1 = pg.Transformation(translation=np.array([-.02, .35, 1]), rotation=np.deg2rad([30, 22, 278]), scaling=np.array([.1, 1.1, .56])) np.testing.assert_allclose(t1.apply_to_point(p), [1.18099, -0.541337, 3.33142], **TOLERANCE) t2 = pg.Transformation(translation=np.array([-.2, -.3, -.4]), rotation=np.deg2rad([1, 2, 3]), scaling=np.array([2, .3, .4])) # First execute t1, then t2: t3 = t2.apply_to_transformation(t1) np.testing.assert_allclose(t3.apply_to_point(p), [2.21336, -0.359409, 0.846289], **TOLERANCE)
def __init__(self): super().__init__() self.sensor_id: int = -1 """Unique integer sensor ID. Set by the sensor manager.""" self._transformation = pg.Transformation() """Transformation of this sensor in the scene.""" self._geometry = pg.Geometry(pg.Shapes.NONE) """Geometry of this sensor, set from shape."""
def get_outlet_area( self, outlet_name: str) -> ('pg.Geometry', 'pg.Transformation'): if outlet_name != "outlet": raise ValueError("Outlet not found! " "This pump only has one outlet named 'outlet'.") # The pump doesn't really define any outlet area, # so let's use a nonexistent one. geometry = pg.Geometry(pg.Shapes.NONE) transformation = pg.Transformation() # identity transformation return geometry, transformation
def initialize(self, simulation_kernel: 'pg.SimulationKernel', init_stage: 'pg.InitStages'): if init_stage == pg.InitStages.CHECK_ARGUMENTS: self._geometry = pg.Geometry(shape=pg.Shapes[self.shape]) self._transformation = pg.Transformation( translation=np.array(self.translation), rotation=np.array(self.rotation), scaling=np.array(self.scale)) if self.attached_object not in simulation_kernel.get_components(): raise ValueError( "No object component with the name " f"{self.attached_object} attached to the simulation " "kernel could be found.") if self.seed == 'random': self._rng = np.random.RandomState(seed=None) elif self.seed != '': self._rng = np.random.RandomState(seed=int(self.seed)) else: self._rng = simulation_kernel.get_random_number_generator() self.initialize_sample_points() elif init_stage == pg.InitStages.CREATE_FOLDERS: os.makedirs(os.path.join(simulation_kernel.results_dir, self.log_folder), exist_ok=True) elif init_stage == pg.InitStages.BUILD_SCENE: self._attached_object = cast( 'pg.Object', simulation_kernel.get_components()[self.attached_object]) elif init_stage == pg.InitStages.CREATE_FILES: name = (self.id if self.component_name == "Generic component" else self.component_name) self._csv_file = open(os.path.join(simulation_kernel.results_dir, self.log_folder, f'sensor[{name}].csv'), mode='w') fieldnames = ['sim_time'] if self.log_mesh_index: fieldnames.append('mesh_index') # Assumes self.initialize_sample_points() to have been called: fieldnames.extend([ # e.g., "my_prefix_[0.123, 42.0, 1.21]_x": f'{col}_{axis}' for col, axis in zip([ self.column_prefix + str(point_global) for point_global in self._sample_points_global ], ['x', 'y', 'z']) ]) self._csv_writer = csv.writer(self._csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) self._csv_writer.writerow(fieldnames)
def initialize(self, simulation_kernel: 'pg.SimulationKernel', init_stage: 'pg.InitStages'): super().initialize(simulation_kernel, init_stage) if init_stage == pg.InitStages.CHECK_ARGUMENTS: self._geometry = pg.Geometry(shape=pg.Shapes[self.shape]) self._transformation = pg.Transformation( translation=np.array(self.translation), rotation=np.array(self.rotation), scaling=np.array(self.scale)) if init_stage == pg.InitStages.REGISTER_SENSORS: simulation_kernel.get_sensor_manager().register_sensor(self)
def __init__(self): super().__init__() self._transformation = pg.Transformation() """Transformation of this sensor in the scene.""" self._geometry = pg.Geometry(pg.Shapes.NONE) """Geometry of this sensor, set from shape.""" self._attached_object: Optional['pg.Object'] = None self._csv_file: Optional[io.TextIOWrapper] = None self._csv_writer: Optional[csv.writer] = None self._rng = np.random.RandomState(seed=None) self._sample_points_global: Optional[np.ndarray] = None
def initialize( self, simulation_kernel: 'pg.SimulationKernel', init_stage: 'pg.InitStages' ): super().initialize(simulation_kernel, init_stage) if init_stage == pg.InitStages.CHECK_ARGUMENTS: self._transformation = pg.Transformation( translation=np.array(self.translation), rotation=np.array(self.rotation), scaling=np.array(self.scale) ) if init_stage == pg.InitStages.CREATE_DATA_STRUCTURES: self.load_current_vector_field(simulation_kernel) if init_stage == pg.InitStages.BUILD_SCENE: simulation_kernel.get_scene_manager().add_object(self)
def get_vector_field_manager(): if VectorFieldMgrSingleton._vfm is None: VectorFieldMgrSingleton._vfm = pg.VectorFieldManager( vector_field=pg.VectorFieldParser.parse_folder( folder=os.path.join(os.path.dirname(__file__), '..', 'pogona', 'objects', 'cavity', '0.5'), walls_patch_names={'fixedWalls'} # 'movingWall', 'frontAndBack'}, # ^ Since the cavity mesh is only one cell thick, # it's important not to specify the frontAndBack walls # as walls here. # Otherwise, the Shepard interpolation implementations # will try to make the flow 0 close to these walls. ), transformation=pg.Transformation()) return VectorFieldMgrSingleton._vfm
def initialize(self, simulation_kernel: 'pg.SimulationKernel', init_stage: 'pg.InitStages'): super().initialize(simulation_kernel, init_stage) if init_stage == pg.InitStages.CHECK_ARGUMENTS: if self.seed == 'random': self._rng = np.random.RandomState(seed=None) elif self.seed != '': self._rng = np.random.RandomState(seed=int(self.seed)) else: self._rng = simulation_kernel.get_random_number_generator() self._transformation = pg.Transformation( translation=np.array(self.translation), rotation=np.array(self.rotation), scaling=np.array([1, 1, 1])) elif init_stage == pg.InitStages.BUILD_SCENE: pass
def get_outlet_area(self, outlet_name: str) -> ( 'pg.Geometry', 'pg.Transformation' ): if outlet_name != "outlet": raise ValueError( f"Outlet \"{outlet_name}\" not found! " "This y-piece only has one outlet named 'outlet'." ) geometry = pg.Geometry(pg.Shapes.CYLINDER) local_transformation = pg.Transformation( translation=np.array([ 0, 0, self.outlet_length - self.outlet_zone / 2 ]), scaling=np.array([ self.radius * 2, self.radius * 2, self.outlet_zone ]) ) return ( geometry, self._transformation.apply_to_transformation(local_transformation) )
def test_translation(): t = pg.Transformation(translation=np.array([1, 2, 3])) p = np.array([5, 1, 2.5]) pp = t.apply_to_point(p) np.testing.assert_allclose(pp, [6, 3, 5.5], **TOLERANCE)