Skip to main content

Model


csdl.core.model

Model Objects#

class Model(, metaclass=_CompilerFrontEndMiddleEnd)

initialize#

def initialize()

User defined method to declare parameter values. Parameters are compile time constants (neither inputs nor outputs to the model) and cannot be updated at runtime. Parameters are intended to make a Model subclass definition generic, and therefore reusable. The example below shows how a Model subclass definition uses parameters and how the user can set parameters when constructing the example Model subclass.

Example

class Example(Model):    def initialize(self):        self.parameters.declare('num_times', types=int)        self.parameters.declare('step_size', types=float)        self.parameters.declare('surface', types=dict)
    def define(self):        num_times = self.parameters['num_times']        step_size = self.parameters['step_size']        surface = self.parameters['surface']        name = surface['name'] # str        symmetry = surface['symmetry'] # bool        mesh = surface['mesh'] # numpy array
        # define runtime behavior...
    surface = {        'name': 'wing',        'symmetry': False,        'mesh': mesh,    }
    # compile using Simulator imported from back end...    sim = Simulator(        Example(            num_times=100,            step_size=0.1,            surface=surface,        ),    )

define#

def define()

User defined method to define runtime behavior. Note: the user never calls this method. Only the Simulator class constructor calls this method.

Example

class Example(Model):    def define(self):        self.create_input('x')        m = 5        b = 3        y = m*x + b        self.register_output('y', y)
# compile using Simulator imported from back end...sim = Simulator(Example())sim['x'] = -3/5sim.run()print(sim['y']) # expect 0

print_var#

def print_var(var: Variable)

Print runtime value during execution. Note that print_var should only be used for debugging, as it does have a performance impact. Note that Python's print function will print the CSDL compile time Variable object information, and will have no effect on run time execution.

Example

y = csdl.sin(x)print(y) # will print compile time information about yself.print_var(y) # will print run time value of y

add_objective#

def add_objective(name, ref=None, ref0=None, index=None, units=None, adder=None, scaler=None, parallel_deriv_color=None, cache_linear_solution=False)

Declare the objective for the optimization problem. Objective must be a scalar variable.

add_design_variable#

def add_design_variable(name, lower=None, upper=None, ref=None, ref0=None, indices=None, adder=None, scaler=None, units=None, parallel_deriv_color=None, cache_linear_solution=False)

Add a design variable to the optimization problem. The design variable must be an Input. This will signal to the optimizer that it is responsible for updating the input variable.

add_constraint#

def add_constraint(name, lower=None, upper=None, equals=None, ref=None, ref0=None, adder=None, scaler=None, units=None, indices=None, linear=False, parallel_deriv_color=None, cache_linear_solution=False)

Add a constraint to the optimization problem.

declare_variable#

def declare_variable(name: str, val=1.0, shape=(1, ), src_indices=None, flat_src_indices=None, units=None, desc='', tags=None, shape_by_conn=False, copy_shape=None, distributed=None) -> DeclaredVariable

Declare an input to use in an expression.

An input can be an output of a child System. If the user declares an input that is computed by a child System, then the call to self.declare_variable must appear after the call to self.add.

Parameters

name: str Name of variable in CSDL to be used as a local input that takes a value from a parent model, child model, or previously registered output within the model. shape: Tuple[int] Shape of variable val: Number or ndarray Default value for variable

Returns

DocInput An object to use in expressions

create_input#

def create_input(name, val=1.0, shape=(1, ), units=None, desc='', tags=None, shape_by_conn=False, copy_shape=None, distributed=None) -> Input

Create an input to the main model, whose value remains constant during model evaluation.

Parameters

name: str Name of variable in CSDL shape: Tuple[int] Shape of variable val: Number or ndarray Value for variable during first model evaluation

Returns

Input An object to use in expressions

create_output#

def create_output(name, val=1.0, shape=(1, ), units=None, res_units=None, desc='', lower=None, upper=None, ref=1.0, ref0=0.0, res_ref=1.0, tags=None, shape_by_conn=False, copy_shape=None, distributed=None) -> Concatenation

Create a value that is computed explicitly, either through indexed assignment, or as a fixed point iteration.

Example

x = self.create_output('x', shape=(5,3,2))x[:, :, 0] = ax[:, :, 1] = b

Parameters

name: str Name of variable in CSDL shape: Tuple[int] Shape of variable

Returns

Concatenation An object to use in expressions

register_output#

def register_output(name: str, var: Output) -> Output

Register var as an output of the Model. When adding subsystems, each of the submodel's inputs requires a call to register_output prior to the call to add.

Parameters

name: str

Name of variable in CSDL

var: Output

Variable that defines output

Returns

Output

Variable that defines output (same object as argument)

add#

def add(submodel, name: str = '', promotes: List[str] = None)

Add a submodel to the Model.

self.add call must be preceded by a call to self.register_output for each of the submodel's inputs, and followed by self.declare_variable for each of the submodel's outputs.

Parameters

name: str Name of submodel submodel: System Subsystem to add to Model promotes: List Variables to promote

Returns

System Subsystem to add to Model

create_submodel#

@contextmanagerdef create_submodel(name: str)

Create a Model object and add as a submodel, promoting all inputs and outputs. For use in with contexts. NOTE: Only use if planning to promote all varaibales within child Model object.

Parameters

name: str Name of new child Model object

Returns

Model Child Model object whose variables are all promoted

visualize_sparsity#

def visualize_sparsity()

Visualize the sparsity pattern of jacobian for this model