OpenMDAO / dymos

Open Source Optimization of Dynamic Multidisciplinary Systems
Apache License 2.0
202 stars 65 forks source link

Definig both Mayer and Lagrange terms in Objective #989

Closed saeidb71 closed 12 months ago

saeidb71 commented 12 months ago

Task Description

Could you please provide guidance on defining both Lagrange and Mayer terms in the objective? I attempted to follow an example in the documentation, but it resulted in an error. I also experimented with defining an output and using it as the objective, but that approach didn't yield the expected results.

Here is the example I referred to: https://openmdao.github.io/dymos/examples/hull/hull_problem.html

However, I encountered the following error: " ValueError: Unable to find the source J = 2.5*x**2 + xL in the ODE at traj.phases.phase0.rhs_all."

robfalck commented 12 months ago

In the example you cited we convert the Lagrange term to a state variable (anything being integrated, like the Lagrange term, is treated as a state in dymos) and then we treat the objective as a Mayer term.

What version of Dymos are you using? The ability to use string definitions for the objective and constraints is new in Dymos, so that may be an issue:

phase.add_objective('J = 2.5*x**2 + xL')

Without defining the objective as a string expression you could also create an OpenMDAO component that takes inputs x and xL and outputs J, but the use of ExecComps in OpenMDAO for simple expressions can make things easier by handling derivatives automatically via complex step.

saeidb71 commented 12 months ago

Thank you. I had been using version 1.4.0, but I've now upgraded it to version 1.9.1.dev0, and that resolved the problem