def test_add_imaginary_field(): fc = FieldsContainer() fc.labels = ["time", "complex"] f1 = Field(3) f1.append([10.2, 3.0, -11.8], 1) f1.append([10.2, 2.0, 11.8], 2) f1.append([10.2, 1.0, -11.8], 3) mscop1 = {"time": 1, "complex": 1} fc.add_field(mscop1, f1) assert len(fc) == 1 f2 = Field(1) f2.append([4.0, 4.4, 3.6], 1) mscop2 = {"time": 1, "complex": 0} fc.add_field(mscop2, f2) assert len(fc) == 2 f3 = Field(1) f3.append([0.0, 0.4, 0.6], 1) fc.add_imaginary_field(f3, 2) field_to_compare = Field(1) field_to_compare.append([0.0, 0.4, 0.6], 1) field = fc.get_field({"time": 2, "complex": 1}) assert len(fc) == 3 assert np.allclose(field.data, field_to_compare.data) fc.add_imaginary_field(f3, 1) field_result_1 = fc.get_field({"time": 1, "complex": 1}) field_result_2 = fc.get_field({"time": 2, "complex": 1}) assert np.allclose(field_result_1.data, field_result_2.data) fc.add_label("body") with pytest.raises(dpf_errors.DpfValueError): fc.add_imaginary_field(f3, 10)
def over_time_freq_fields_container(fields, time_freq_unit=None, server=None): """Create a fields container with one field by time set. This method can also set the time frequency support with the correct unit if needed. Parameters ---------- fields : Dictionary(time_int_key : Field) or list of Field Dictionary of field entities to add to the fields container. time_freq_unit : str, optional Unit of the time frequency support, which is taken into account if the fields attribute has a dictionary type. The default is ``None``. server : ansys.dpf.core.server, optional Server with the channel connected to the remote or local instance. The default is ``None``, in which case an attempt is made to use the global server. Returns ------- fields_container : FieldsContainer Fields container containing one field by time step. Examples -------- Create a fields container from scratch based on time labels. >>> from ansys.dpf import core as dpf >>> field1 = dpf.Field() >>> field2 = dpf.Field() >>> from ansys.dpf.core import fields_container_factory >>> my_fc = fields_container_factory.over_time_freq_fields_container([ field1, field2 ]) """ if not isinstance(fields, dict) and not isinstance(fields, list): raise dpf_errors.InvalidTypeError("dictionary/list", "fields") fc = FieldsContainer(server=server) fc.labels = {"time"} i = 0 # dict case if isinstance(fields, dict): time_freq = [] for field_key in fields: fc.add_field({"time": i + 1}, fields[field_key]) time_freq.append(field_key) i += 1 time_freq_field = fields_factory.create_scalar_field( len(fields), location=locations.time_freq, server=server) time_freq_field.append(time_freq, 1) time_freq_field.unit = time_freq_unit time_freq_support = TimeFreqSupport(server=server) time_freq_support.time_frequencies = time_freq_field fc.time_freq_support = time_freq_support # list case elif isinstance(fields, list): for field in fields: fc.add_field({"time": i + 1}, field) i += 1 return fc
def test_get_item_field_fields_container(): fc = FieldsContainer() fc.labels = ["time", "complex"] for i in range(0, 20): mscop = {"time": i + 1, "complex": 0} fc.add_field(mscop, Field(nentities=i + 10)) for i in range(0, 20): assert fc[i]._message.id != 0
def test_get_label_scoping(): fc = FieldsContainer() fc.labels = ["time", "complex"] for i in range(0, 20): mscop = {"time": i + 1, "complex": 0} fc.add_field(mscop, Field(nentities=i + 10)) scop = fc.get_label_scoping() assert scop._message.id != 0 assert scop.ids == list(range(1, 21))
def test_light_copy(): fc = FieldsContainer() fc.labels = ["time"] field = Field(1) field.append([0.0, 3.0, 4.1], 20) fc.add_field({"time": 1}, field) assert fc[0] != None fc2 = FieldsContainer(fields_container=fc) assert fc2[0] != None fc = 2 assert fc2[0] != None
def test_set_get_field_fields_container(): fc = FieldsContainer() fc.labels = ["time", "complex"] for i in range(0, 20): mscop = {"time": i + 1, "complex": 0} fc.add_field(mscop, Field(nentities=i + 10)) assert fc.get_available_ids_for_label() == list(range(1, 21)) for i in range(0, 20): fieldid = fc.get_field({"time": i + 1, "complex": 0})._message.id assert fieldid != 0 assert fc.get_field(i)._message.id != 0 assert (fc.get_field_by_time_complex_ids(timeid=i + 1, complexid=0)._message.id != 0) assert fc[i]._message.id != 0
def test_get_imaginary_field(disp_fc): with pytest.raises(dpf_errors.DpfValueError): disp_fc.get_imaginary_fields(1) fc = FieldsContainer() fc.labels = ["complex"] with pytest.raises(dpf_errors.DpfValueError): fc.get_imaginary_fields(1) fc = FieldsContainer() fc.labels = ["time", "complex"] field_real = Field(1) field_real.append([0.0, 3.0, 4.1], 20) fc.add_field({"time": 1, "complex": 0}, field_real) field_to_check = fc.get_imaginary_field(1) assert field_to_check is None field_img = Field(1) field_img.append([1.0, 301.2, 4.2], 20) fc.add_field({"time": 1, "complex": 1}, field_img) field_to_check_2 = fc.get_imaginary_field(1) assert np.allclose(field_img.data, field_to_check_2.data)
def test_set_get_field_fields_container_new_label_default_value(): fc = FieldsContainer() fc.labels = ["time", "complex"] for i in range(0, 20): mscop = {"time": i + 1, "complex": 0} fc.add_field(mscop, Field(nentities=i + 10)) fc.add_label("shape", 3) for i in range(0, 20): mscop = {"time": i + 1, "complex": 0, "shape": 1} fc.add_field(mscop, Field(nentities=i + 10)) for i in range(0, 20): fieldid = fc.get_field({ "time": i + 1, "complex": 0, "shape": 1 })._message.id assert fieldid != 0 assert fc.get_field(i + 20)._message.id != 0 assert fc[i]._message.id != 0 assert fc.get_label_space(i + 20) == { "time": i + 1, "complex": 0, "shape": 1 } for i in range(0, 20): fieldid = fc.get_field({ "time": i + 1, "complex": 0, "shape": 3 })._message.id assert fieldid != 0 assert fc.get_field(i)._message.id != 0 assert fc[i]._message.id != 0 assert fc.get_label_space(i) == { "time": i + 1, "complex": 0, "shape": 3 }
def complex_fields_container(real_field, imaginary_field, server=None): """Create a fields container with two fields (real and imaginary) and only one time set. Parameters ---------- real_fields : Field Real :class:`ansys.dpf.core.Field` entity to add to the fields container. imaginary_fields : Field Imaginary :class:`ansys.dpf.core.Field` entity to add to the fields container. server : ansys.dpf.core.server, optional Server with the channel connected to the remote or local instance. The default is ``None``, in which case an attempt is made to use the global server. Returns ------- fields_container : FieldsContainer Fields container with two fields (real and imaginary). """ fc = FieldsContainer(server=server) fc.labels = ["complex"] fc.add_field({"complex": 0}, real_field) fc.add_field({"complex": 1}, imaginary_field) return fc
def over_time_freq_complex_fields_container(real_fields, imaginary_fields, time_freq_unit=None, server=None): """Create a fields container with two fields (real and imaginary) by time set. If the inputs for the fields are dictionaries, this method sets the time frequency support with the correct unit if needed. Parameters ---------- real_fields : Dictionary(time_int_key : Field) or list of Field Dictionary or list of field entities to add to the fields container. imaginary_fields : Dictionary(time_int_key : Field) or list of Field Dictionary or list of field entities to add to the fields container. time_freq_unit : str , optional Unit of the time frequency support, which is taken into account if the field's attribute has a dictionary type. server : ansys.dpf.core.server, optional Server with the channel connected to the remote or local instance. The default is ``None``, in which case an attempt is made to use the global server. Returns ------- fields_container : FieldsContainer Fields container containing two fields (real and imaginary) by time step. """ if not isinstance(real_fields, dict) and not isinstance(real_fields, list): raise dpf_errors.InvalidTypeError("dictionary/list", "real_fields") if not isinstance(imaginary_fields, dict) and not isinstance( imaginary_fields, list): raise dpf_errors.InvalidTypeError("dictionary/list", "imaginary_fields") errorString = ( "Both real_fields and imaginary_fields must have the same type (list or dict)" ) if isinstance(real_fields, dict): if not isinstance(imaginary_fields, dict): raise dpf_errors.DpfValueError(errorString) elif isinstance(real_fields, list): if not isinstance(imaginary_fields, list): raise dpf_errors.DpfValueError(errorString) fc = FieldsContainer(server=server) fc.labels = ["time", "complex"] i = 0 # dict case if isinstance(real_fields, dict): time_freq = [] for field_key in real_fields: fc.add_field({"time": i + 1, "complex": 0}, real_fields[field_key]) time_freq.append(field_key) i += 1 i = 0 im_time_freq = [] for field_key in imaginary_fields: fc.add_field({ "time": i + 1, "complex": 1 }, imaginary_fields[field_key]) im_time_freq.append(field_key) i += 1 time_freq_field = fields_factory.create_scalar_field( len(real_fields), locations.time_freq, server=server) time_freq_field.append(time_freq, 1) time_freq_field.unit = time_freq_unit im_time_freq_field = fields_factory.create_scalar_field( len(imaginary_fields), locations.time_freq, server=server) im_time_freq_field.append(im_time_freq, 1) im_time_freq_field.unit = time_freq_unit time_freq_support = TimeFreqSupport(server=server) time_freq_support.time_frequencies = time_freq_field time_freq_support.complex_frequencies = im_time_freq_field fc.time_freq_support = time_freq_support # list case if isinstance(real_fields, list): for field in real_fields: fc.add_field({"time": i + 1, "complex": 0}, field) i += 1 i = 0 for field in imaginary_fields: fc.add_field({"time": i + 1, "complex": 1}, field) i += 1 return fc