-
Notifications
You must be signed in to change notification settings - Fork 40
/
model_var_access.py
68 lines (51 loc) · 1.44 KB
/
model_var_access.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
"Demo of accessing variables in models"
from gpkit import Model, Variable
class Battery(Model):
"""A simple battery
Upper Unbounded
---------------
m
Lower Unbounded
---------------
E
"""
def setup(self):
h = Variable("h", 200, "Wh/kg", "specific energy")
E = self.E = Variable("E", "MJ", "stored energy")
m = self.m = Variable("m", "lb", "battery mass")
return [E <= m*h]
class Motor(Model):
"""Electric motor
Upper Unbounded
---------------
m
Lower Unbounded
---------------
Pmax
"""
def setup(self):
m = self.m = Variable("m", "lb", "motor mass")
f = Variable("f", 20, "lb/hp", "mass per unit power")
Pmax = self.Pmax = Variable("P_{max}", "hp", "max output power")
return [m >= f*Pmax]
class PowerSystem(Model):
"""A battery powering a motor
Upper Unbounded
---------------
m
Lower Unbounded
---------------
E, Pmax
"""
def setup(self):
battery, motor = Battery(), Motor()
components = [battery, motor]
m = self.m = Variable("m", "lb", "mass")
self.E = battery.E
self.Pmax = motor.Pmax
return [components,
m >= sum(comp.m for comp in components)]
PS = PowerSystem()
print("Getting the only var 'E': %s" % PS["E"])
print("The top-level var 'm': %s" % PS.m)
print("All the variables 'm': %s" % PS.variables_byname("m"))