def cyc_models(local_server): otherfile = dpf.upload_file_in_tmp_folder(examples.simple_cyclic, server=local_server) return ( dpf.Model(dpf.upload_file_in_tmp_folder(examples.simple_cyclic)), dpf.Model(otherfile, server=local_server), )
def transient_models(local_server): otherfile = dpf.upload_file_in_tmp_folder(examples.msup_transient, server=local_server) return ( dpf.Model(dpf.upload_file_in_tmp_folder(examples.msup_transient)), dpf.Model(otherfile, server=local_server), )
def test_mismatch_instances_cache(simple_bar): model = dpf.Model(simple_bar) model2 = dpf.Model(simple_bar) mesh = model.metadata.meshed_region mesh2 = model2.metadata.meshed_region initunit = mesh.unit assert len(mesh._cache.cached) == 1 assert len(mesh2._cache.cached) == 0 assert mesh.unit == initunit mesh.unit = "cm" assert len(mesh._cache.cached) == 0 mesh2.unit assert len(mesh2._cache.cached) == 1
def test_create_op_with_input_model(plate_msup): model = core.Model(plate_msup) u = core.operators.result.displacement(time_scoping=0.015, data_sources=model) norm = core.operators.math.norm_fc(u) fc = norm.outputs.fields_container() assert len(fc) == 1 assert np.allclose(fc[0].data[0], [0.00036435444541115566])
def test_throw_complex_file(complex_model): model = core.Model(complex_model) stress = model.results.displacement() fc = stress.outputs.fields_container() mesh = model.metadata.meshed_region with pytest.raises(dpf_errors.ComplexPlottingError): mesh.plot(fc)
def test_operator_any_input(allkindofcomplexity): serialization = core.operators.serialization.serializer() model = core.Model(allkindofcomplexity) u = model.results.displacement() serialization.inputs.any_input1.connect(u.outputs) serialization.inputs.any_input2.connect(u.outputs.fields_container) serialization.inputs.any_input3.connect(u.outputs) # create a temporary file at the default temp directory path = os.path.join(tempfile.gettempdir(), "dpf_temp_ser.txt") serialization.inputs.file_path(path) serialization.run() deser = core.operators.serialization.deserializer() deser.inputs.connect(path) fc = deser.get_output(1, core.types.fields_container) assert len(fc) == 1 fc = deser.get_output(2, core.types.fields_container) assert len(fc) == 1 fc = deser.get_output(2, core.types.fields_container) assert len(fc) == 1 assert hasattr(fc, "outputs") == False if os.path.exists(path): os.remove(path)
def test_cff(cff_data_sources): m = dpf.Model(cff_data_sources) assert m.metadata.meshed_region.nodes.n_nodes == 1430 op = dpf.Operator("cff::cas::SV_DENSITY") op.connect(4, m.metadata.data_sources) fc = op.get_output(0, dpf.types.fields_container) assert len(fc[0]) == 1380
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
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)
def test_plot_meshes_container_only(multishells): model = core.Model(multishells) mesh = model.metadata.meshed_region split_mesh_op = core.Operator("split_mesh") split_mesh_op.connect(7, mesh) split_mesh_op.connect(13, "mat") meshes_cont = split_mesh_op.get_output(0, core.types.meshes_container) meshes_cont.plot()
def test_calloperators(allkindofcomplexity): my_data_sources = core.DataSources(allkindofcomplexity) my_model = core.Model(my_data_sources) displacement_op = my_model.results.displacement() assert isinstance(displacement_op, ansys.dpf.core.dpf_operator.Operator) norm_op = my_model.operator("norm_fc") assert isinstance(norm_op, ansys.dpf.core.dpf_operator.Operator) square_op = core.operators.math.sqr() assert isinstance(square_op, ansys.dpf.core.operators.math.sqr)
def test_mat_time_fc(): model = dpf.Model(examples.download_all_kinds_of_complexity_modal()) fc = model.results.stress.on_all_time_freqs.split_by_body.eval() assert isinstance(fc, BodyFieldsContainer) assert len(fc.get_fields_by_mat_id(45)) == 45 assert np.allclose( fc.get_fields_by_mat_id(45)[0].data, fc.get_field_by_mat_id(45, 1).data) assert len(fc.get_mat_scoping().ids) == 32
def test_named_selections_mesh_cache(simple_bar): model = dpf.Model(simple_bar) mesh = model.metadata.meshed_region init = mesh.available_named_selections assert len(mesh._cache.cached) == 1 assert mesh.available_named_selections == init assert len(mesh._cache.cached) == 1 ns = mesh.named_selection(init[0]) assert len(mesh._cache.cached) == 2
def test_hdf5_ellipsis_any_pins(simple_bar, tmpdir): tmp_path = str(tmpdir.join("hdf5.h5")) model = core.Model(simple_bar) u = model.results.displacement() s = model.operator("S") op = core.Operator("serialize_to_hdf5") op.inputs.file_path.connect(tmp_path) op.inputs.data1.connect(u.outputs) op.inputs.data2.connect(s.outputs) assert len(op.inputs._connected_inputs) == 3
def test_unit_mesh_cache(simple_bar): model = dpf.Model(simple_bar) mesh = model.metadata.meshed_region initunit = mesh.unit assert len(mesh._cache.cached) == 1 assert mesh.unit == initunit mesh.unit = "cm" assert len(mesh._cache.cached) == 0 assert mesh.unit == "cm" assert len(mesh._cache.cached) == 1
def test_plotter_add_mesh(multishells): model = core.Model(multishells) mesh = model.metadata.meshed_region split_mesh_op = core.Operator("split_mesh") split_mesh_op.connect(7, mesh) split_mesh_op.connect(13, "mat") meshes_cont = split_mesh_op.get_output(0, core.types.meshes_container) from ansys.dpf.core.plotter import DpfPlotter pl = DpfPlotter() for i in range(len(meshes_cont) - 10): pl.add_mesh(meshes_cont[i]) pl.show_figure()
def test_throw_on_several_time_steps(plate_msup): model = core.Model(plate_msup) scoping = core.Scoping() scoping.ids = range( 3, len(model.metadata.time_freq_support.time_frequencies) + 1) stress = model.results.displacement() stress.inputs.time_scoping.connect(scoping) fc = stress.outputs.fields_container() mesh = model.metadata.meshed_region with pytest.raises(dpf_errors.FieldContainerPlottingError): mesh.plot(fc)
def test_deep_copy_over_time_fields_container(velocity_acceleration): model = dpf.Model(velocity_acceleration) stress = model.results.stress(time_scoping=[1, 2, 3]) fc = stress.outputs.fields_container() copy = fc.deep_copy() idenfc = dpf.operators.logic.identical_fc(fc, copy) assert idenfc.outputs.boolean() tf = fc.time_freq_support copy = copy.time_freq_support assert np.allclose(tf.time_frequencies.data, copy.time_frequencies.data) assert tf.time_frequencies.scoping.ids == copy.time_frequencies.scoping.ids
def test_plot_meshes_container_1(multishells): model = core.Model(multishells) mesh = model.metadata.meshed_region split_mesh_op = core.Operator("split_mesh") split_mesh_op.connect(7, mesh) split_mesh_op.connect(13, "mat") meshes_cont = split_mesh_op.get_output(0, core.types.meshes_container) disp_op = core.Operator("U") disp_op.connect(7, meshes_cont) ds = core.DataSources(multishells) disp_op.connect(4, ds) disp_fc = disp_op.outputs.fields_container() meshes_cont.plot(disp_fc)
def test_field_shell_plot_scoping_elemental(multishells): model = core.Model(multishells) mesh = model.metadata.meshed_region stress = model.results.stress() scoping = core.Scoping() scoping.location = "Elemental" l = list(range(3000, 4500)) scoping.ids = l stress.inputs.mesh_scoping.connect(scoping) avg = core.Operator("to_elemental_fc") avg.inputs.fields_container.connect(stress.outputs.fields_container) s = avg.outputs.fields_container() f = s[1] f.plot(shell_layers=core.shell_layers.top)
def test_plot_fields_on_mesh_scoping_title(multishells): model = core.Model(multishells) mesh = model.metadata.meshed_region stress = model.results.stress() stress.inputs.requested_location.connect("Nodal") scoping = core.Scoping() scoping.location = "Nodal" l = list(range(0, 400)) l += list(range(1500, 2000)) l += list(range(2200, 2600)) scoping.ids = l stress.inputs.mesh_scoping.connect(scoping) s = stress.outputs.fields_container() mesh.plot(s[0], text="test")
def test_makeconnections(allkindofcomplexity): my_data_sources = core.DataSources(allkindofcomplexity) my_model = core.Model(my_data_sources) displacement_op = my_model.results.displacement() norm_op = my_model.operator("norm") square_op = core.operators.math.sqr() assert len(displacement_op.inputs._connected_inputs) == 2 assert len(norm_op.inputs._connected_inputs) == 0 # assert len(square_op.inputs._connected_inputs)==0 norm_op.inputs.connect(displacement_op.outputs) square_op.inputs.field.connect(norm_op.outputs.field) assert len(displacement_op.inputs._connected_inputs) == 2 assert len(norm_op.inputs._connected_inputs) == 1 assert len(square_op.inputs._connected_inputs) == 1 square_op.inputs.connect(norm_op.outputs.field) assert len(square_op.inputs._connected_inputs) == 1 square_op.inputs.field.connect(norm_op.outputs) assert len(square_op.inputs._connected_inputs) == 1
def test_eng(engineering_data_sources): dpf.load_library("composite_operators.dll", "compo") dpf.load_library("Ans.Dpf.EngineeringData.dll", "eng") m = dpf.Model(engineering_data_sources) stress_op = dpf.operators.result.stress() stress_op.inputs.data_sources.connect(engineering_data_sources) result_info_provider = dpf.operators.metadata.result_info_provider() result_info_provider.inputs.data_sources.connect(engineering_data_sources) mat_support_operator = dpf.operators.metadata.material_support_provider() mat_support_operator.inputs.data_sources.connect(engineering_data_sources) ans_mat_operator = dpf.Operator("eng_data::ans_mat_material_provider") ans_mat_operator.connect(0, mat_support_operator, 0) ans_mat_operator.connect(1, result_info_provider, 0) ans_mat_operator.connect(4, engineering_data_sources) field_variable_provider = dpf.Operator( "composite::inistate_field_variables_provider") field_variable_provider.connect(4, engineering_data_sources) field_variable_provider.inputs.mesh.connect(m.metadata.mesh_provider) field_variable_provider.run()
def test_el_shape_fc(allkindofcomplexity): model = dpf.Model(allkindofcomplexity) fc = model.results.stress.split_by_shape.eval() assert isinstance(fc, ElShapeFieldsContainer) assert len(fc.beam_fields()) == 1 assert len(fc.shell_fields()) == 1 assert len(fc.solid_fields()) == 1 mesh = model.metadata.meshed_region f = fc.beam_field() for id in f.scoping.ids: assert mesh.elements.element_by_id(id).shape == "beam" f = fc.shell_field() for id in f.scoping.ids: assert mesh.elements.element_by_id(id).shape == "shell" f = fc.solid_field() for id in f.scoping.ids: assert mesh.elements.element_by_id(id).shape == "solid"
def test_connect_output_to_inputs(plate_msup): my_model = core.Model(plate_msup) s = my_model.results.stress() to_elemental = op.averaging.to_elemental_fc() to_elemental.inputs.connect(s.outputs) comp = op.logic.component_selector_fc() comp.inputs.component_number.connect(2) comp.inputs.connect(to_elemental.outputs) min_max = op.min_max.min_max_over_label_fc() min_max.inputs.connect(comp.outputs) min_max.inputs.label.connect("time") scale = op.math.scale(min_max.outputs.field_max, 0.5) high_pass = op.filter.field_high_pass_fc(comp, scale) fields = high_pass.outputs.fields_container() assert len(fields) == 1
def test_plot_meshes_container_2(multishells): from ansys.dpf import core model = core.Model(multishells) mesh = model.metadata.meshed_region split_mesh_op = core.Operator("split_mesh") split_mesh_op.connect(7, mesh) split_mesh_op.connect(13, "mat") meshes_cont = split_mesh_op.get_output(0, core.types.meshes_container) disp_op = core.Operator("U") disp_op.connect(7, meshes_cont) ds = core.DataSources(multishells) disp_op.connect(4, ds) disp_fc = disp_op.outputs.fields_container() meshes_cont_2 = core.MeshesContainer() meshes_cont_2.labels = meshes_cont.labels disp_fc_2 = core.FieldsContainer() disp_fc_2.labels = meshes_cont.labels for i in range(len(meshes_cont) - 10): lab = meshes_cont.get_label_space(i) meshes_cont_2.add_mesh(lab, meshes_cont.get_mesh(lab)) disp_fc_2.add_field(lab, disp_fc.get_field(lab)) meshes_cont_2.plot(disp_fc_2)
def test_el_shape_time_fc(): model = dpf.Model(examples.download_all_kinds_of_complexity_modal()) fc = model.results.stress.on_all_time_freqs.split_by_shape.eval() assert isinstance(fc, ElShapeFieldsContainer) assert len(fc.beam_fields()) == 45 assert len(fc.shell_fields()) == 45 assert len(fc.solid_fields()) == 45 assert len(fc.beam_fields(1)) == 1 assert len(fc.shell_fields(3)) == 1 assert len(fc.solid_fields(20)) == 1 mesh = model.metadata.meshed_region f = fc.beam_field(3) for id in f.scoping.ids: assert mesh.elements.element_by_id(id).shape == "beam" f = fc.shell_field(4) for id in f.scoping.ids: assert mesh.elements.element_by_id(id).shape == "shell" f = fc.solid_field(5) for id in f.scoping.ids: assert mesh.elements.element_by_id(id).shape == "solid"
Use Result Helpers to compare mode shapes for solids and then shells ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The `Result` class which instances are created by the `Model` gives access to helpers to request results on specific mesh and time scopings. With those helpers, working on a custom spatial and temporal subset of the model is straightforward. """ from ansys.dpf import core as dpf from ansys.dpf.core import examples ############################################################################### # First, create a model object to establish a connection with an # example result file model = dpf.Model(examples.download_all_kinds_of_complexity_modal()) print(model) ############################################################################### # Visualize specific mode shapes # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Choose the modes to visualize modes = [1, 5, 10, 15] ############################################################################### # Choose to split the displacement on solid/shell/beam to only focus on shell # elements disp = model.results.displacement for mode in modes: fc = disp.on_time_scoping(mode).split_by_shape.eval() model.metadata.meshed_region.plot(fc.shell_field())
performance. Using the ``as_local_field`` option brings the data from the server to your local machine where you can work on it. When finished, you send the updated data back to the server in one transaction. Import necessary modules: """ from ansys.dpf import core as dpf from ansys.dpf.core import examples from ansys.dpf.core import operators as ops ############################################################################### # Create a model object to establish a connection with an # example result file and then extract: model = dpf.Model(examples.download_multi_stage_cyclic_result()) print(model) ############################################################################### # Create the Workflow # ~~~~~~~~~~~~~~~~~~~~ # Maximum principal stress usually occurs on the skin of the # model. Computing results only on this skin reduces the data size. # Create a simple workflow computing the principal stress on the skin # of the model. skin_op = ops.mesh.external_layer(model.metadata.meshed_region) skin_mesh = skin_op.outputs.mesh() ###############################################################################
""" .. _ref_basic_cyclic: Modal Cyclic symmetry Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This example shows how to expand a cyclic mesh and its results. """ from ansys.dpf import core as dpf from ansys.dpf.core import examples ############################################################################### # Create the model and display the state of the result. model = dpf.Model(examples.simple_cyclic) print(model) ############################################################################### # Expand displacement results # ~~~~~~~~~~~~~~~~~~~~~~~~~~~ # In this example we expand displacement results, by default on all # nodes and the first time step. # Create displacement cyclic operator u_cyc = model.operator("mapdl::rst::U_cyclic") # expand the displacements fields = u_cyc.outputs.fields_container() # # get the expanded mesh mesh_provider = model.metadata.mesh_provider