Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,32 @@ rather than `class` for template type parameters.

template <typename real_type> ...; // Yes
```
Types used within class implementation code and provided in the public class
interface should use `UpperCamelT` format, similar to class names, but with a
`T` suffix. For consistency, create aliases for template type parameters
(*e.g.,* `using ScalarT = scalar_type;`) and use the same name everywhere the
same type is used. This applies across multiple classes in which the type has
the same meaning.
Types used within class implementation code should use `UpperCamelT` format,
similar to class names, but with a `T` suffix. For consistency, create aliases
for template type parameters (*e.g.,* `using ScalarT = scalar_type;`) and use
the same name everywhere the same type is used. This applies across multiple
classes in which the type has the same meaning.

These aliases are implementation details of the class that declares them, so
keep them `private`. Only make an alias `public` (or `protected`) when other
code must name it through the class, for example a base class alias that
derived classes re-export, a data struct whose aliases a concept inspects, or a
type that another class reads as `OtherClass::SomeT`.

```c++
template <typename scalar_type, typename index_type>
class ExTemp
{
private:
// Convention used in all applicable classes for scalar, index, and
// primitive floating-point types
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = GridKit::ScalarTraits<ScalarT>::RealT;

public:
// void foo(scalar_type value); // No, using template type parameter
void foo(ScalarT value); // Yes, using interface types in code
void foo(ScalarT value); // Yes, using the class aliases in code
};
```

Expand Down
7 changes: 4 additions & 3 deletions GridKit/AutomaticDifferentiation/Enzyme/DfDwb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <GridKit/AutomaticDifferentiation/Enzyme/EnzymeDefinitions.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/LowerSparseStorage.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp>
#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
Expand All @@ -25,9 +26,9 @@ namespace GridKit
template <typename ModelT, MemberFunctions function>
struct DfDwb
{
using ScalarT = typename ModelT::ScalarT;
using IdxT = typename ModelT::IdxT;
using RealT = typename ModelT::RealT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;
using IdxT = typename GridKit::Model::EvaluatorTraits<ModelT>::IdxT;
using RealT = typename GridKit::Model::EvaluatorTraits<ModelT>::RealT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
7 changes: 4 additions & 3 deletions GridKit/AutomaticDifferentiation/Enzyme/DfDws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <GridKit/AutomaticDifferentiation/Enzyme/EnzymeDefinitions.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/LowerSparseStorage.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp>
#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
Expand All @@ -25,9 +26,9 @@ namespace GridKit
template <typename ModelT, MemberFunctions function>
struct DfDws
{
using ScalarT = typename ModelT::ScalarT;
using IdxT = typename ModelT::IdxT;
using RealT = typename ModelT::RealT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;
using IdxT = typename GridKit::Model::EvaluatorTraits<ModelT>::IdxT;
using RealT = typename GridKit::Model::EvaluatorTraits<ModelT>::RealT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
7 changes: 4 additions & 3 deletions GridKit/AutomaticDifferentiation/Enzyme/DfDy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <GridKit/AutomaticDifferentiation/Enzyme/EnzymeDefinitions.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/LowerSparseStorage.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp>
#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
Expand All @@ -25,9 +26,9 @@ namespace GridKit
template <typename ModelT, MemberFunctions function>
struct DfDy
{
using ScalarT = typename ModelT::ScalarT;
using IdxT = typename ModelT::IdxT;
using RealT = typename ModelT::RealT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;
using IdxT = typename GridKit::Model::EvaluatorTraits<ModelT>::IdxT;
using RealT = typename GridKit::Model::EvaluatorTraits<ModelT>::RealT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
7 changes: 4 additions & 3 deletions GridKit/AutomaticDifferentiation/Enzyme/DfDyp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <GridKit/AutomaticDifferentiation/Enzyme/EnzymeDefinitions.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/LowerSparseStorage.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp>
#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
Expand All @@ -25,9 +26,9 @@ namespace GridKit
template <typename ModelT, MemberFunctions function>
struct DfDyp
{
using ScalarT = typename ModelT::ScalarT;
using IdxT = typename ModelT::IdxT;
using RealT = typename ModelT::RealT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;
using IdxT = typename GridKit::Model::EvaluatorTraits<ModelT>::IdxT;
using RealT = typename GridKit::Model::EvaluatorTraits<ModelT>::RealT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
7 changes: 4 additions & 3 deletions GridKit/AutomaticDifferentiation/Enzyme/DhDwb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <GridKit/AutomaticDifferentiation/Enzyme/EnzymeDefinitions.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/LowerSparseStorage.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp>
#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
Expand All @@ -25,9 +26,9 @@ namespace GridKit
template <typename ModelT, MemberFunctions function>
struct DhDwb
{
using ScalarT = typename ModelT::ScalarT;
using IdxT = typename ModelT::IdxT;
using RealT = typename ModelT::RealT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;
using IdxT = typename GridKit::Model::EvaluatorTraits<ModelT>::IdxT;
using RealT = typename GridKit::Model::EvaluatorTraits<ModelT>::RealT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
7 changes: 4 additions & 3 deletions GridKit/AutomaticDifferentiation/Enzyme/DhDy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <GridKit/AutomaticDifferentiation/Enzyme/EnzymeDefinitions.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/LowerSparseStorage.hpp>
#include <GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp>
#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
Expand All @@ -25,9 +26,9 @@ namespace GridKit
template <typename ModelT, MemberFunctions function>
struct DhDy
{
using ScalarT = typename ModelT::ScalarT;
using IdxT = typename ModelT::IdxT;
using RealT = typename ModelT::RealT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;
using IdxT = typename GridKit::Model::EvaluatorTraits<ModelT>::IdxT;
using RealT = typename GridKit::Model::EvaluatorTraits<ModelT>::RealT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
16 changes: 9 additions & 7 deletions GridKit/AutomaticDifferentiation/Enzyme/ModelWrappers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <vector>

#include <GridKit/Model/EvaluatorTraits.hpp>

namespace GridKit
{
namespace Enzyme
Expand Down Expand Up @@ -48,7 +50,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::InternalResidual>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand All @@ -74,7 +76,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::InternalResidualWithSignal>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down Expand Up @@ -102,7 +104,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::BusResidual>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand All @@ -128,7 +130,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::BusResidual11>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand All @@ -154,7 +156,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::BusResidual12>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand All @@ -180,7 +182,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::BusResidual21>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand All @@ -207,7 +209,7 @@ namespace GridKit
template <typename ModelT>
struct ModelWrapper<ModelT, MemberFunctions::BusResidual22>
{
using ScalarT = typename ModelT::ScalarT;
using ScalarT = typename GridKit::Model::EvaluatorTraits<ModelT>::ScalarT;

/**
* @param[in] model - Pointer to the model to be differentiated
Expand Down
40 changes: 40 additions & 0 deletions GridKit/Model/EvaluatorTraits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file EvaluatorTraits.hpp
* @brief Scalar and index types of a model, taken from its Evaluator base.
*/

#pragma once

#include <utility>

#include <GridKit/Model/Evaluator.hpp>

namespace GridKit
{
namespace Model
{
/**
* @brief Exposes the scalar, index, and real types of any model that
* publicly derives from Evaluator.
*
* Model classes keep their own type aliases private, so generic code
* (for example the Enzyme Jacobian evaluators) obtains the types through
* this trait instead of through the model class.
*
* @tparam model_type Model type derived from Evaluator
*/
template <typename model_type>
struct EvaluatorTraits
{
private:
template <typename scalar_type, typename index_type>
static Evaluator<scalar_type, index_type> probe(const Evaluator<scalar_type, index_type>&);

public:
using EvaluatorT = decltype(probe(std::declval<const model_type&>()));
using ScalarT = typename EvaluatorT::ScalarT;
using IdxT = typename EvaluatorT::IdxT;
using RealT = typename EvaluatorT::RealT;
};
} // namespace Model
} // namespace GridKit
3 changes: 2 additions & 1 deletion GridKit/Model/PhasorDynamics/Branch/Branch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ namespace GridKit
using Component<scalar_type, index_type>::residual_indices_;
using Component<scalar_type, index_type>::allocated_;

public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename Component<ScalarT, IdxT>::RealT;
using BusT = BusBase<ScalarT, IdxT>;
using ModelDataT = BranchData<RealT, IdxT>;
using MonitorT = Model::VariableMonitor<Branch, BranchData>;

public:
Branch(BusT* bus1, BusT* bus2);
Branch(BusT* bus1,
BusT* bus2,
Expand Down
3 changes: 2 additions & 1 deletion GridKit/Model/PhasorDynamics/Bus/Bus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace GridKit
using BusBase<scalar_type, index_type>::monitor_;
using BusBase<scalar_type, index_type>::allocated_;

public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename BusBase<ScalarT, IdxT>::RealT;
Expand All @@ -41,6 +41,7 @@ namespace GridKit
using ModelDataT = BusData<RealT, IdxT>;
using BusTypeT = typename BusData<RealT, IdxT>::BusType;

public:
Bus();
Bus(ScalarT Vr, ScalarT Vi);
Bus(const ModelDataT& data);
Expand Down
3 changes: 2 additions & 1 deletion GridKit/Model/PhasorDynamics/Bus/BusFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ namespace GridKit
template <typename scalar_type = double, typename index_type = int>
class BusFactory
{
public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename Model::Evaluator<ScalarT, IdxT>::RealT;
using SignalNodeSetT = SignalNodeSet<ScalarT, IdxT>;
using BusData = GridKit::PhasorDynamics::BusData<RealT, IdxT>;
using BusTypeT = typename GridKit::PhasorDynamics::BusData<RealT, IdxT>::BusType;

public:
BusFactory() = delete;

static BusBase<ScalarT, IdxT>* create(const BusData& data, SignalNodeSetT& signal_nodes)
Expand Down
3 changes: 2 additions & 1 deletion GridKit/Model/PhasorDynamics/Bus/BusInfinite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace GridKit
using BusBase<scalar_type, index_type>::monitor_;
using BusBase<scalar_type, index_type>::allocated_;

public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename BusBase<ScalarT, IdxT>::RealT;
Expand All @@ -37,6 +37,7 @@ namespace GridKit
using ModelDataT = BusData<RealT, IdxT>;
using BusTypeT = typename BusData<RealT, IdxT>::BusType;

public:
BusInfinite();
BusInfinite(ScalarT Vr, ScalarT Vi);
BusInfinite(const ModelDataT& data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace GridKit
using BusBase<scalar_type, index_type>::monitor_;
using BusBase<scalar_type, index_type>::allocated_;

public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename BusBase<ScalarT, IdxT>::RealT;
Expand All @@ -61,6 +61,7 @@ namespace GridKit
using BusTypeT = typename BusData<RealT, IdxT>::BusType;
using SignalPortsT = SignalPorts<ScalarT, ModelDataT>;

public:
BusSignalVoltageIn();
/// Initial voltage arguments are ignored; the voltage comes from signals.
BusSignalVoltageIn(ScalarT Vr, ScalarT Vi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace GridKit
using BusBase<scalar_type, index_type>::monitor_;
using BusBase<scalar_type, index_type>::allocated_;

public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename BusBase<ScalarT, IdxT>::RealT;
Expand All @@ -60,6 +60,7 @@ namespace GridKit
using BusTypeT = typename BusData<RealT, IdxT>::BusType;
using SignalPortsT = SignalPorts<ScalarT, ModelDataT>;

public:
BusSignalVoltageOut();
BusSignalVoltageOut(ScalarT Vr, ScalarT Vi);
BusSignalVoltageOut(const ModelDataT& data);
Expand Down
3 changes: 2 additions & 1 deletion GridKit/Model/PhasorDynamics/Controller/REECB/Reecb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace GridKit
using Component<scalar_type, index_type>::y_;
using Component<scalar_type, index_type>::yp_;

public:
private:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename Component<ScalarT, IdxT>::RealT;
Expand All @@ -106,6 +106,7 @@ namespace GridKit
using InternalVariablesT = ReecbInternalVariables;
using ExternalVariablesT = ReecbExternalVariables;

public:
/// Initialization reconstruction tolerance.
static constexpr RealT INITIALIZATION_TOLERANCE =
static_cast<RealT>(100.0) * std::numeric_limits<RealT>::epsilon();
Expand Down
Loading
Loading