Sweep over CAD files¶
This example demonstrates how to iterate over a collection CAD files and generate case files for each simulation. This is relevant if you need to evaluate many designs generated from an external CAD tool. This example is a basically a 1D parameter sweep on geometry CAD file name.
The workflow represented here is
Create CAD designs in external tool
Export each of the CAD files
Create M-Star model in the Pre GUI that will be used as the base model
Use Python to generate an M-Star model for each of the CAD files
Export case for CAD file
You will need a working M-Star installation with the Python Pre API setup to run this example. See Installation
CAD Designs¶
Each of the designs is oriented with the Z axis as the rotation axis and the origin as at the center of the geometry.
Base M-Star Model¶
The base model is setup as a blend time calculation using an non-baffled cylindrical tank. For more details on how to setup blend time calculations, see Computing Blend Time on any Model.
We will swap out the child geometry under Moving Body with each of the CAD geometries above.
Python script¶
This python script loads in the base.msb file, and iterates over all the *.brep
files in the directory. Each geometry file is loaded and transformed to align the rotation axis with Y+ and translate the position to coincide with the Moving Body location. The case files are saved and exported.
import os
import glob
import mstar
import math
mstar.Initialize()
def applyParameters(geometryFn: str):
# Load in M-Star file base.msb
# Swap in geometry under Moving Body object
# Save case files
name = F"MovingBody-{geometryFn}"
if not os.path.isdir(name):
os.makedirs(name)
# Load in model and get current Moving Body location
m = mstar.Load("base.msb")
movingBody = m.Get("Moving Body")
loc = movingBody.GetLocation()
# Swap in geometry file
# Each geometry is aligned with Z axis, so rotate to align with Y axis of Moving Body
# Move geometry to current moving body location
movingBody.ClearChildren()
geometry = movingBody.AddGeometryFromFile(geometryFn)
geometry.Rotate(0,0,0, 1, 0, 0, -math.pi * 0.5)
geometry.Translate(*loc)
# Save msb file and export solver input files
m.Save(os.path.join(name, F"{name}.msb"))
m.Export(name)
m.Close()
# Iterate over all *.brep files and apply changes
for fn in filter(os.path.isfile, glob.glob("*.brep")):
applyParameters(fn)
Running the example¶
Download the above files into a directory
Activate the Python environment
Run
python param-sweep-geo.py
The case files are now available in each of the new sub-directories. You can run the cases individually using the Solver GUI or use the Queue.