Exemple #1
0
def test_generated_operator_several_output_types(plate_msup):
    inpt = core.Field(nentities=3)
    inpt.data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    inpt.scoping.ids = [1, 2, 3]
    inpt.unit = "m"
    uc = op.math.unit_convert()
    uc.inputs.entity_to_convert(inpt)
    uc.inputs.unit_name("mm")
    f = uc.outputs.converted_entity_as_field()
    assert f.unit == "mm"
    assert np.allclose(
        f.data.flatten("C"), np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) * 1000
    )

    model = core.Model(plate_msup)
    din = model.metadata.meshed_region.nodes.coordinates_field.data

    assert model.metadata.meshed_region.nodes.coordinates_field.unit == "m"

    uc.inputs.entity_to_convert(model.metadata.meshed_region)
    uc.inputs.unit_name("mm")
    m = uc.outputs.converted_entity_as_meshed_region()

    assert m.nodes.coordinates_field.unit == "mm"
    assert np.allclose(m.nodes.coordinates_field.data, np.array(din) * 1000)
Exemple #2
0
def create_mesh_and_field_mapped(multishells):
    # get metadata
    model = core.Model(multishells)
    mesh = model.metadata.meshed_region
    disp_fc = model.results.displacement().outputs.fields_container()
    field = disp_fc[0]
    # coordinates field to map
    coordinates = [[-0.02, 0.006, 0.014], [-0.02, 0.006, 0.012],
                   [-0.018, 0.006, 0.012], [-0.018, 0.006, 0.014]]
    field_coord = core.Field()
    field_coord.location = core.locations.nodal
    field_coord.data = coordinates
    scoping = core.Scoping()
    scoping.location = core.locations.nodal
    scoping.ids = list(range(1, len(coordinates) + 1))
    field_coord.scoping = scoping
    # mapping operator
    mapping_operator = core.Operator("mapping")
    mapping_operator.inputs.fields_container.connect(disp_fc)
    mapping_operator.inputs.coordinates.connect(field_coord)
    mapping_operator.inputs.mesh.connect(mesh)
    mapping_operator.inputs.create_support.connect(True)
    fields_mapped = mapping_operator.outputs.fields_container()
    # mesh path
    assert len(fields_mapped) == 1
    field_m = fields_mapped[0]
    mesh_m = field_m.meshed_region
    return field, field_m, mesh, mesh_m
Exemple #3
0
def test_set_get_data_property_field():
    field = core.Field(nentities=20, nature=dpf.core.natures.scalar)
    scoping = core.Scoping()
    ids = []
    data = []
    for i in range(0, 20):
        ids.append(i + 1)
        data.append(i + 0.001)
    scoping.ids = ids
    field.scoping = scoping
    field.data = data
    assert np.allclose(field.data, data)
Exemple #4
0
def test_generated_operator_several_output_types2():
    inpt = core.Field(nentities=3)
    inpt.data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    inpt.scoping.ids = [1, 2, 3]
    inpt.unit = "m"
    uc = op.scoping.rescope(inpt, core.Scoping(ids=[1, 2]))
    f = uc.outputs.fields_as_field()
    assert np.allclose(f.data.flatten("C"), [1, 2, 3, 4, 5, 6])

    fc = core.FieldsContainer()
    fc.labels = ["time"]
    fc.add_field({"time": 1}, inpt)
    uc = op.scoping.rescope(fc, core.Scoping(ids=[1, 2]))
    fc2 = uc.outputs.fields_as_fields_container()
    assert np.allclose(fc2[0].data.flatten("C"), [1, 2, 3, 4, 5, 6])
    def _set_harmonic_indices_at_stage(self, stage_num, step_harmonic_indices,
                                       step_id):
        """Set values for harmonic indices for a specific stage number.

        Parameters
        ----------
        stage_num: int
            Stage number.
         harmonic_indices: list of int or float
            List of values for harmonic indices.
        """
        harmonic_indices = self.get_harmonic_indices(stage_num)
        if harmonic_indices is None:
            harmonic_indices = core.Field(
                server=self._server,
                nature=core.natures.scalar,
                location=core.locations.time_freq,
            )
            harmonic_indices.scoping.location = core.locations.time_freq_step
        harmonic_indices.append(step_harmonic_indices, step_id)
        self.set_harmonic_indices(harmonic_indices, stage_num)
Exemple #6
0
def test_generated_operator_set_config():
    inpt = core.Field(nentities=3)
    inpt.data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    inpt.scoping.ids = [1, 2, 3]
    inpt.unit = "m"

    inpt2 = core.Field(nentities=3)
    inpt2.data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    inpt2.scoping.ids = [3, 4, 5]
    inpt2.unit = "m"

    conf = op.math.add.default_config()
    print(conf)
    conf.set_work_by_index_option(True)
    op1 = op.math.add(config=conf)
    op1.inputs.fieldA.connect(inpt)
    op1.inputs.fieldB.connect(inpt2)
    out = op1.outputs.field()
    assert np.allclose(out.scoping.ids, [1, 2, 3]) or np.allclose(out.scoping.ids, [3, 4, 5])
    assert np.allclose(
        out.data, np.array([[2.0, 4.0, 6.0], [8.0, 10.0, 12.0], [14.0, 16.0, 18.0]])
    )

    conf.set_work_by_index_option(False)
    op1 = op.math.add(config=conf)
    op1.inputs.fieldA.connect(inpt)
    op1.inputs.fieldB.connect(inpt2)
    out = op1.outputs.field()
    assert np.allclose(out.scoping.ids, [1, 2, 3, 4, 5])
    assert np.allclose(
        out.data,
        np.array(
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
                [8.0, 10.0, 12.0],
                [4.0, 5.0, 6.0],
                [7.0, 8.0, 9.0],
            ]
        ),
    )

    inpt2.unit = "Pa"
    conf = op.math.add.default_config()
    conf.set_permissive_option(True)
    op1.config = conf
    op1.inputs.fieldB.connect(inpt2)
    out = op1.outputs.field()
    assert np.allclose(out.scoping.ids, [1, 2, 3, 4, 5])
    assert np.allclose(
        out.data,
        np.array(
            [
                [1.0, 2.0, 3.0],
                [4.0, 5.0, 6.0],
                [8.0, 10.0, 12.0],
                [4.0, 5.0, 6.0],
                [7.0, 8.0, 9.0],
            ]
        ),
    )

    assert conf.get_mutex_option() == "false"
    assert conf.config_option_default_value("mutex") == "false"
    assert conf.config_option_accepted_types("mutex") == ["bool"]
    assert conf.options["mutex"] == "false"
    assert "multiple threads" in conf.config_option_documentation("mutex")
Exemple #7
0
def create_mesh_and_field_mapped_2(multishells):
    # get metadata
    model = core.Model(multishells)
    mesh = model.metadata.meshed_region
    disp_fc = model.results.displacement().outputs.fields_container()
    field = disp_fc[0]
    # coordinates field to map
    coordinates = [[-0.0195, 0.006, -0.0025]]
    for i in range(1, 101):
        coord_copy = []
        coord_copy.append(coordinates[0][0])
        coord_copy.append(coordinates[0][1])
        coord_copy.append(coordinates[0][2])
        coord_copy[0] = coord_copy[0] + i * 0.0003
        coordinates.append(coord_copy)
    ref = [-0.0155, 0.00600634, -0.0025]
    coordinates.append(ref)
    for i in range(1, 101):
        coord_copy = []
        coord_copy.append(ref[0])
        coord_copy.append(ref[1])
        coord_copy.append(ref[2])
        coord_copy[0] = coord_copy[0] + i * 0.0003
        coordinates.append(coord_copy)
    ref = [-0.0125, 0.00600507, -0.0025]
    coordinates.append(ref)
    for i in range(1, 101):
        coord_copy = []
        coord_copy.append(ref[0])
        coord_copy.append(ref[1])
        coord_copy.append(ref[2])
        coord_copy[0] = coord_copy[0] + i * 0.0003
        coordinates.append(coord_copy)
    ref = [-0.0125, 0.00600444, -0.0025]
    coordinates.append(ref)
    for i in range(1, 101):
        coord_copy = []
        coord_copy.append(ref[0])
        coord_copy.append(ref[1])
        coord_copy.append(ref[2])
        coord_copy[0] = coord_copy[0] + i * 0.0003
        coordinates.append(coord_copy)
    field_coord = core.Field()
    field_coord.location = core.locations.nodal
    field_coord.data = coordinates
    scoping = core.Scoping()
    scoping.location = core.locations.nodal
    scoping.ids = list(range(1, len(coordinates) + 1))
    field_coord.scoping = scoping
    # mapping operator
    mapping_operator = core.Operator("mapping")
    mapping_operator.inputs.fields_container.connect(disp_fc)
    mapping_operator.inputs.coordinates.connect(field_coord)
    mapping_operator.inputs.mesh.connect(mesh)
    mapping_operator.inputs.create_support.connect(True)
    fields_mapped = mapping_operator.outputs.fields_container()
    # mesh path
    assert len(fields_mapped) == 1
    field_m = fields_mapped[0]
    mesh_m = field_m.meshed_region
    return field, field_m, mesh, mesh_m
    def append_step(
        self,
        step_id,
        step_time_frequencies,
        step_complex_frequencies=None,
        rpm_value=None,
        step_harmonic_indices=None,
    ):
        """Append a step with all its field values in the time frequencies support.
        The RPM value is a step (or load step)-based value.
        The values for time frequencies, complex frequencies, and harmonic indices are set-based.
        There is one set value for each step/substep combination.

        It is necessary that each call of my_time_freq_support.append_step(kwargs**) contains
        the same arguments.

        It is necessary that time_frequencies/complex_frequencies/harmonic_indices lists have
        the same size if specified in the same my_time_freq_support.append_step(kwargs**) call.

        Parameters
        ----------
        step_id: int
            ID of the step to add.
        step_time_frequencies: list of int/float
            Values for time frequencies related to the specified step.
        step_complex_frequencies: list of int/float, optional
            Values for complex frequencies related to the specified step.
        rpm_value: int/float, optional
            Value for RPM value for the specified step.
        step_harmonic_indices: optional, dictionary or list
            Dictionary of ``{ int : list of int/float } = { stage_num : harmonic_indices }``
            or a list of int/float. In this case, stage_num default value is 0.
            Harmonic indices are values related to the specified step.

        Example
        -------
        >>> from ansys.dpf.core import TimeFreqSupport

        >>> tfq = TimeFreqSupport()
        >>> tfq.append_step(1, [0.1, 0.21, 1.0], rpm_value = 2.0)
        >>> tfq.append_step(2, [1.1, 2.0], rpm_value = 2.3)

        >>> tfq2 = TimeFreqSupport()
        >>> tfq2.append_step(1, [0.1, 0.21, 1.0], rpm_value = 2.0, step_harmonic_indices = [1.0, 2.0, 3.0])
        >>> tfq2.append_step(2, [1.1, 2.0], rpm_value = 2.3, step_harmonic_indices = [1.0, 2.0])
        >>> tfq2.append_step(3, [0.23, 0.25], rpm_value = 3.0, step_harmonic_indices = [1.0, 2.0])

        >>> tfq3 = TimeFreqSupport()
        >>> tfq3.append_step(1, [0.1, 0.21, 1.0], rpm_value = 2.0, step_harmonic_indices = {1: [1.0, 2.0, 3.0], 2: [1.0, 2.0, 2.5]})

        """  # noqa: E501

        time_frequencies = self.time_frequencies
        if time_frequencies is None:
            time_frequencies = core.Field(
                server=self._server,
                nature=core.natures.scalar,
                location=core.locations.time_freq,
            )
            time_frequencies.scoping.location = core.locations.time_freq_step
        time_frequencies.append(step_time_frequencies, step_id)
        self.time_frequencies = time_frequencies

        if step_complex_frequencies is not None:
            complex_frequencies = self.complex_frequencies
            if complex_frequencies is None:
                complex_frequencies = core.Field(
                    server=self._server,
                    nature=core.natures.scalar,
                    location=core.locations.time_freq,
                )
                complex_frequencies.scoping.location = core.locations.time_freq_step
            complex_frequencies.append(step_complex_frequencies, step_id)
            self.complex_frequencies = complex_frequencies

        if rpm_value is not None:
            rpms = self.rpms
            if rpms is None:
                rpms = core.Field(
                    server=self._server,
                    nature=core.natures.scalar,
                    location=core.locations.time_freq_step,
                )
            rpms.append([rpm_value], step_id)
            self.rpms = rpms

        if step_harmonic_indices is not None:
            if isinstance(step_harmonic_indices, list):
                self._set_harmonic_indices_at_stage(0, step_harmonic_indices,
                                                    step_id)
            elif isinstance(step_harmonic_indices, dict):
                for key in step_harmonic_indices:
                    self._set_harmonic_indices_at_stage(
                        key, step_harmonic_indices[key], step_id)
            else:
                raise dpf_errors.InvalidTypeError("list/dict",
                                                  "step_harmonic_indices")