def plot(self, fields_container=None, **kwargs): """Plot the meshes container with a specific result if fields_container is specified. Parameters ---------- fields_container : FieldsContainer, optional Data to plot. The default is ``None``. Examples -------- >>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> model = dpf.Model(examples.multishells_rst) >>> mesh = model.metadata.meshed_region >>> split_mesh_op = dpf.operators.mesh.split_mesh(mesh=mesh, property="mat") >>> meshes_cont = split_mesh_op.eval() >>> disp_op = dpf.operators.result.displacement( ... data_sources = dpf.DataSources(examples.multishells_rst), ... mesh = meshes_cont ... ) >>> disp_fc = disp_op.outputs.fields_container() >>> meshes_cont.plot(disp_fc) """ kwargs.setdefault("show_edges", True) notebook = kwargs.pop("notebook", None) pl = DpfPlotter(notebook=notebook) if fields_container is not None: for i in range(len(fields_container)): label_space = fields_container.get_label_space(i) mesh_to_send = self.get_mesh(label_space) if mesh_to_send is None: raise dpf_errors.DpfValueError( "Meshes container and result fields " "container do not have the same scope. " "Plotting can not proceed. ") field = fields_container[i] pl.add_field(field, mesh_to_send, **kwargs) else: from random import random random_color = "color" not in kwargs for mesh in self: if random_color: kwargs["color"] = [random(), random(), random()] pl.add_mesh(mesh, **kwargs) pl.show_figure()
def get_imaginary_field(self, timeid=None): """Retrieve the complex field at a requested time. Parameters ---------- timeid: int, optional Time ID, which is the one-based index of the result set. Returns ------- fields : Field Field corresponding to the request. """ if not self.has_label("complex") or not self.has_label("time"): raise dpf_errors.DpfValueError( "The fields container is not based on time and complex scoping." ) label_space = self.__time_complex_label_space__(timeid, 1) return super()._get_entry(label_space)
def add_imaginary_field(self, field, timeid=1): """Add or update an imaginary field at a requested time ID. Parameters ---------- field : Field DPF field to add or update. timeid: int, optional Time ID for the requested time set. The default is ``1``. """ if not self.has_label("time") and (len(self.labels) == 0 or (len(self.labels) == 1 and self.has_label("complex"))): self.add_label("time") if (not self.has_label("complex") and len(self.labels) == 1 and self.has_label("time")): self.add_label("complex") if (self.has_label("time") and self.has_label("complex") and len(self.labels) == 2): super()._add_entry({"time": timeid, "complex": 1}, field) else: raise dpf_errors.DpfValueError( "The fields container is not only based on time scoping.")
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