Is there a way to obtain an expression using symbolic variables in OpenMDAO?

62 Views Asked by At

I want to use a block of code written in OpenMDAO to extract an expression of an output as function of a couple of variables (needed to be declared symbolic). In this case the structure of the problem is not highly nested, so maybe I will loose less time doing it by hand directly, but I would want to know if there exist any kind of similar capability for doing this.

In this case, the structure is the following: Just the computation of the drag coefficient CD as function of Mach (M) and lift coefficient (CL):

Polar Drag

Each contribution (CD0 and e) are groups composed of several components (ExplicitComponents in this case) with further dependencies on M and CL.

So being able to get an expression that would look something like this, in terms of M and CL:

enter image description here

Being di coefficients evaluated from the rest of inputs in relations defined in the rest of components.

2

There are 2 best solutions below

2
Justin Gray On

You are looking for the ExecComp, which lets you just type out an equation in the initialization arguments.

The docs are here.

Simple example copied from those docs:

import openmdao.api as om

prob = om.Problem()
model = prob.model

model.add_subsystem('comp', om.ExecComp('y=x+1.'))

model.set_input_defaults('comp.x', 2.0)

prob.setup()

prob.run_model()

print(prob.get_val('comp.y'))
1
Rob Falck On

If you want to compose somewhat complicated expressions and automatically get the derivatives through them, then you may want to consider Jax or even interfacing with Julia. If you're looking at interfacing with code with its own symbolic engine, like sympy, there are ways of converting the resulting derivative expression to executable Python code but it's not a seamless process in my experience.

Interfacing with Jax requires some boilerplate functionality but some examples are documented here: https://openmdao.org/newdocs/versions/latest/features/building_blocks/jax_subpackage/jax_subpackage.html