Search code examples
openmdao

Constraints on BsplinesComp


I am using BsplinesComp for a sample problem. The objective is to maximize the area under the line. My problem arises when I want to set a constraint for one of the values in the output array that bspline gives. So a value such that the spline goes through that no matter what configuration it is in. I tried this in two ways and I have uploaded the codes. They are both very badly coded so i think there is a neater way to do so. Links to codes: https://gist.github.com/stackoverflow38/5eae1e86c5802a4df91becdf580d28c5

1- Using an extra explicit component in which the middle array value is imposed to be a selected value

2- Tried to use an execcomp but I get an error. Target shapes do not match. I vaguely remember reading such a question but could not find it.

Overall I am trying to set a constraint for either the first, middle or last value of the bspline and some range that it should be in. Similar to the plots here

enter image description here


Solution

  • So, I think you want to know the best way to do this, and the best way is to not use any extra components at all. You can directly constrain a single point in the output of the BsplinesComp by using the "indices" argument in the add_constraint call. Here, I constrain the first point in the spline to lie on the interval [-1, 1].

    model.add_constraint('interp.h', lower=-1, upper=1, indices=[0])
    

    Running the model gives me a shape that looks more like one of the ones you included.

    Just for reference, for the errors you got with 1 and 2:

    1. Not sure what is wrong here, but maybe the version you uploaded isn't the latest. You never used the AeraComp in a constraint, so it didn't do anything.

    2. The exception was due to a size mismatch in connecting the vector output of the Bsplines comp to a scaler expression. You can do this by specifying the "src_indices", giving it a list of which indices in the array to connect to the target. model.connect('interp.h', 'execcomp.x', src_indices=[0])