Question:
How to set up a DOE in Python for nTop Automate
Applies to:
- nTop Automate
- DOE
- Python
Answer:
1. Prepare your nTop notebook by setting up the Inputs and Outputs for running in nTop Automate (Preparing an nTop Notebook for nTop Command Line). The inputs for this example are Unit Cell Size and Thickness, which we wish to change for multiple values and get the relative density values as output.
Tip: These learning center courses have step-by-step instructions and examples for automating your nTop files (https://learn.ntopology.com/intro-to-automation/ and https://learn.ntopology.com/ntopcl-course/)
2. Run the -t template command in ntopcl with the nTop file to generate the input_template.json and output_template.json
3. Open your preferred code editor and create a new .py file to run the script to generate multiple input JSON files for running multiple values.
This article (Running nTop Command Line in Python scripts) has a Python template file you can use as a base and modify for your use case.
Tips: There are two important things to remember while using a list of values to run multiple iterations. You can use a list directly for one of the inputs; see below for how we have used thickness values. To use another list input, we will create multiple input.json files to run DOE; see below how we have used cellsize_values.
import os
import subprocess
import json
# Assuming this script, ntop file, and json files will be in the same folde
Current_Directory = os.path.dirname(os.path.realpath('__file__'))
exePath = r"C:/Program Files/nTopology/nTopology/nTopCL.exe" # nTopCL path
nTopFilePath = r"RelDensity.ntop" # nTop notebook file name
Output_Directory = r"C:\Users\ajayprasad\Downloads\RelDensity" # Output directory path
Input_File_Name = "input{}.json" # JSON input file name template
Output_File_Name = "out{}.json" # JSON output file name template
# Input variables in JSON structure
thickness_values = [round(x * 0.1, 1) for x in range(1, 21)] # List of thickness values from 0.1 to 2 with increments of 0.1
cellsize_values = [round(x * 0.5, 1) for x in range(1, 21)] # List of L values from 0.5 to 10 with increments of 0.5
# Iterate over each cell size value
for i, cellsize in enumerate(cellsize_values):
Inputs_JSON = {
"description": "",
"inputs": [
{
"description": "",
"name": "Output directory",
"type": "text",
"value": Output_Directory
},
{
"description": "",
"name": "Unit cell",
"type": "enum",
"value": 0
},
{
"description": "",
"name": "L",
"type": "real",
"units": "mm",
"value": 10.0
},
{
"description": "",
"name": "Unit Cell Size",
"type": "real",
"units": "mm",
"value": cellsize
},
{
"description": "",
"name": "Thickness",
"type": "real",
"units": "mm",
"values": thickness_values
}
],
"title": "Relative Density of Walled TPMS"
}
# Create input.json file
input_file_name = Input_File_Name.format(i + 1)
with open(input_file_name, 'w') as outfile:
json.dump(Inputs_JSON, outfile, indent=4)
# nTopCL arguments in a list
Arguments = [exePath] # nTopCL path
Arguments.append("-j") # json input argument
Arguments.append(input_file_name) # json path
Arguments.append("-o") # output argument
output_file_name = Output_File_Name.format(i + 1)
Arguments.append(output_file_name) # output json path
Arguments.append(nTopFilePath) # .ntop notebook file path
Arguments.append("-v2")
# nTopCL call with arguments
print("Running nTopCL with input file: {}".format(input_file_name))
print(" ".join(Arguments))
output, error = subprocess.Popen(Arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
# Print the return messages
print(output.decode("utf-8"))
4. When you run this script, the folder will have multiple input and output JSON files generated.
5. You can then write a script to read the output JSON files to plot as needed.