Introduction
The philote::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.
Creating Variables
From Type and Shape
for (size_t i = 0; i < 100; ++i) {
pressure(i) = 101325.0 + i * 10.0;
}
A class for storing continuous and discrete variables.
Definition variable.h:85
From Metadata
philote::VariableMetaData meta;
meta.set_name("temperature");
meta.add_shape(5);
meta.set_units("K");
meta.set_type(philote::kOutput);
temp(0) = 300.0;
Accessing Variable Data
Element Access
vec(0) = 1.5;
vec(99) = 2.7;
double val = vec(0);
Shape Information
std::vector<size_t> shape = matrix.
Shape();
size_t total_size = matrix.Size();
std::vector< size_t > Shape() const noexcept
Returns the shape of the array.
Working with Segments
Variables support partial array access through segments:
std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
std::vector<double> retrieved = large_array.Segment(10, 15);
void Segment(const size_t &start, const size_t &end, const std::vector< double > &data)
Assigns a segment of the array given a subvector.
Variables Collections
Variables Map
The philote::Variables type is a map of variable names to Variable objects:
inputs.at("x")(0) = 5.0;
inputs.at("y")(0) = -2.0;
std::map< std::string, philote::Variable > Variables
Definition variable.h:404
Partials (Jacobians)
Partials use pairs of strings as keys (output, input):
gradients[std::make_pair("f", "x")](0) = 2.5;
gradients[{"f", "y"}](0) = 3.7;
std::map< std::pair< std::string, std::string >, philote::Variable > Partials
Definition variable.h:405
PairDict for Cleaner Access
For more readable gradient access, use PairDict:
gradients("f", "x")(0) = 2.5;
gradients("f", "y")(0) = 3.7;
double df_dx = gradients("f", "x")(0);
}
Dictionary for storing values with respect to two keys.
Definition variable.h:292
bool contains(const std::string &key1, const std::string &key2) const noexcept
Check if a key pair exists.
Definition variable.h:355
Multi-dimensional Arrays
Variables support multi-dimensional arrays with row-major ordering:
matrix(0) = 1.0;
matrix(1) = 2.0;
matrix(2) = 3.0;
matrix(3) = 4.0;
Best Practices
- Always specify units: Provide physical units for all variables
- Check dimensions: Ensure variable shapes match expectations
- Use descriptive names: Variable names should be clear and meaningful
- Preallocate: Create variables with correct shape before filling data
- Use type-safe access: Prefer
.at() over [] for bounds checking
See Also