Closed codykarcher closed 8 years ago
This works fine:
from gpkit.constraints.set import ConstraintSet
from gpkit import VectorVariable, Variable, units, SignomialProgram, Model
import numpy as np
# ========================================================================
class XFOIL():
def __init__(self):
self.pathname = "/Users/codykarcher/Xfoil/bin/./xfoil"
def cd_model(self, x0):
Ak = np.asarray(x0["A_K"])
CD_val = np.sum(np.sum(Ak))
return CD >= CD_val
# ========================================================================
class CDModel(ConstraintSet):
def as_posyslt1(self):
raise ValueError("AeroModel is not allowed to solve as a GP.")
def as_gpconstr(self, x0):
if x0 and x0["Re"] >= 1e7:
xfoil=XFOIL()
self[0] = xfoil.cd_model(x0)
else:
self[0] = CD >= 1. + CL**2
return super(CDModel, self).as_gpconstr(x0)
# ========================================================================
n_Kulfan = 3
n_afl_pts = 100
max_iter = 100
A = VectorVariable([4,n_Kulfan], "A_K", "-", "Kulfan Coefficients")
c = Variable("c", 1, "-", "Chord")
v = Variable("v", 100, "-", "Velocity")
rho = Variable("\\rho", 1.23, "-", "Density")
mu = Variable("\\mu", 1e-5, "-", "Viscosity")
Re = Variable("Re", "-", "Reynolds Number")
CL = Variable("C_L", "-", "Lift Coefficient")
CD = Variable("C_D", "-", "Drag Coefficient")
constraints = [CL == 1.0,
Re == c*v*rho/mu,
A >= [[.2, .2, .2],
[.1, .1, .1],
[.1, .1, .1],
[.2, .2, .1]],
A <=1
]
constraints = CDModel(constraints)
objective = CD
m = Model(objective, constraints)
sol=m.localsolve('cvxopt', verbosity=1)
m.controlpanel()
I'm guessing it doesn't like something about this:
# sort by alpha
try:
data = np.genfromtxt(polarfile, skip_header=12)
data2 = data[data[:, 0].argsort()]
alpha = data2[:, 0]
cl = data2[:, 1]
cd = data2[:, 2]
cdp = data2[:, 3]
cm = data2[:, 4]
xtr_top = data2[:, 5]
xtr_bot = data2[:, 6]
CD_val = np.interp(x0['C_L'], cl, cd)
except:
CD_val = 1.0
return [CD >= CD_val]
Yea, this has the same error as the original post:
from gpkit.constraints.set import ConstraintSet
from gpkit import VectorVariable, Variable, units, SignomialProgram, Model
import numpy as np
import subprocess
import math as math
# ========================================================================
# ========================================================================
class XFOIL():
def __init__(self):
self.pathname = "/Users/codykarcher/Xfoil/bin/./xfoil"
def cd_model(self, x0):
polarfile = 'polars.txt'
# sort by alpha
try:
data = np.genfromtxt(polarfile, skip_header=12)
data2 = data[data[:, 0].argsort()]
alpha = data2[:, 0]
cl = data2[:, 1]
cd = data2[:, 2]
cdp = data2[:, 3]
cm = data2[:, 4]
xtr_top = data2[:, 5]
xtr_bot = data2[:, 6]
CD_val = np.interp(x0['C_L'], cl, cd)
except:
CD_val = 1.0
return [CD >= CD_val]
# ========================================================================
class CDModel(ConstraintSet):
def as_posyslt1(self):
raise ValueError("AeroModel is not allowed to solve as a GP.")
def as_gpconstr(self, x0):
if x0 and x0["Re"] >= 1e7:
xfoil=XFOIL()
self[0] = xfoil.cd_model(x0)
else:
self[0] = CD >= 1. + CL**2
return super(CDModel, self).as_gpconstr(x0)
# ========================================================================
n_Kulfan = 3
n_afl_pts = 100
max_iter = 100
A = VectorVariable([4,n_Kulfan], "A_K", "-", "Kulfan Coefficients")
c = Variable("c", 1, "-", "Chord")
v = Variable("v", 100, "-", "Velocity")
rho = Variable("\\rho", 1.23, "-", "Density")
mu = Variable("\\mu", 1e-5, "-", "Viscosity")
Re = Variable("Re", "-", "Reynolds Number")
CL = Variable("C_L", "-", "Lift Coefficient")
CD = Variable("C_D", "-", "Drag Coefficient")
constraints = [CL == 1.0,
Re == c*v*rho/mu,
A >= [[.2, .2, .2],
[.1, .1, .1],
[.1, .1, .1],
[.2, .2, .1]],
A <=1
]
constraints = CDModel(constraints)
objective = CD
m = Model(objective, constraints)
sol=m.localsolve('cvxopt', verbosity=0)
m.controlpanel()
It's overwriting the first constraint every time it runs through as_gpcontsr
And you should return the constraint return CD >= CD_val
, not the constraints in a list (if you're only overwriting self[0]
). That's probably the cause of the error?
edited but not tested:
from gpkit.constraints.set import ConstraintSet
from gpkit import VectorVariable, Variable, units, SignomialProgram, Model
import numpy as np
import subprocess
import math as math
# ========================================================================
# ========================================================================
class XFOIL():
def __init__(self):
self.pathname = "/Users/codykarcher/Xfoil/bin/./xfoil"
def cd_model(self, x0):
polarfile = 'polars.txt'
# sort by alpha
try:
data = np.genfromtxt(polarfile, skip_header=12)
data2 = data[data[:, 0].argsort()]
alpha = data2[:, 0]
cl = data2[:, 1]
cd = data2[:, 2]
cdp = data2[:, 3]
cm = data2[:, 4]
xtr_top = data2[:, 5]
xtr_bot = data2[:, 6]
CD_val = np.interp(x0['C_L'], cl, cd)
except:
CD_val = 1.0
return CD >= CD_val
# ========================================================================
class CDModel(ConstraintSet):
def as_posyslt1(self):
raise ValueError("AeroModel is not allowed to solve as a GP.")
def as_gpconstr(self, x0):
if x0 and x0["Re"] >= 1e7:
xfoil=XFOIL()
self[0] = xfoil.cd_model(x0)
else:
self[0] = CD >= 1. + CL**2
return super(CDModel, self).as_gpconstr(x0)
# ========================================================================
n_Kulfan = 3
n_afl_pts = 100
max_iter = 100
A = VectorVariable([4,n_Kulfan], "A_K", "-", "Kulfan Coefficients")
c = Variable("c", 1, "-", "Chord")
v = Variable("v", 100, "-", "Velocity")
rho = Variable("\\rho", 1.23, "-", "Density")
mu = Variable("\\mu", 1e-5, "-", "Viscosity")
Re = Variable("Re", "-", "Reynolds Number")
CL = Variable("C_L", "-", "Lift Coefficient")
CD = Variable("C_D", "-", "Drag Coefficient")
constraints = [CD >= 1. + CL**2,
CL == 1.0,
Re == c*v*rho/mu,
A >= [[.2, .2, .2],
[.1, .1, .1],
[.1, .1, .1],
[.2, .2, .1]],
A <=1
]
constraints = CDModel(constraints)
objective = CD
m = Model(objective, constraints)
sol=m.localsolve('cvxopt', verbosity=0)
m.controlpanel()
Worked thanks. Sorry again for the code dump...
Wait, what if I want to pass out two constraints from the model, so like:
class XFOIL():
def __init__(self):
self.pathname = "/Users/codykarcher/Xfoil/bin/./xfoil"
def cd_model(self, x0):
polarfile = 'polars.txt'
# sort by alpha
try:
data = np.genfromtxt(polarfile, skip_header=12)
data2 = data[data[:, 0].argsort()]
alpha = data2[:, 0]
cl = data2[:, 1]
cd = data2[:, 2]
cdp = data2[:, 3]
cm = data2[:, 4]
xtr_top = data2[:, 5]
xtr_bot = data2[:, 6]
return CD >= CD_val, CL>=CL_val
Or do I need a list now?
You should return a ConstraintSet: ConstraintSet([CD >= CD_val, CL>=CL_val])
Ah, cool
Sorry, I tried to get this down to a MWE, but can't reproduce the error: