-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver_BOGP_example.py
executable file
·165 lines (142 loc) · 6.34 KB
/
driver_BOGP_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###############################################################
# Bayesian Optimization based on Gaussian Processes
# Find the upper boundary shape s.t. a given pressure gradient
# for the TBL at the lower wall is maintained
###############################################################
# Yuki Morita, [email protected]
# Saleh Rezaeiravesh, [email protected]
# %% libralies
import subprocess
import os
import logging
import pathlib
import numpy as np
# %% logging
# create logger
logger = logging.getLogger("Driver")
if (logger.hasHandlers()):
logger.handlers.clear()
logger.setLevel(logging.INFO)
def add_handler():
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)s - %(funcName)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
# if not logger.handlers:
# logger.addHandler(ch)
logger.addHandler(ch)
add_handler()
# %% SETTINGS
iStart = 1 # Starting iteration
iEnd = 50 # < 100
# assert iEnd < 100
in_exc = 0.3 # ignore this region when assess the objective
out_exc = 0.1
# setting for OFcase
U_infty, delta99_in, Nx, Ny, Nz, tEnd, Lx, Ly, nProcessors = \
1, 0.05, int(500), int(218), int(1), int(200), 50, 2, 10
# %% path
current_dir = str(pathlib.Path(__file__).resolve().parent)
PATH2BUP = current_dir + "/storage/current"
PATH2DATA = current_dir + "/data"
PATH2FIGS = current_dir + "/figs"
PATH2OFCASE = current_dir + "/OFcase"
PATH2GPLIST = current_dir + "/gpOptim/workDir/gpList.dat"
# %% misc.
minInd, minR = 0, np.inf
# %% MAIN
if __name__ == '__main__':
from OFpost import main_post
from OFpre import main_pre
from gpOptim import gpOpt_TBL as X
# initialiization
#subprocess.call('clear')
logger.info("CHECK KERBEROS VALIDITY !!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
logger.info("process id = %d" % os.getpid())
logger.info("pwd = %s" % current_dir)
logger.info("iStart = %d, iEnd = %d, in_exc = %f, out_exc = %f" \
% (iStart, iEnd, in_exc, out_exc))
logger.info("U_infty = %f, delta99_in = %f, Nx = %d, Ny = %d, Nz = %d, "\
"tEnd = %d, Lx = %f, Ly = %f" % \
(U_infty, delta99_in, Nx, Ny, Nz, tEnd, Lx, Ly))
X.printSetting()
# make directories
if not os.path.isdir(PATH2BUP):
os.mkdir(PATH2BUP)
if not os.path.isdir(PATH2DATA):
os.mkdir(PATH2DATA)
if not os.path.isdir(PATH2FIGS):
os.mkdir(PATH2FIGS)
# clean remaining data
if iStart == 1:
logger.info("RESET figs/, gpList, data/, %s !!!!!!!!!!!!!!!" % PATH2BUP)
subprocess.call("rm -f %s/*.npy" % PATH2DATA, shell=True)
subprocess.call("sed -i '3,$d' %s" % PATH2GPLIST, shell=True)
subprocess.call("rm -f %s/*.pdf" % PATH2FIGS, shell=True)
subprocess.call("rm -f %s/png/*" % PATH2FIGS, shell=True)
subprocess.call("rm -rf %s/*" % PATH2BUP, shell=True)
else:
assert len(X.read_available_GPsamples(PATH2GPLIST, X.nPar)[1]) + 1 == iStart, \
"gpList and iStart doesn't match"
# MAIN LOOP
for i in range(iStart, iEnd + 1):
logger.info("############### START LOOP i = %d #################" % i)
#1. Generate a sample from the parameters space
newQ = X.nextGPsample(PATH2GPLIST)#"gpOptim/workDir/gpList.dat") # path2gpList
#2. Write new q to path2case/system/yTopParams.in for blockMesh and controlDict
main_pre.write_yTopParams\
(Nx, Ny, Lx, Ly, newQ*delta99_in, U_infty, tEnd, PATH2OFCASE)
#3. Run OpenFOAM
os.chdir(PATH2OFCASE)
logger.info("clean OFcase files")
subprocess.call("rm -rf processor*", shell=True)
subprocess.call("rm -rf postProcessing", shell=True)
subprocess.call("rm -rf constant/polyMesh", shell=True)
subprocess.call("foamListTimes -rm", shell=True) # delete time directories (except 0)
# preparation
subprocess.check_call("blockMesh", shell=True)
subprocess.call("wait", shell=True)
subprocess.check_call("decomposePar", shell=True)
logger.info("MAIN SIMULATION START")
subprocess.check_call("bash OFrun.sh %d" % (nProcessors), shell=True) # for run in workstation (not cluster)
# subprocess.call("sbatch jobScript", shell=True)
subprocess.call("wait", shell=True)
logger.info("MAIN SIMULATION END")
# post process
subprocess.check_call("reconstructPar -latestTime", shell=True)
subprocess.check_call("postProcess -func writeCellCentres -time 0", shell=True)
# backup
logger.info("COPY THE LATEST TIME DATA TO %s/%s" % (PATH2BUP, i))
if not os.path.isdir(PATH2BUP + "/" + str(i)):
os.mkdir(PATH2BUP + "/" + str(i))
subprocess.check_call("cp -r %d %s/%s/%d" % (tEnd,PATH2BUP, i, tEnd), shell=True)
subprocess.check_call("cp -r 0 %s/%s/" % (PATH2BUP, i), shell=True)
if not os.path.isdir(PATH2BUP + "/" + str(i) + "/constant"):
os.mkdir(PATH2BUP + "/" + str(i) + "/constant")
subprocess.check_call("cp -r constant %s/%s/" % (PATH2BUP, i), shell=True)
subprocess.check_call("cp -r postProcessing %s/%s/" % (PATH2BUP, i), shell=True)
os.chdir(current_dir)
#4. Post-process OpenFOAM
obj = main_post.main(in_exc, out_exc, i, U_infty, delta99_in, \
Nx, Ny, Nz, tEnd, newQ)
# update minInd
if obj < minR:
minR = obj
minInd = i
#5. Post-process optimization
isConv = X.BO_update_convergence(newQ, obj, path2gpList=PATH2GPLIST, path2figs=PATH2FIGS)
# os.chdir(current_dir)
#6. check convergence
if isConv:
break
logger.info("################### MAIN LOOP END ####################")
logger.info("The iteration gave the smallest R: %d" % minInd)
logger.info("copy figs/, data/, gpList.dat, log to %s" % PATH2BUP)
subprocess.check_call("cp -r %s %s/" % (PATH2FIGS, PATH2BUP), shell=True)
subprocess.check_call("cp -r %s %s/" % (PATH2DATA, PATH2BUP), shell=True)
subprocess.check_call("cp %s %s/" % (PATH2GPLIST, PATH2BUP), shell=True)
subprocess.check_call("cp log %s/" % (PATH2BUP), shell=True)
logger.info("FINISHED")