Sweep over CAD files + 1D parameter sweep

This example demonstrates how to iterate over a collection CAD files and generate a 1D parameter sweep for each geometry. This example extends the previously shown example Sweep over CAD files.

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.

../../../../_images/stirbar-simple1.PNG

stirbar-simple.brep

../../../../_images/stirbar-rounded1.PNG

stirbar-rounded.brep

../../../../_images/stirbar-gear1.PNG

stirbar-gear.brep

../../../../_images/stirbar-ellipse-pill1.PNG

stirbar-ellipse-pill.brep

../../../../_images/stirbar-cross1.PNG

stirbar-cross.brep

../../../../_images/stirbar-cross-diamond1.PNG

stirbar-cross-diamond.brep

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.

../../../../_images/base-model1.PNG

base.msb

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.

For each geometry, a 1D parameter sweep on the Rotation Speed is done. The resulting set of simulation looks like:

  • Geometry 1 (stirbar-simple.brep)

    • RPM=60

    • RPM=80

    • RPM=100

  • Geometry 2 (stirbar-rounded.brep)

    • RPM=60

    • RPM=80

    • RPM=100

  • … and so on

The case files are saved and exported.

param-sweep-geo-2.py


import os
import glob
import mstar
import math

mstar.Initialize()

def applyParameters(geometryFn: str, rpmValues: list[float]):
	# Load in M-Star file base.msb
	# Swap in geometry under Moving Body object
	# Save case files	
	
	for rpmValue in rpmValues:

		name = F"MovingBody-{geometryFn}_RPM={rpmValue}"
		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)

		# Apply RPM Value
		movingBody.Get("Rotation Speed UDF").ValueFloat = rpmValue

		# 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(geometryFn=fn, rpmValues=[60.0, 80.0, 100.0])

Running the example

  • Download the above files into a directory

  • Activate the Python environment

  • Run python param-sweep-geo-2.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.