PyDPF and Mechanical produce different current density results.

Options
jwkim
jwkim Member Posts: 6
Name Dropper First Comment First Anniversary

I wrote the code below, but the current density value in the automation program using PyDPF and the current density value in Mechanical are different, so how can I output the same?


`
def plot_result(self):
try:
self.plotter.clear()
rst = runpath2+"/WB/Busbar_files/dp0/SYS/MECH/file.rst"
model = dpf.Model(rst)
meshed=model.metadata.meshed_region
t_f=model.metadata.time_freq_support

    time_1 = list(t_f.time_frequencies.data)

    dt_times=[str(round(items,2)) for items in time_1]

    if self.dt_box.currentText()=="Last Time":
        index = -1
        if self.dt_box.count() == 1:
            self.dt_box.addItems(dt_times)
            self.dt_box.removeItem(0)
    else:
        index = self.dt_box.currentIndex()

    if self.model_box.currentText()=="Full Model":
        part_name = "MESH_SZ"
    else:
        part_name = self.model_box.currentText()
        part_name = part_name.upper()
        part_name = "NS_" + part_name

    if self.radio_temp.isChecked():
        my_mesh_scoping = model.metadata.named_selection(part_name)
        scoping_op = dpf.operators.mesh.from_scoping()
        scoping_op.inputs.scoping.connect(my_mesh_scoping)
        scoping_op.inputs.mesh.connect(meshed)
        my_mesh = scoping_op.outputs.mesh()

        if index == 0:
            index2 = 0
            get_all_temp = model.results.temperature.on_all_time_freqs
            get_fieldContents_temp = get_all_temp(mesh_scoping=my_mesh_scoping).eval()
            get_field_temp = get_fieldContents_temp[index2]
        elif index == -1:
            index2 = -1
            get_all_temp = model.results.temperature.on_last_time_freq
            get_fieldContents_temp = get_all_temp(mesh_scoping=my_mesh_scoping).eval()
            get_field_temp = get_fieldContents_temp[0]
        else:
            index2 =  index * 2
            get_all_temp = model.results.temperature.on_all_time_freqs
            get_fieldContents_temp = get_all_temp(mesh_scoping=my_mesh_scoping).eval()
            get_field_temp = get_fieldContents_temp[index2]

        res_nodal_op=dpf.operators.averaging.elemental_nodal_to_nodal()
        my_field = get_field_temp

        res_nodal_op.inputs.field.connect(my_field)
        get_field_temp=res_nodal_op.outputs.field()

        field = get_field_temp

        meshed_region = my_mesh

        mesh_location = meshed_region.nodes
        component_count = field.component_count

        overall_data = np.full((len(mesh_location), component_count), np.nan)
        ind, mask = mesh_location.map_scoping(field.scoping)
        value_array = field.data[mask].reshape(-1, 1)
        overall_data[ind] = value_array

        grid = meshed_region.grid

        max_temp = max(overall_data)
        min_temp = min(overall_data)

        self.pd = pv.UnstructuredGrid(grid.cells,grid.celltypes, grid.points)

        title2= "Temperature($^\circ$C) \n"+"Time ="+str(round(time_1[index],2))

        sbar_kwargs =dict(title_font_size=18, label_font_size=15, height=0.8, width=0.12, vertical=True, interactive=False, position_x=0.05, position_y=0.1, color="black", title=title2, n_labels=6, fmt="%10.3f")
        self.plotter.enable_point_picking(left_clicking=True,pickable_window=False)
        self.plotter.add_mesh(self.pd,pickable=True, scalars=overall_data, show_scalar_bar=True, scalar_bar_args=sbar_kwargs, show_edges=False, cmap='jet', rng=(min_temp, max_temp))



    elif self.radio_hf.isChecked():
        my_mesh_scoping = model.metadata.named_selection(part_name)
        scoping_op = dpf.operators.mesh.from_scoping()
        scoping_op.inputs.scoping.connect(my_mesh_scoping)
        scoping_op.inputs.mesh.connect(meshed)

        my_mesh = scoping_op.outputs.mesh()

        if index == 0:
            index = 0
            get_all_currentdensity = model.results.current_density.on_all_time_freqs
            get_fieldContents_currentdensity = get_all_currentdensity(mesh_scoping=my_mesh_scoping).eval()
            get_field_currentdensity = get_fieldContents_currentdensity[index]
        elif index == -1:
            index = -1
            get_all_currentdensity = model.results.current_density.on_last_time_freq
            get_fieldContents_currentdensity = get_all_currentdensity(mesh_scoping=my_mesh_scoping).eval()
            get_field_currentdensity = get_fieldContents_currentdensity[0]
        else:
            get_all_currentdensity = model.results.current_density.on_all_time_freqs
            get_fieldContents_currentdensity = get_all_currentdensity(mesh_scoping=my_mesh_scoping).eval()
            get_field_currentdensity = get_fieldContents_currentdensity[index]

        res_nodal_op=dpf.operators.averaging.elemental_nodal_to_nodal()
        my_field = get_field_currentdensity
        res_nodal_op.inputs.field.connect(my_field)
        get_field_currentdensity=res_nodal_op.outputs.field()

        field = get_field_currentdensity
        meshed_region = my_mesh

        mesh_location = meshed_region.nodes
        component_count = field.component_count

        overall_data = np.full((len(mesh_location), component_count), np.nan)
        ind, mask = mesh_location.map_scoping(field.scoping)
        overall_data[ind] = field.data[mask]

        grid = meshed_region.grid
        grid.set_active_scalars(None)

        scaling = 5e-4
        grid["my_vectors"] = overall_data * scaling
        grid.set_active_vectors("my_vectors")
        vector_magnitudes = np.linalg.norm(overall_data, axis=1)
        max_temp = max(vector_magnitudes)
        min_temp = min(vector_magnitudes)

        title2= "       Current Density(A/m\u00B2) \n"+"Time ="+str(round(time_1[index],2))

        sbar_kwargs =dict(title_font_size=18, label_font_size=15, height=0.8, width=0.12, vertical=True, interactive=False, position_x=0.05, position_y=0.1, color="black", title=title2, n_labels=6)#, fmt="%10.3f")
        self.plotter.add_mesh(grid.arrows, lighting=False, show_scalar_bar=False) 
        self.plotter.add_mesh(grid, scalars= overall_data, show_edges=False, scalar_bar_args=sbar_kwargs, cmap='jet', rng=(min_temp, max_temp))

    self.plotter.window_size = [859, 429]
    self.plotter.add_camera_orientation_widget()
    self.plotter.camera_position='xy'

`