.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/50-advanced/01-two_simulation_same_db.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_50-advanced_01-two_simulation_same_db.py: .. _two_simulation_same_db: Multiple simulations in same database ===================================== Use a single database and report template for multiple simluation results. .. note:: This example assumes that you have a local Ansys installation. .. GENERATED FROM PYTHON SOURCE LINES 15-20 Start an Ansys Dynamic Reporting service ---------------------------------------- Start an Ansys Dynamic Reporting service with a new database. The path for the database directory must be to an empty directory. .. GENERATED FROM PYTHON SOURCE LINES 20-37 .. code-block:: Python from random import random as r import numpy as np import ansys.dynamicreporting.core as adr from ansys.dynamicreporting.core.utils import report_utils # Find a random open port for the ADR service adr_port = report_utils.find_unused_ports(1)[0] adr_service = adr.Service( ansys_installation=r"C:\Program Files\Ansys Inc\v241", db_directory=r"D:\tmp\new_db", port=adr_port, ) session_guid = adr_service.start(create_db=True) .. GENERATED FROM PYTHON SOURCE LINES 38-47 Set methods for items and report template creation -------------------------------------------------- Define two methods: one to create items associated with a design point simulation, and a method to create the report template for a simulation report. The first method to create items is mocking what a real simulation would produce: a set of items pushed directly into the database. Please note that each item is tagged with the corresponding design point. .. GENERATED FROM PYTHON SOURCE LINES 47-130 .. code-block:: Python def create_items(dp=0) -> None: intro_text = adr_service.create_item() intro_text.item_text = "This section describes the settings for the simulation: initial conditions, solver settings, and such." intro_text.set_tags(f"dp=dp{str(dp)} section=intro") intro_tree = adr_service.create_item() mytree = [] mytree.append({"key": "root", "name": "Solver", "value": "My Solver"}) mytree.append({"key": "root", "name": "Number cells", "value": 10e6}) mytree.append({"key": "root", "name": "Mesh Size", "value": "1.0 mm^3"}) mytree.append({"key": "root", "name": "Mesh Type", "value": "Hex8"}) intro_tree.item_tree = mytree intro_tree.set_tags(f"dp=dp{str(dp)} section=intro") data_table = adr_service.create_item() ics = [] ips = [] zet = [] for i in range(30): ics.append(i / 5.0) ips.append(np.sin((i + 6 * dp) * np.pi / 10.0) + r() * 0.1) zet.append(np.cos((i + 6 * dp) * np.pi / 10.0) + r() * 0.1) data_table.item_table = np.array([ics, ips, zet], dtype="|S20") data_table.labels_row = ["X", "Sin", "Cos"] data_table.set_tags(f"dp=dp{str(dp)} section=data") data_table.plot = "line" data_table.xaxis = "X" data_table.yaxis = ["Sin", "Cos"] data_table.xaxis_format = "floatdot0" data_table.yaxis_format = "floatdot1" data_table.ytitle = "Values" data_table.xtitle = "X" def create_report_template(server=None) -> None: template_1 = server.create_template( name="Simulation Report", parent=None, report_type="Layout:basic" ) template_1.params = '{"HTML": "

Simulation Report

"}' server.put_objects(template_1) template_0 = server.create_template(name="TOC", parent=template_1, report_type="Layout:toc") template_0.params = '{"TOCitems": 1, "HTML": "

Table of Content

"}' template_0.set_filter("A|i_name|eq|__NonexistantName__;") server.put_objects(template_0) server.put_objects(template_1) template_2 = server.create_template( name="Introduction", parent=template_1, report_type="Layout:panel" ) template_2.params = '{"HTML": "

Introduction

", "properties": {"TOCItem": "1"}}' template_2.set_filter("A|i_tags|cont|section=intro;") server.put_objects(template_2) server.put_objects(template_1) template_3 = server.create_template(name="Text", parent=template_2, report_type="Layout:basic") template_3.params = '{"properties": {"TOCItem": "0"}}' template_3.set_filter("A|i_type|cont|html,string;") server.put_objects(template_3) server.put_objects(template_2) server.put_objects(template_1) template_4 = server.create_template(name="Tree", parent=template_2, report_type="Layout:basic") template_4.params = '{"properties": {"TOCItem": "0"}}' template_4.set_filter("A|i_type|cont|tree;") server.put_objects(template_4) server.put_objects(template_2) server.put_objects(template_1) template_5 = server.create_template( name="Results", parent=template_1, report_type="Layout:panel" ) template_5.params = ( '{"HTML": "

Results

\\nYour simulation results.", "properties": {"TOCItem": "1"}}' ) template_5.set_filter("A|i_tags|cont|section=data;") server.put_objects(template_5) server.put_objects(template_1) .. GENERATED FROM PYTHON SOURCE LINES 131-138 Create items and report template -------------------------------- Assume we have ran the design point dp0. Create the items. Check if the database already contains the report template with the method :func:`get_list_reports`. If it doesn't exist, then create it as well. .. GENERATED FROM PYTHON SOURCE LINES 138-143 .. code-block:: Python create_items(dp=0) if "Simulation Report" not in adr_service.get_list_reports(): create_report_template(server=adr_service.serverobj) .. GENERATED FROM PYTHON SOURCE LINES 144-150 Create items from second simulation ----------------------------------- Now assume we separately ran the design point dp1. Connect to the currently running ADR service and push the new items in the same database. Check if the report template already exists and create it only if it does not. .. GENERATED FROM PYTHON SOURCE LINES 150-158 .. code-block:: Python new_service = adr.Service(ansys_installation=r"C:\Program Files\Ansys Inc\v241") new_service.connect(url=adr_service.url) create_items(dp=1) if "Simulation Report" not in new_service.get_list_reports(): create_report_template(server=new_service.serverobj) .. GENERATED FROM PYTHON SOURCE LINES 159-170 .. image:: /_static/01_two_simulation_same_db_0.png Visualize the report -------------------- Now the database contains data from both design point simulations. If you were to simply visualize the report, both simulation results would appear. To avoid that, use a filter to downselect the items to visualize each time. Setting the filter to look only at items with tag dp=dp0 will show the report for the first design point. Simularly, filter for tag dp=dp1 will display the report for the second design point. .. GENERATED FROM PYTHON SOURCE LINES 170-174 .. code-block:: Python new_service.visualize_report(report_name="Simulation Report", filter="A|i_tags|cont|dp0;") .. GENERATED FROM PYTHON SOURCE LINES 175-182 .. image:: /_static/01_two_simulation_same_db_1.png Visualize the report for dp1 ---------------------------- Visualize the report for the second design point. See how you only need to change the filter. .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: Python new_service.visualize_report(report_name="Simulation Report", filter="A|i_tags|cont|dp1;") .. GENERATED FROM PYTHON SOURCE LINES 186-191 Close the service ----------------- Close the Ansys Dynamic Reporting service. The database with the items that were created remains on disk. .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: Python # sphinx_gallery_thumbnail_path = '_static/01_two_simulation_same_db_0.png' adr_service.stop() .. _sphx_glr_download_examples_50-advanced_01-two_simulation_same_db.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01-two_simulation_same_db.ipynb <01-two_simulation_same_db.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01-two_simulation_same_db.py <01-two_simulation_same_db.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01-two_simulation_same_db.zip <01-two_simulation_same_db.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_