Пример #1
0
    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])
Пример #2
0
 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)
     )
Пример #3
0
    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."""
Пример #4
0
 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
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
    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
Пример #8
0
 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)
     )