-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunProjector.py
137 lines (114 loc) · 4.02 KB
/
runProjector.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
#!/usr/bin/env python3
#%---------------------------------------------------------------------------
# IMPORTS
#-----------------------------------------------------------------------------
#%%
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch_optimizer as topt
from netw.miscfuncs import makeTensor
from airfoildata import loadAirfoilData
from auxfuncs import drawAirfoil,netwDataName,shoelaceArea1
from projarea import AreaProjector
from objective import loadSurogateModel,wingLodF,predictDraglift
def dispLatentV1(net,model,z,color='-b'):
cd,cl,xy = predictDraglift(net,model,makeTensor(z))
cd = cd.item()
cl = cl.item()
drawAirfoil(xy,color)
print('cl {:2.3f}, cd {:2.3f} , lod {:2.3f}'.format(cl,cd,cl/cd))
def dispLatentV2(net,model,z1,z2):
dispLatentV1(net,model,z1,'-r')
dispLatentV1(net,model,z2,'-b')
def dispLatentV3(net,model,z1,z2,z3):
dispLatentV1(net,model,z1,'-r')
dispLatentV1(net,model,z2,'-g')
dispLatentV1(net,model,z3,'-b')
def setAxes(xmin,ymin,xmax,ymax,legendP=False):
ax=plt.gca()
ax.axis([xmin,xmax,ymin,ymax])
if(legendP):
plt.legend()
zdim=8
step=25
n1 = 16
n2 = 32
n3 = 16
targetA= 0.1
sigN = 2
sigA = 0.1
sigT = 0.01
optP = True
dataT = loadAirfoilData(zdim=zdim,batchN=400,trainP=True,step=step,targetA=targetA,sigA=sigA,sigN=sigN,sigT=sigT)
#Load data
ydim = dataT.target.size(1)
dName = netwDataName(zdim,n1,n2,n3)
dataT.restore(dName)
zs,xys0 = dataT.batch(0)
#Load projector
net = AreaProjector(n1=n1,n2=n2,n3=n3,nIn=zdim,nOut=ydim)
pName = netwDataName(zdim,n1,n2,n3,targetA)
net.restore(pName)
net.toGpu()
#Load surrogate
model = loadSurogateModel()
#%%
# n = 3
# typ = None
# z0 = zs[n].clone().detach().requires_grad_(True)
# xy0 = net(z0.view((1,-1)))[0]
# drawAirfoil(xy0)
#%%----------------------------------------------------------------------------
# Lift Over Drag
#------------------------------------------------------------------------------
def getOptimizer(z,lr=0.01,typ=None):
if(typ=='MadGrad'):
return topt.MADGRAD([z],lr=lr,momentum=0.9,weight_decay=0,eps=1e-6)
elif(typ=='LFBGS'):
return torch.optim.LBFGS([z],lr=lr,line_search_fn='strong_wolfe')
return torch.optim.Adam([z],lr=lr)
def torchOtimize(net,z,lambd=0.2,nIt=100,dispP=False,savS=None,typ=None):
#global minLoss
model = loadSurogateModel()
z = z.clone().detach().requires_grad_(True)
z0 = z.clone().detach()
xy0 = net(z0.view((1,-1)))
optim = getOptimizer(z,lr=0.01,typ=typ)
mLoss = [np.inf,z0]
def closure():
optim.zero_grad()
loss = wingLodF(net,z,model=model,K=None,z0=z0,lambF=lambd)
if(loss<mLoss[0]):
# Store best current result
z1 = z.clone().detach()
mLoss[0] = loss
mLoss[1] = z1.view(-1)
if(dispP):
dispF(z1,savS=savS)
loss.backward()
return loss
def dispF(z,savS=None):
cd,cl,xy1=predictDraglift(net,model,z)
ar = shoelaceArea1(xy1).item()
label = 'It: {:3d}, loss {:2.3f}, cl: {:2.3f}, cd: {:2.3f} , ar: {:2.3f} , lod: {:2.3f}'.format(it,mLoss[0].item(),cl.item(),cd.item(),ar,(cl/cd).item())
plt.clf()
drawAirfoil(xy0,'-r')
drawAirfoil(xy1,'-b',label=label)
print(label)
setAxes(0.0,-0.3,1.0,0.3,legendP=True)
plt.pause(0.1)
if(savS is not None):
if(it<10):
plt.savefig('fig/{}00{:d}.png'.format(savS,it))
elif(it<100):
plt.savefig('fig/{}0{:d}.png'.format(savS,it))
else:
plt.savefig('fig/{}{:d}.png'.format(savS,it))
for it in range(nIt):
optim.step(closure)
return mLoss[1]
n = 3
z0 = zs[n].clone().detach().requires_grad_(True)
z1 = torchOtimize(net,z0,lambd=0.1,nIt=30,dispP=True,savS=None)
dispLatentV2(net,model,z0,z1)