Skip to content

Commit

Permalink
Replaces older sol.value() with preferred sol() for consistency throu…
Browse files Browse the repository at this point in the history
…ghout repository
  • Loading branch information
peterdsharpe committed Apr 5, 2024
1 parent 1229420 commit 6818b3c
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
Expand Up @@ -86,7 +86,7 @@ def logint(x):
### Solve
sol = opti.solve()
theta = sol(theta)
H = sol.value(H)
H = sol(H)

### Plot
from aerosandbox.tools.pretty_plots import plt, show_plot
Expand Down
Expand Up @@ -32,14 +32,14 @@ def test_fuselage_aerodynamics_optimization():

opti.minimize(-aero["L"] / aero["D"])
sol = opti.solve(verbose=True)
print(sol.value(alpha))
assert sol.value(alpha) > 10 and sol.value(alpha) < 20
assert sol.value(beta) == pytest.approx(0, abs=1e-3)
print(sol(alpha))
assert sol(alpha) > 10 and sol(alpha) < 20
assert sol(beta) == pytest.approx(0, abs=1e-3)

opti.minimize(aero["D"])
sol = opti.solve(verbose=False)
assert sol.value(alpha) == pytest.approx(0, abs=1e-2)
assert sol.value(beta) == pytest.approx(0, abs=1e-2)
assert sol(alpha) == pytest.approx(0, abs=1e-2)
assert sol(beta) == pytest.approx(0, abs=1e-2)


if __name__ == '__main__':
Expand Down
Expand Up @@ -52,7 +52,7 @@ def get_aero(alpha, taper_ratio):
opti.minimize(-LD)
sol = opti.solve()

assert sol.value(alpha) == pytest.approx(5.5, abs=1)
assert sol(alpha) == pytest.approx(5.5, abs=1)


if __name__ == '__main__':
Expand Down
Expand Up @@ -50,8 +50,8 @@ def test_vlm_optimization_operating_point():
verbose=True,
# callback=lambda _: print(f"alpha = {opti.debug.value(alpha)}")
)
print(sol.value(alpha), sol.value(LD))
assert sol.value(alpha) == pytest.approx(5.85, abs=0.1)
print(sol(alpha), sol(LD))
assert sol(alpha) == pytest.approx(5.85, abs=0.1)


if __name__ == '__main__':
Expand Down
Expand Up @@ -107,8 +107,8 @@ def test_block_move_minimum_time():
assert dyn.u_e[-1] == pytest.approx(0)
assert np.max(dyn.u_e) == pytest.approx(1, abs=0.01)
assert sol(u)[0] == pytest.approx(1, abs=0.05)
assert sol.value(u)[-1] == pytest.approx(-1, abs=0.05)
assert np.mean(np.abs(sol.value(u))) == pytest.approx(1, abs=0.01)
assert sol(u)[-1] == pytest.approx(-1, abs=0.05)
assert np.mean(np.abs(sol(u))) == pytest.approx(1, abs=0.01)


if __name__ == '__main__':
Expand Down
Expand Up @@ -114,7 +114,7 @@ def test_quadcopter_flip():
sol = opti.solve(verbose=False)
dyn = sol(dyn)

assert sol.value(time_final) == pytest.approx(0.824, abs=0.01)
assert sol(time_final) == pytest.approx(0.824, abs=0.01)


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions aerosandbox/optimization/opti.py
Expand Up @@ -23,7 +23,7 @@ class Opti(cas.Opti):
>>> opti.subject_to(x > 3) # Adds a constraint to be enforced
>>> opti.minimize(f) # Sets the objective function as f
>>> sol = opti.solve() # Solves the problem using CasADi and IPOPT backend
>>> print(sol.value(x)) # Prints the value of x at the optimum.
>>> print(sol(x)) # Prints the value of x at the optimum.
"""

def __init__(self,
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def __init__(self,
>>> sol = opti.solve()
>>>
>>> # Retrieve the value of the variable x in the solution:
>>> x_value = sol.value(x)
>>> x_value = sol(x)
>>>
>>> # Or, to be more concise:
>>> x_value = sol(x)
Expand All @@ -1232,8 +1232,8 @@ def _value_scalar(self, x: Union[cas.MX, np.ndarray, float, int]) -> Union[float
"""
Gets the value of a variable at the solution point. For developer use - see following paragraph.
This method is basically a less-powerful version of calling either `sol(x)` or `sol.value(x)` - if you're a
user and not a developer, you almost-certainly want to use one of those methods instead, as those are less
This method is basically a less-powerful version of calling `sol(x)` - if you're a
user and not a developer, you almost-certainly want to use that method instead, as those are less
fragile with respect to various input data types. This method exists only as an abstraction to make it easier
for other developers to subclass OptiSol, if they wish to intercept the variable substitution process.
Expand Down
8 changes: 4 additions & 4 deletions aerosandbox/propulsion/ignore/propeller_model.py
Expand Up @@ -245,21 +245,21 @@ def airfoil_CDp(alpha, Re, Ma, Cl):
# n_blades * 0.5 * air_density * (W ** 2) *
# cl * chord_local * blade_section
# )
# dDrag = sol.value(
# dDrag = sol(
# n_blades * 0.5 * air_density * (W ** 2) *
# cd * chord_local * blade_section
# )
dThrust = sol.value(
dThrust = sol(
air_density * n_blades * gamma * (
W_t - W_a * cd / cl
) * blade_section
)
dTorque = sol.value(
dTorque = sol(
air_density * n_blades * gamma * (
W_a + W_t * cd / cl
) * radial_loc * blade_section
)
# if sol.value(alpha_deg) <= 0:
# if sol(alpha_deg) <= 0:
# break

thrust.append(dThrust)
Expand Down
4 changes: 2 additions & 2 deletions aerosandbox/structures/tube_spar_bending.py
Expand Up @@ -391,11 +391,11 @@ def draw(self, show=True):

beam = sol(beam)

print(f"{sol.value(mass)} kg per half-wing")
print(f"{sol(mass)} kg per half-wing")

beam.draw()

computed_spar_mass = 2 * sol.value(mass)
computed_spar_mass = 2 * sol(mass)

vehicle_mass = lift / 9.81
ultimate_load_factor = 2
Expand Down

0 comments on commit 6818b3c

Please sign in to comment.