Skip to content

Commit

Permalink
Merge pull request #324 from fusion-energy/improving-cad-examples
Browse files Browse the repository at this point in the history
Improving cad examples
  • Loading branch information
shimwell authored Feb 25, 2025
2 parents bdbcb53 + 81ae471 commit 3a16ddc
Show file tree
Hide file tree
Showing 8 changed files with 14,769 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,123 @@
"\n",
"This makes DAGMC geometry which particle transport codes like OpenMC can understand and simulate particle transport through\n",
"\n",
"In this example we are going to convert some CAD to a neutronics model and simulate particles"
"In this example we are going to convert some CAD to a neutronics model and simulate particles\n",
"\n",
"Read more about DAGMC geometry here\n",
"https://svalinn.github.io/DAGMC/\n",
"\n",
"First import the package used to convert CAD to DAGMC geometry"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from cad_to_dagmc import CadToDagmc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read more about DAGMC geometry here\n",
"https://svalinn.github.io/DAGMC/"
"Make a instance of CadToDagmc "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model = CadToDagmc()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add the step file to the model. This step file contains 6"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model.add_stp_file(\n",
" filename=\"step_cad_file_for_conversion.step\",\n",
" material_tags=[\n",
" \"mat1\",\n",
" \"mat2\",\n",
" \"mat3\",\n",
" \"mat4\",\n",
" \"mat5\",\n",
" \"mat6\",\n",
" ], # 6 volumes one for each letter in the CAD STEP file\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now export the DAGMC h5m geometry file that can be used in OpenMC and other codes that support DAGMC\n",
"\n",
"The mesh will be made with default meshing parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model.export_dagmc_h5m_file(filename=\"dagmc.h5m\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The mesh production was quick but you might want to customise it. For example add extra mesh nods to a particular solid.\n",
"\n",
"The next mesh is customised with the set_size argument to have small mesh (size is 0.5) elements for the first solid and large mesh (size is 2.0) for the other solids.\n",
"\n",
"You can change the numbers used for each solid to specify different mesh sizes for each solid in the geometry."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model.export_dagmc_h5m_file(\n",
" filename=\"dagmc.h5m\",\n",
" set_size={1:0.5, 2:2.0, 3:2.0, 4:2.0, 5:2.0, 6:2.0},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The resulting DAGMC.h5m can be used in neutronics simulations.\n",
"\n",
"For more details of how to convert CAD geometry see then next task which goes over \n",
"- Additional mesh parameters\n",
"- 3D volume mesh production\n",
"- Making a mesh from CAD without writing a file\n",
"\n",
"Also take a look at \n",
"\n",
"LEarning objectives\n",
"\n",
" - We learned how to make a simple DAGMC geometry from a STEP CAD file\n",
" - We learned how to customise the mesh to make it finer on a specific volume ID number"
]
},
{
Expand All @@ -28,8 +136,22 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "openmc_dev_2",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CAD conversion in memory\n",
"\n",
"It is possible to convert from a CadQuery object to a DAGMC surface or volume mesh.\n",
"\n",
"This is quicker and more concise than writing to file and reading it in again.\n",
"\n",
"First import the packages needed for the cad creation ([CadQuery](https://cadquery.readthedocs.io)) and conversion ([cad_to_dagmc](https://github.com/fusion-energy/cad_to_dagmc))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from cad_to_dagmc import CadToDagmc\n",
"import cadquery as cq"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make some CAD geometry, we have lots of options here but to keep the example concise I've made some text.\n",
"\n",
"The important thing to note here is that we have 10 separate solids in the CAD model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"text = cq.Workplane().text(txt=\"cad to dagmc\", fontsize=10, distance=1)\n",
"text"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we make an instance of CadToDagmc with this geometry to prepare for conversion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model = CadToDagmc()\n",
"my_model.add_cadquery_object(\n",
" cadquery_object=text,\n",
" material_tags=[\"mat1\"]* 10 # ten volumes each with with material tag \"mat1\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ```export_dagmc_h5m_file()``` function will make a surface mesh geometry of the CAD ready for use in neutronics simulations.\n",
"\n",
"With openmc this DAGMC h5m file can be loading in using the ```openmc.DAGMCUniverse``` class.\n",
"\n",
"This makes a 2D mesh / surface mesh / faceted geometry / triangles of the geometry faces.\n",
"\n",
"With this type of geometry you can do simulations and score material tallies, cell tallies directly on the mesh volumes.\n",
"\n",
"You can also overlay OpenMC meshes such as regular mesh or cylindrical mesh on top of the geometry.\n",
"\n",
"The ```export_dagmc_h5m_file()``` function supports different arguments to control the mesh parameters.\n",
"\n",
"For this example we will use simple settings for ```max_mesh_size``` and ```min_mesh_size```.\n",
"\n",
"To see more options take a look at the [cad_to_dagmc package](https://github.com/fusion-energy/cad_to_dagmc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model.export_dagmc_h5m_file(\n",
" filename=\"dagmc.h5m\",\n",
" max_mesh_size=10,\n",
" min_mesh_size=2,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DAGMC and OpenMC support another form of mesh often called an unstructured mesh.\n",
"\n",
"An unstructured mesh can be overlaid over the geometry much like a regular mesh or cylindrical mesh and used to score tallies spatially.\n",
"\n",
"The ```openmc.UnstructuredMesh``` class supports MOAB and LibMesh type unstructured meshes.\n",
"\n",
"In this example we are making a MOAB / DAGMC unstructured mesh which is entirely made of tetrahedrals.\n",
"\n",
"This is a volume mesh where the outer surface is also the same as the outer surface of the 2D mesh as we have used the same mesh parameters.\n",
"\n",
"This means we have a conformal volume mesh to score on which aligns to the material / cell boundaries in the DAGMC h5m 2D mesh.\n",
"\n",
"This conformal mesh is one of the advantages of UnstructuredMesh over regular meshes and cylindrical meshes\n",
"\n",
"The ```export_unstructured_mesh_file()``` function will produce a vtk file than can be loaded into OpenMC using ```openmc.UnstructuredMesh```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_model.export_unstructured_mesh_file(\n",
" filename=\"dagmc.vtk\",\n",
" max_mesh_size=10,\n",
" min_mesh_size=2,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now you have two mesh files that can be used for neutronics simulations in OpenMC or other DAGMC compatible codes.\n",
"\n",
"Learning objectives for this task\n",
"- Know how to produce DAGMC 2d surface mesh geometry\n",
"- Know how to produce DAGMC 3d volume mesh\n",
"- Understand the differences between the types of meshes produced by cad-to-dagmc\n",
"- know some of the arguments for customising the mesh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "openmc_dev_2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 6 additions & 0 deletions tasks/task_16_converting_CAD_geometry_to_DAGMC/make_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cad_to_dagmc import CadToDagmc
import cadquery as cq

text = cq.Workplane().text(txt="OpenMC", fontsize=10, distance=1)

text.export("step_cad_file_for_conversion.step")
Loading

0 comments on commit 3a16ddc

Please sign in to comment.