{ "cells": [ { "cell_type": "markdown", "id": "90f7d2d0", "metadata": {}, "source": [ "# Material phase\n", "\n", "This example shows how to explore the material phase results of a sustainability summary query.\n", "\n", "The following supporting files are required for this example:\n", "\n", "* [sustainability-bom-2412.xml](../supporting-files/sustainability-bom-2412.xml)\n", "\n", "For help on constructing an XML BoM, see [BoM examples](../6_BoMs/index.rst)." ] }, { "cell_type": "markdown", "id": "0f4437eb", "metadata": {}, "source": [ "
\n", "\n", "**Info:**\n", "\n", "This example uses an input file that is in the 24/12 XML BoM format. This structure requires Granta MI Restricted\n", "Substances and Sustainability Reports 2025 R2 or later.\n", "\n", "To run this example with an older version of the reports bundle, use\n", "[sustainability-bom-2301.xml](../supporting-files/sustainability-bom-2301.xml) instead. Some sections of this example\n", "will produce different results from the published example when this BoM is used.\n", "
" ] }, { "cell_type": "markdown", "id": "626cf068", "metadata": {}, "source": [ "## Run a sustainability summary query" ] }, { "cell_type": "code", "execution_count": null, "id": "c28f64d8", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import Connection, queries\n", "\n", "MASS_UNIT = \"kg\"\n", "ENERGY_UNIT = \"MJ\"\n", "DISTANCE_UNIT = \"km\"\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "cxn = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()\n", "\n", "xml_file_path = \"../supporting-files/sustainability-bom-2412.xml\"\n", "with open(xml_file_path) as f:\n", " bom = f.read()\n", "\n", "sustainability_summary_query = (\n", " queries.BomSustainabilitySummaryQuery()\n", " .with_bom(bom)\n", " .with_units(mass=MASS_UNIT, energy=ENERGY_UNIT, distance=DISTANCE_UNIT)\n", ")\n", "sustainability_summary = cxn.run(sustainability_summary_query)" ] }, { "cell_type": "markdown", "id": "53827d23", "metadata": {}, "source": [ "## Materials phase\n", "\n", "The environmental contribution from the material phase is summarized in the ``material_details`` property. The results\n", "are aggregated: each item in ``material_details`` represents the total environmental impact of a material summed\n", "from all its occurrences in the BoM. Listed materials contribute more than 2% of the total impact for the material\n", "phase. Materials that do not contribute at least 2% of the total are aggregated under the ``Other`` item." ] }, { "cell_type": "code", "execution_count": null, "id": "d994c993", "metadata": {}, "outputs": [], "source": [ "sustainability_summary.material_details" ] }, { "cell_type": "code", "execution_count": null, "id": "e2edcb8f", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "EE_HEADER = f\"EE [{ENERGY_UNIT}]\"\n", "CC_HEADER = f\"CC [{MASS_UNIT}]\"\n", "\n", "materials_df = pd.DataFrame.from_records(\n", " [\n", " {\n", " \"Name\": item.identity,\n", " \"EE%\": item.embodied_energy_percentage,\n", " EE_HEADER: item.embodied_energy.value,\n", " \"CC%\": item.climate_change_percentage,\n", " CC_HEADER: item.climate_change.value,\n", " f\"Mass before processing [{MASS_UNIT}]\": item.mass_before_processing.value,\n", " f\"Mass after processing [{MASS_UNIT}]\": item.mass_after_processing.value,\n", " }\n", " for item in sustainability_summary.material_details\n", " ]\n", ")\n", "materials_df" ] }, { "cell_type": "markdown", "id": "59518d58", "metadata": {}, "source": [ "Plot a pair of pie charts which show the \"Embodied Energy\" and \"Climate Change CO2 equivalent\" impacts\n", "respectively." ] }, { "cell_type": "code", "execution_count": null, "id": "3b386427", "metadata": {}, "outputs": [], "source": [ "import plotly.graph_objects as go\n", "from plotly.subplots import make_subplots\n", "\n", "\n", "fig = make_subplots(\n", " rows=1,\n", " cols=2,\n", " specs=[[{\"type\": \"domain\"}, {\"type\": \"domain\"}]],\n", " subplot_titles=[\"Embodied Energy\", \"Climate Change\"],\n", ")\n", "fig.add_trace(go.Pie(labels=materials_df[\"Name\"], values=materials_df[EE_HEADER], name=ENERGY_UNIT), 1, 1)\n", "fig.add_trace(go.Pie(labels=materials_df[\"Name\"], values=materials_df[CC_HEADER], name=MASS_UNIT), 1, 2)\n", "fig.update_layout(title_text=\"Aggregated materials impact\", legend=dict(orientation=\"h\"))\n", "fig.update_traces(textposition=\"inside\", textinfo=\"percent+label\", hoverinfo=\"value+name\")\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "4f1555ec", "metadata": {}, "source": [ "Mass before and mass after secondary processing can help determine if the material mass removed during processing\n", "contributes a significant fraction of the impact of the overall material phase.\n", "\n", "Plot the aggregated material masses as a bar chart." ] }, { "cell_type": "code", "execution_count": null, "id": "a84a3976", "metadata": {}, "outputs": [], "source": [ "fig = go.Figure(\n", " data=[\n", " go.Bar(\n", " name=\"Mass before secondary processing\",\n", " x=materials_df[\"Name\"],\n", " y=materials_df[f\"Mass before processing [{MASS_UNIT}]\"],\n", " ),\n", " go.Bar(\n", " name=\"Mass after secondary processing\",\n", " x=materials_df[\"Name\"],\n", " y=materials_df[f\"Mass after processing [{MASS_UNIT}]\"],\n", " ),\n", " ],\n", " layout=go.Layout(\n", " xaxis=go.layout.XAxis(title=\"Materials\"),\n", " yaxis=go.layout.YAxis(title=f\"Mass [{MASS_UNIT}]\"),\n", " legend=dict(orientation=\"h\")\n", " ),\n", ")\n", "fig.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }