A how-to guide for scripting marc mentat using Python API.
This README will contain an overview of the provided example. Each script is relatively well documented and commented.
There is a section at the bottom of this README with some useful tips and tricks. There are a couple of quirks that mentat has which I have figured out how to get around.
Extension | File function |
---|---|
.proc | This is the Mentat input file. We create this file and then run it to build the model. |
.dat | This is the input file to the solver (Marc). |
.t16 | Results file - can be processed in mentat or using py_post . |
.sts | Status file - file is updated whilst being solved. After completion, the exit code is found at the bottom of this file. |
.x_t | Not sure, have never used it. |
.mud | Marc Binary Model File - I do not use this file in my scripting pipeline. |
I start with the resulting deformation images to show what the final result should look like.
- The
create_model.py
first creates the rectangle cavity that will be placed in the unit. This makes use of thecreate_rectangle
function in this script. - Once the rectangle has been generated, the
create_model.py
script will call thecreate_rectangle_proc_dat
function in thecreate_rectangle_proc_dat.py
script. This function will create the.proc
files.- The
.proc
files are all basically the same so the commands have been hardcoded and only the model-specific parameters are changed. - The parameter values come from two locations.
- The
config.ini
file. - The nodal locations as a result of the rectangle cavity geometry.
- The
- The
- Once the
.proc
files have been generated they can be run in background mode using the commandmentat -bg <file_name>.proc
in the terminal. This will create the.dat
files. - The
.dat
files can then be solved with Marc using the commandrun_marc -j <file_name>.dat
. This will create the.t16
and.sts
files.- Note: The
mentat
andrun_marc
commands have to be run for each file. When I have many files on the HPC I have a bash script that does this for me. - The
.sts
files contain the information for each increment of the simulation and can be monitored to see the progress of the simulation.
- Note: The
- Once the simulation is complete the
py_post_process.py
can be run which will extract the final nodal positions of each of the prescribed nodal target locations and place it in a text file.- I have simply prescribed the outer edge of the unit as the nodal target in this example. This is done in the
set_up_node_targets
function. - If a simulation failed, or a file is missing the name of the failed or missing file is placed in the
failed_files.txt
andmissing_files.txt
files respectively. - The extracted nodal positions are stored in the text file using the convention x0, x1, x2, ...., xn, y0, y1, y2, ...., yn. Where each new line is a new simulation. (This is useful to me for the Monte-Carlo simulations I run).
- I have simply prescribed the outer edge of the unit as the nodal target in this example. This is done in the
Mentat can create a polygon by placing node points in a specified winding order. The create_rectangle
function takes in the
- Care needs to be taken to ensure that the vertices fall within the desired domain. I have some functions that do this for me, but they are not included in this example.
- When running an optimisation I also just leave this for the optimisation constraints to handle.
We cannot directly alter the .dat
files if have a changing geometry. Therefore we will create a .proc
file and let mentat set up the model for us in the background using mentat -bg <file_name>.proc
.
- The
-bg
flag can be left out and then mentat will open and all the commands will be generated in the GUI. This is useful to see what's actually happening. - When generating many
.dat
files I have found it more efficient to combine all the.proc
files into one and then just run it. I have set the.proc
files up in such a way that this is possible. The.proc
files close and open the required models as needed.
- When passing nodes to mentat to create a polygon, the order in which they are passed is important. The nodes must be passed in a clockwise/counterclockwise order (not sure which one). If you pass the nodes in the wrong order, mentat will give an error. A simple solution to this is to check if the area of the polygon is negative when using the shoelace area formula. This is especially useful when working with irregular polygons with many nodes.
- When post-processing the files with mentat the .mud file is expected to be in the same directory. However, I have found that even though mentat gives an error it is not there, it still works. Therefore I just ignore the .mud files to save a bit of space.
- When using Windows and pressing to autocomplete the file name a
.\
is added to the start of the file name. This causes mentat to give an error. Therefore be sure to remove it when running mentat. - Marc and Mentat create a lot of files. I try to keep a folder where all of these are placed to avoid bloating my working directory.
- Marc and mentat will dump their files within the directory the files are called from, i.e., where the .proc file is located when run.
- I append all the commands to create the model into a single .proc file. This is the most efficient way I have found to generate a lot of .dat files in the case of a Monte Carlo simulation scenario.
- Mentat requires the use of Python 3.8.
- The py_mentat and py_post files need to be placed in the system path, or they can be placed in the Anacondas virtual environment. If both of these fail the files can just be placed in the directory from which the script calling them is being run.
- On Windows the files are located at
C:\Program Files\MSC.Software\Marc\2021.4.0\mentat2021.4\shlib\win64
.
- If the
run_marc
andmentat
commands do not work in the terminal you need to add them to your system path. - The files (on Windows machine) are located at
C:\Program Files\MSC.Software\Marc\2021.4.0\marc2021.4\tools
andC:\Program Files\MSC.Software\Marc\2021.4.0\mentat2021.4\bin