Example 02 - Use the HstBatch Python module#

 1################################################################################
 2# Copyright (c) 2023 - 2024 Altair Engineering Inc.  All Rights Reserved
 3# Contains trade secrets of Altair Engineering, Inc.  Copyright notice
 4# does not imply publication.  Decompilation or disassembly of this
 5# software is strictly prohibited.
 6################################################################################
 7
 8"""
 9Example script to demo use of the API to generate study setup using HyperStudy 
10Batch as a Python module
11
12This script generates a basic study setup. 
13To run script use following command 
14
15Windows -
16   <installation_root>/hwdesktop/hst/bin/win64/hstpy.bat hstapp.py
17"""
18
19import alt.hst.api.hstapp as hstapp
20import os
21import alt.hst.api.session as sess
22from alt.hst.api import api_objects
23
24
25def makeStudy(name: str) -> api_objects.Study:
26    studyList = sess.getStudyList()
27    study = studyList.add()
28    studyDir = os.path.join(os.getcwd(), 'hstbatch_py_tests', 
29                            f'{name}_{os.getpid()}')
30    print('Create study at ( ' + studyDir + ' )')
31    study.setDirectory(studyDir)
32    study.setStudyFile(os.path.join(studyDir, 'api_test.hstudy'))
33    print(f'Created study at ( {study.getStudyFile()} )')
34    study = sess.getStudy()
35
36    approach = study.getApproachList().add()
37    approach.setTool('Doe')
38    definition = approach.getDefinition()
39    dvList = definition.getDesignVariableList()
40    dv = dvList.add()
41    dv.setLabel("First DV")
42    dv.setVarname("var_1_test")
43    
44    study.save()
45
46    print(f"Created approach: {approach.getVarname()}")
47    studyList.closeStudy(study.getVarname())
48    return study
49
50
51# HstBatch can be used as a context manager for automatic resource cleanup
52with hstapp.HstBatchApp() as app:
53    makeStudy("ContextManaged_TestStudy")
54
55
56# It can also be used normally, with explicit calls to start() and close()
57app = hstapp.HstBatchApp()
58app.start()
59makeStudy("NormallyManaged_TestStudy")
60app.close()