Philote-Cpp
C++ bindings for the Philote MDO standard
Loading...
Searching...
No Matches
philote::Discipline Class Reference

Definition of the discipline base class. More...

#include <discipline.h>

Inheritance diagram for philote::Discipline:
philote::ExplicitDiscipline philote::ImplicitDiscipline Paraboloid Rosenbrock Quadratic

Public Member Functions

 Discipline ()
 Construct a new Discipline object.
 
 ~Discipline () noexcept
 Destroy the Discipline object.
 
std::map< std::string, std::string > & options_list ()
 Gets the options list.
 
std::vector< philote::VariableMetaData > & var_meta ()
 Accesses the variable meta data.
 
const std::vector< philote::VariableMetaData > & var_meta () const noexcept
 
std::vector< philote::PartialsMetaData > & partials_meta ()
 Accesses the partials meta data.
 
const std::vector< philote::PartialsMetaData > & partials_meta () const noexcept
 
philote::DisciplineProperties & properties ()
 Gets the discipline properties.
 
philote::StreamOptions & stream_opts ()
 Gets the stream options.
 
const philote::StreamOptions & stream_opts () const noexcept
 
void AddInput (const std::string &name, const std::vector< int64_t > &shape, const std::string &units)
 Declares an input.
 
void AddOutput (const std::string &name, const std::vector< int64_t > &shape, const std::string &units)
 Declares an output.
 
void DeclarePartials (const std::string &f, const std::string &x)
 Declare a (set of) partial(s) for the discipline.
 
void AddOption (const std::string &name, const std::string &type)
 Add an option to the discipline.
 
virtual void Initialize ()
 Initialize function that sets up available options.
 
virtual void Configure ()
 Configure function that is called after options are set.
 
virtual void SetOptions (const google::protobuf::Struct &options_struct)
 Sets up all discipline options based on a protobuf struct that the server received from the client.
 
virtual void Setup ()
 Setup function that is called by the server when the client calls the setup RPC.
 
virtual void SetupPartials ()
 Setup function that is called by the server when the client calls the setup RPC.
 
void SetContext (grpc::ServerContext *context) const noexcept
 Set the gRPC server context for cancellation detection.
 
void ClearContext () const noexcept
 Clear the gRPC server context.
 
bool IsCancelled () const noexcept
 Check if the current operation has been cancelled.
 

Protected Member Functions

std::vector< int64_t > ComputePartialShape (const std::string &f, const std::string &x, bool allow_output_as_x)
 Computes the shape for a partial derivative df/dx.
 

Protected Attributes

std::map< std::string, std::string > options_list_
 List of options that can be set by the client.
 
std::vector< philote::VariableMetaData > var_meta_
 List of variable meta data.
 
std::vector< philote::PartialsMetaData > partials_meta_
 List of partials meta data.
 
philote::DisciplineProperties properties_
 Discipline properties.
 
philote::StreamOptions stream_opts_
 Stream options.
 
grpc::ServerContext * current_context_ = nullptr
 Current gRPC server context for cancellation detection (mutable for const correctness)
 

Detailed Description

Definition of the discipline base class.

This class is used as the base class for the Explicit and Implicit Disciplines. It allows the discipline developers to overload the setup and setup partials functions without code duplication within the explicit and implicit classes.

Note
Thread Safety: This class is NOT thread-safe. Concurrent calls to methods that modify internal state (Setup, SetupPartials, SetOptions, etc.) will cause data races. Discipline instances should not be shared across threads without external synchronization. User-defined Compute methods must also be thread-safe if concurrent RPC calls are expected.

Constructor & Destructor Documentation

◆ Discipline()

philote::Discipline::Discipline ( )

Construct a new Discipline object.

◆ ~Discipline()

philote::Discipline::~Discipline ( )
noexcept

Destroy the Discipline object.

Member Function Documentation

◆ AddInput()

void philote::Discipline::AddInput ( const std::string &  name,
const std::vector< int64_t > &  shape,
const std::string &  units 
)

Declares an input.

Parameters
var

◆ AddOption()

void philote::Discipline::AddOption ( const std::string &  name,
const std::string &  type 
)

Add an option to the discipline.

Parameters
nameOption name
typeOption type as string (e.g., "bool", "int", "float", "string")

◆ AddOutput()

void philote::Discipline::AddOutput ( const std::string &  name,
const std::vector< int64_t > &  shape,
const std::string &  units 
)

Declares an output.

Parameters
var

◆ ClearContext()

void philote::Discipline::ClearContext ( ) const
noexcept

Clear the gRPC server context.

This method is called by the server after user-defined compute methods complete to ensure the context pointer doesn't outlive the RPC call.

◆ ComputePartialShape()

std::vector< int64_t > philote::Discipline::ComputePartialShape ( const std::string &  f,
const std::string &  x,
bool  allow_output_as_x 
)
protected

Computes the shape for a partial derivative df/dx.

This helper method encapsulates the logic for determining the shape of partial derivative arrays based on the shapes of the output (f) and input (x) variables.

Parameters
fName of the output variable
xName of the input (or output for implicit disciplines) variable
allow_output_as_xIf true, allows x to be an output variable (for implicit disciplines)
Returns
std::vector<int64_t> The computed shape for the partial derivative
Exceptions
std::runtime_errorIf variables are not found

◆ Configure()

virtual void philote::Discipline::Configure ( )
virtual

Configure function that is called after options are set.

This function is called after SetOptions but before Setup. It can be used for any configuration that depends on the option values.

◆ DeclarePartials()

void philote::Discipline::DeclarePartials ( const std::string &  f,
const std::string &  x 
)

Declare a (set of) partial(s) for the discipline.

Parameters
f
x

◆ Initialize()

virtual void philote::Discipline::Initialize ( )
virtual

Initialize function that sets up available options.

This function is called during discipline construction. It should be used to define what option names and types are available. The SetOptions function is used to actually set the option values.

◆ IsCancelled()

bool philote::Discipline::IsCancelled ( ) const
noexcept

Check if the current operation has been cancelled.

User disciplines can call this method during long-running computations to detect if the client has cancelled the request. If true is returned, the discipline should stop computation and return/throw as appropriate.

Returns
true if the operation has been cancelled by the client
false if no cancellation has been requested or no context is set
Example: Checking Cancellation in Long Computations
void MyDiscipline::Compute(const Variables &inputs, Variables &outputs) {
for (int iter = 0; iter < 1000000; iter++) {
// Check cancellation every 1000 iterations
if (iter % 1000 == 0 && IsCancelled()) {
throw std::runtime_error("Computation cancelled by client");
}
// ... expensive computation ...
}
}
bool IsCancelled() const noexcept
Check if the current operation has been cancelled.
std::map< std::string, philote::Variable > Variables
Definition variable.h:404

◆ options_list()

std::map< std::string, std::string > & philote::Discipline::options_list ( )

Gets the options list.

Returns

◆ partials_meta() [1/2]

std::vector< philote::PartialsMetaData > & philote::Discipline::partials_meta ( )
inline

Accesses the partials meta data.

◆ partials_meta() [2/2]

const std::vector< philote::PartialsMetaData > & philote::Discipline::partials_meta ( ) const
inlinenoexcept

◆ properties()

philote::DisciplineProperties & philote::Discipline::properties ( )

Gets the discipline properties.

Returns
DisciplineProperties

◆ SetContext()

void philote::Discipline::SetContext ( grpc::ServerContext *  context) const
noexcept

Set the gRPC server context for cancellation detection.

This method is called by the server before invoking user-defined compute methods. It allows disciplines to check for cancellation during long-running computations.

Parameters
contextThe gRPC server context, or nullptr to clear

◆ SetOptions()

virtual void philote::Discipline::SetOptions ( const google::protobuf::Struct &  options_struct)
virtual

Sets up all discipline options based on a protobuf struct that the server received from the client.

Parameters
options_struct

◆ Setup()

virtual void philote::Discipline::Setup ( )
virtual

Setup function that is called by the server when the client calls the setup RPC.

◆ SetupPartials()

virtual void philote::Discipline::SetupPartials ( )
virtual

Setup function that is called by the server when the client calls the setup RPC.

This function is used to setup the partials.

◆ stream_opts() [1/2]

philote::StreamOptions & philote::Discipline::stream_opts ( )
inline

Gets the stream options.

Returns
StreamOptions&

◆ stream_opts() [2/2]

const philote::StreamOptions & philote::Discipline::stream_opts ( ) const
inlinenoexcept

◆ var_meta() [1/2]

std::vector< philote::VariableMetaData > & philote::Discipline::var_meta ( )
inline

Accesses the variable meta data.

◆ var_meta() [2/2]

const std::vector< philote::VariableMetaData > & philote::Discipline::var_meta ( ) const
inlinenoexcept

Member Data Documentation

◆ current_context_

grpc::ServerContext* philote::Discipline::current_context_ = nullptr
mutableprotected

Current gRPC server context for cancellation detection (mutable for const correctness)

◆ options_list_

std::map<std::string, std::string> philote::Discipline::options_list_
protected

List of options that can be set by the client.

◆ partials_meta_

std::vector<philote::PartialsMetaData> philote::Discipline::partials_meta_
protected

List of partials meta data.

◆ properties_

philote::DisciplineProperties philote::Discipline::properties_
protected

Discipline properties.

◆ stream_opts_

philote::StreamOptions philote::Discipline::stream_opts_
protected

Stream options.

◆ var_meta_

std::vector<philote::VariableMetaData> philote::Discipline::var_meta_
protected

List of variable meta data.


The documentation for this class was generated from the following file: