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

A class for storing continuous and discrete variables. More...

#include <variable.h>

Public Member Functions

 Variable ()=default
 Construct a new Variables object.
 
 Variable (const philote::VariableType &type, const std::vector< size_t > &shape)
 Construct a new Variable object.
 
 Variable (const philote::VariableMetaData &meta)
 Construct a new Variable object.
 
 Variable (const philote::PartialsMetaData &meta)
 Construct a new Variable object.
 
 ~Variable () noexcept=default
 Destroy the Variables object.
 
void Segment (const size_t &start, const size_t &end, const std::vector< double > &data)
 Assigns a segment of the array given a subvector.
 
std::vector< double > Segment (const size_t &start, const size_t &end) const
 Retrieves a reference to a segment of the array given a subvector.
 
std::vector< size_t > Shape () const noexcept
 Returns the shape of the array.
 
size_t Size () const noexcept
 Returns the size of the array.
 
double operator() (const size_t &i) const
 Returns the value of the array at a given index.
 
double & operator() (const size_t &i)
 Returns the value of the array at a given index.
 
philote::Array CreateChunk (const size_t &start, const size_t &end) const
 Create a Chunk of the variable.
 
void Send (std::string name, std::string subname, grpc::ClientReaderWriter<::philote::Array, ::philote::Array > *stream, const size_t &chunk_size) const
 Sends the variable from the client to the server.
 
void Send (std::string name, std::string subname, grpc::ServerReaderWriterInterface<::philote::Array, ::philote::Array > *stream, const size_t &chunk_size, grpc::ServerContext *context=nullptr) const
 Sends the variable from the server to the client using the interface.
 
void Send (std::string name, std::string subname, grpc::ClientReaderWriterInterface<::philote::Array, ::philote::Array > *stream, const size_t &chunk_size) const
 Sends the variable from the client to the server using the interface.
 
void AssignChunk (const Array &data)
 Assigns a chunk to the variable.
 

Detailed Description

A class for storing continuous and discrete variables.

The Variable class provides a container for multi-dimensional arrays used in Philote disciplines. Variables can represent inputs, outputs, or residuals and support operations like segmentation, streaming over gRPC, and element access.

Example: Creating and Using Variables
// Create a variable with shape
philote::Variable pressure(philote::kInput, {10, 10});
// Set values
for (size_t i = 0; i < 100; ++i) {
pressure(i) = 101325.0 + i * 10.0;
}
// Get shape information
std::vector<size_t> shape = pressure.Shape(); // Returns {10, 10}
size_t total = pressure.Size(); // Returns 100
// Access individual elements
double val = pressure(0); // Get first element
A class for storing continuous and discrete variables.
Definition variable.h:85
std::vector< size_t > Shape() const noexcept
Returns the shape of the array.
Example: Creating from Metadata
philote::VariableMetaData meta;
meta.set_name("temperature");
meta.add_shape(5);
meta.set_units("K");
meta.set_type(philote::kOutput);
philote::Variable temp(meta);
temp(0) = 300.0;
Note
Thread Safety: This class is NOT thread-safe. Concurrent reads and writes to the same Variable instance will cause data races. If multiple threads need to access the same Variable, external synchronization is required. Each thread should preferably have its own Variable instances.

Constructor & Destructor Documentation

◆ Variable() [1/4]

philote::Variable::Variable ( )
default

Construct a new Variables object.

◆ Variable() [2/4]

philote::Variable::Variable ( const philote::VariableType &  type,
const std::vector< size_t > &  shape 
)

Construct a new Variable object.

Parameters
type
shape

◆ Variable() [3/4]

philote::Variable::Variable ( const philote::VariableMetaData &  meta)
explicit

Construct a new Variable object.

Parameters
meta

◆ Variable() [4/4]

philote::Variable::Variable ( const philote::PartialsMetaData &  meta)
explicit

Construct a new Variable object.

Parameters
meta

◆ ~Variable()

philote::Variable::~Variable ( )
defaultnoexcept

Destroy the Variables object.

Member Function Documentation

◆ AssignChunk()

void philote::Variable::AssignChunk ( const Array &  data)

Assigns a chunk to the variable.

Parameters
stream
chunk_size

◆ CreateChunk()

philote::Array philote::Variable::CreateChunk ( const size_t &  start,
const size_t &  end 
) const

Create a Chunk of the variable.

Parameters
start
end
Returns
philote::Array

◆ operator()() [1/2]

double & philote::Variable::operator() ( const size_t &  i)

Returns the value of the array at a given index.

Parameters
indicesindices at which the array should be accessed
Returns
double value of the array at the given indices

◆ operator()() [2/2]

double philote::Variable::operator() ( const size_t &  i) const

Returns the value of the array at a given index.

Parameters
indicesindices at which the array should be accessed
Returns
double value of the array at the given indices

◆ Segment() [1/2]

std::vector< double > philote::Variable::Segment ( const size_t &  start,
const size_t &  end 
) const

Retrieves a reference to a segment of the array given a subvector.

Parameters
start
end
Returns
std::vector<Type>&

◆ Segment() [2/2]

void philote::Variable::Segment ( const size_t &  start,
const size_t &  end,
const std::vector< double > &  data 
)

Assigns a segment of the array given a subvector.

Parameters
startstarting index of the segment
endending index of the segment
datadata to be assigned to the segment
Example
philote::Variable vec(philote::kOutput, {100});
// Set a segment from indices 10 to 14 (inclusive)
std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
vec.Segment(10, 15, data);
void Segment(const size_t &start, const size_t &end, const std::vector< double > &data)
Assigns a segment of the array given a subvector.

◆ Send() [1/3]

void philote::Variable::Send ( std::string  name,
std::string  subname,
grpc::ClientReaderWriter<::philote::Array, ::philote::Array > *  stream,
const size_t &  chunk_size 
) const

Sends the variable from the client to the server.

Parameters
stream

◆ Send() [2/3]

void philote::Variable::Send ( std::string  name,
std::string  subname,
grpc::ClientReaderWriterInterface<::philote::Array, ::philote::Array > *  stream,
const size_t &  chunk_size 
) const

Sends the variable from the client to the server using the interface.

Parameters
stream

◆ Send() [3/3]

void philote::Variable::Send ( std::string  name,
std::string  subname,
grpc::ServerReaderWriterInterface<::philote::Array, ::philote::Array > *  stream,
const size_t &  chunk_size,
grpc::ServerContext *  context = nullptr 
) const

Sends the variable from the server to the client using the interface.

Parameters
nameVariable name
subnameVariable subname (for partials)
streamThe gRPC stream to write to
chunk_sizeNumber of elements per chunk
contextOptional server context for cancellation detection

◆ Shape()

std::vector< size_t > philote::Variable::Shape ( ) const
noexcept

Returns the shape of the array.

Returns
std::vector<size_t> vector containing the length of the individual dimensions

◆ Size()

size_t philote::Variable::Size ( ) const
noexcept

Returns the size of the array.

Returns
size_t size of the array

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