Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
- Added `REGCA` converter model implementation for PhasorDynamics.
- Remove unnecessary data copying while evaluating `PowerElectronics` models, speeding up large simulations by up to 3x
- Added `HYGOV` governor model implementation for PhasorDynamics.
- Added Windows compatibility (G++, Clang, MSVC)

## v0.1

Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -Wpedantic")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_definitions(FORCE_INLINE=[[gnu::always_inline]]\ inline)
elseif(MSVC)
add_compile_definitions(FORCE_INLINE=[[msvc::forceinline]]\ inline)
else()
add_compile_definitions(FORCE_INLINE=inline)
endif()

set(GRIDKIT_THIRD_PARTY_DIR ${PROJECT_SOURCE_DIR}/third-party)

# Ipopt support is disabled by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ namespace GridKit
* @param[in,out] nnz - number of nonzeros
*/
template <typename ScalarT, typename IdxT>
__attribute__((always_inline)) static void sparse_store(
FORCE_INLINE static void sparse_store(
ScalarT val,
IdxT row,
IdxT col,
Expand Down Expand Up @@ -251,7 +251,7 @@ namespace GridKit
* @tparam IdxT - matrix index data type
*/
template <typename ScalarT, typename IdxT>
__attribute__((always_inline)) static ScalarT sparse_load(IdxT, IdxT, IdxT*, IdxT*, ScalarT*)
FORCE_INLINE static ScalarT sparse_load(IdxT, IdxT, IdxT*, IdxT*, ScalarT*)
{
return 0.0;
}
Expand All @@ -263,7 +263,7 @@ namespace GridKit
* @tparam IdxT - matrix index data type
*/
template <typename ScalarT, typename IdxT>
__attribute__((always_inline)) static void ident_store(ScalarT, IdxT, IdxT)
FORCE_INLINE static void ident_store(ScalarT, IdxT, IdxT)
{
assert(0 && "should never store");
}
Expand All @@ -275,7 +275,7 @@ namespace GridKit
* @tparam IdxT - matrix index data type
*/
template <typename ScalarT, typename IdxT>
__attribute__((always_inline)) static ScalarT ident_load(IdxT row, IdxT col)
FORCE_INLINE static ScalarT ident_load(IdxT row, IdxT col)
{
row /= sizeof(ScalarT);
return (ScalarT) (row == col);
Expand Down
32 changes: 16 additions & 16 deletions GridKit/CommonMath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace GridKit
* @return value of the sigmoid function
*/
template <class ScalarT>
__attribute__((always_inline)) inline ScalarT sigmoid(const ScalarT x)
FORCE_INLINE ScalarT sigmoid(const ScalarT x)
{
using RealT = typename GridKit::ScalarTraits<ScalarT>::RealT;
return HALF<RealT> * (ONE<RealT> + std::tanh(HALF<RealT> * MU<RealT> * x));
Expand All @@ -52,7 +52,7 @@ namespace GridKit
* @return value of the smooth ramp function
*/
template <class ScalarT>
__attribute__((always_inline)) inline ScalarT ramp(const ScalarT x)
FORCE_INLINE ScalarT ramp(const ScalarT x)
{
using RealT = typename GridKit::ScalarTraits<ScalarT>::RealT;

Expand All @@ -76,7 +76,7 @@ namespace GridKit
* @return value of the quadratic ramp
*/
template <class ScalarT>
__attribute__((always_inline)) inline ScalarT qramp(const ScalarT x)
FORCE_INLINE ScalarT qramp(const ScalarT x)
{
return x * x * sigmoid(x);
}
Expand All @@ -101,7 +101,7 @@ namespace GridKit
* forcing callers to cast every parameter.
*/
template <class LeftT, class RightT>
__attribute__((always_inline)) inline auto max(
FORCE_INLINE auto max(
const LeftT x,
const RightT y)
{
Expand All @@ -128,7 +128,7 @@ namespace GridKit
* forcing callers to cast every parameter.
*/
template <class LeftT, class RightT>
__attribute__((always_inline)) inline auto min(
FORCE_INLINE auto min(
const LeftT x,
const RightT y)
{
Expand All @@ -152,7 +152,7 @@ namespace GridKit
* @return value of the smooth clamp function
*/
template <class ScalarT, typename LowerT, typename UpperT>
__attribute__((always_inline)) inline auto clamp(
FORCE_INLINE auto clamp(
const ScalarT x,
const LowerT lower,
const UpperT upper)
Expand All @@ -176,7 +176,7 @@ namespace GridKit
* @return Smooth no-offset deadbanded value
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT deadband1(
FORCE_INLINE ScalarT deadband1(
const ScalarT x,
const RealT lower,
const RealT upper)
Expand All @@ -200,7 +200,7 @@ namespace GridKit
* @return Smooth offset deadbanded value
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT deadband2(
FORCE_INLINE ScalarT deadband2(
const ScalarT x,
const RealT lower,
const RealT upper)
Expand All @@ -222,7 +222,7 @@ namespace GridKit
* @return Slew-rate-limited value of f
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT slew(
FORCE_INLINE ScalarT slew(
const ScalarT f,
const RealT rate)
{
Expand All @@ -247,7 +247,7 @@ namespace GridKit
* @return Smooth linear segment contribution
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT linseg(
FORCE_INLINE ScalarT linseg(
const ScalarT x,
const RealT lower,
const RealT upper,
Expand All @@ -268,7 +268,7 @@ namespace GridKit
* @return Smooth indicator that x is above limit_min
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT above(
FORCE_INLINE ScalarT above(
const ScalarT x,
const RealT limit_min)
{
Expand All @@ -286,7 +286,7 @@ namespace GridKit
* @return Smooth indicator that x is below limit_max
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT below(
FORCE_INLINE ScalarT below(
const ScalarT x,
const RealT limit_max)
{
Expand All @@ -305,7 +305,7 @@ namespace GridKit
* @return Smooth indicator that x is inside [limit_min, limit_max]
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT inside(
FORCE_INLINE ScalarT inside(
const ScalarT x,
const RealT limit_min,
const RealT limit_max)
Expand All @@ -326,7 +326,7 @@ namespace GridKit
* @return Smooth indicator that x is outside [limit_min, limit_max]
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT outside(
FORCE_INLINE ScalarT outside(
const ScalarT x,
const RealT limit_min,
const RealT limit_max)
Expand All @@ -349,7 +349,7 @@ namespace GridKit
* 0 when integration should be blocked.
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT indicator(
FORCE_INLINE ScalarT indicator(
const ScalarT x,
const ScalarT f,
const RealT limit_min,
Expand Down Expand Up @@ -383,7 +383,7 @@ namespace GridKit
* @return Smooth anti-windup limited derivative
*/
template <class ScalarT, typename RealT>
__attribute__((always_inline)) inline ScalarT antiwindup(
FORCE_INLINE ScalarT antiwindup(
const ScalarT x,
const ScalarT f,
const RealT limit_min,
Expand Down
4 changes: 4 additions & 0 deletions GridKit/Definitions.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
#define GRIDKIT_VERSION_MAJOR "@GridKit_VERSION_MAJOR@"
#define GRIDKIT_VERSION_MINOR "@GridKit_VERSION_MINOR@"
#define GRIDKIT_VERSION_PATCH "@GridKit_VERSION_PATCH@"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
Comment on lines +14 to +16

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this:

Suggested change
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

12 changes: 6 additions & 6 deletions GridKit/Model/PhasorDynamics/Branch/Branch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ namespace GridKit
typename ModelDataT::Parameters parameter,
RealT& target);

static __attribute__((always_inline)) inline void addAdmittanceContribution(const RealT G,
FORCE_INLINE static void addAdmittanceContribution(const RealT G,
const RealT B,
const ScalarT Vr,
const ScalarT Vi,
ScalarT& Ir,
ScalarT& Ii);

static __attribute__((always_inline)) inline void evaluateAdmittanceBlock(const RealT G,
FORCE_INLINE static void evaluateAdmittanceBlock(const RealT G,
const RealT B,
const ScalarT* wb,
ScalarT* h);
Expand Down Expand Up @@ -188,13 +188,13 @@ namespace GridKit
}

public:
__attribute__((always_inline)) inline int evaluateBusResidual11(
FORCE_INLINE int evaluateBusResidual11(
const ScalarT*, const ScalarT*, const ScalarT*, ScalarT*);
__attribute__((always_inline)) inline int evaluateBusResidual12(
FORCE_INLINE int evaluateBusResidual12(
const ScalarT*, const ScalarT*, const ScalarT*, ScalarT*);
__attribute__((always_inline)) inline int evaluateBusResidual21(
FORCE_INLINE int evaluateBusResidual21(
const ScalarT*, const ScalarT*, const ScalarT*, ScalarT*);
__attribute__((always_inline)) inline int evaluateBusResidual22(
FORCE_INLINE int evaluateBusResidual22(
const ScalarT*, const ScalarT*, const ScalarT*, ScalarT*);

private:
Expand Down
12 changes: 6 additions & 6 deletions GridKit/Model/PhasorDynamics/Branch/BranchImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace GridKit
}

template <typename scalar_type, typename index_type>
__attribute__((always_inline)) inline void Branch<scalar_type, index_type>::addAdmittanceContribution(
FORCE_INLINE void Branch<scalar_type, index_type>::addAdmittanceContribution(
const RealT G,
const RealT B,
const ScalarT Vr,
Expand All @@ -198,7 +198,7 @@ namespace GridKit
}

template <typename scalar_type, typename index_type>
__attribute__((always_inline)) inline void Branch<scalar_type, index_type>::evaluateAdmittanceBlock(
FORCE_INLINE void Branch<scalar_type, index_type>::evaluateAdmittanceBlock(
const RealT G,
const RealT B,
const ScalarT* wb,
Expand Down Expand Up @@ -234,7 +234,7 @@ namespace GridKit
*
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) inline int Branch<scalar_type, index_type>::evaluateBusResidual11(
FORCE_INLINE int Branch<scalar_type, index_type>::evaluateBusResidual11(
[[maybe_unused]] const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
const ScalarT* wb,
Expand All @@ -250,7 +250,7 @@ namespace GridKit
*
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) inline int Branch<scalar_type, index_type>::evaluateBusResidual12(
FORCE_INLINE int Branch<scalar_type, index_type>::evaluateBusResidual12(
[[maybe_unused]] const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
const ScalarT* wb,
Expand All @@ -266,7 +266,7 @@ namespace GridKit
*
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) int Branch<scalar_type, index_type>::evaluateBusResidual21(
FORCE_INLINE int Branch<scalar_type, index_type>::evaluateBusResidual21(
[[maybe_unused]] const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
const ScalarT* wb,
Expand All @@ -282,7 +282,7 @@ namespace GridKit
*
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) int Branch<scalar_type, index_type>::evaluateBusResidual22(
FORCE_INLINE int Branch<scalar_type, index_type>::evaluateBusResidual22(
[[maybe_unused]] const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
const ScalarT* wb,
Expand Down
4 changes: 2 additions & 2 deletions GridKit/Model/PhasorDynamics/BusFault/BusFault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ namespace GridKit
}

public:
__attribute__((always_inline)) inline int evaluateBusResidual(
FORCE_INLINE int evaluateBusResidual(
const ScalarT*, const ScalarT*, const ScalarT*, ScalarT*);
__attribute__((always_inline)) inline int evaluateInternalResidual(
FORCE_INLINE int evaluateInternalResidual(
const ScalarT*, const ScalarT*, const ScalarT*, ScalarT*);

private:
Expand Down
4 changes: 2 additions & 2 deletions GridKit/Model/PhasorDynamics/BusFault/BusFaultImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ namespace GridKit
*
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) int BusFault<scalar_type, index_type>::evaluateBusResidual(
FORCE_INLINE int BusFault<scalar_type, index_type>::evaluateBusResidual(
const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
[[maybe_unused]] const ScalarT* wb,
Expand All @@ -228,7 +228,7 @@ namespace GridKit
*
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) int BusFault<scalar_type, index_type>::evaluateInternalResidual(
FORCE_INLINE int BusFault<scalar_type, index_type>::evaluateInternalResidual(
const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
const ScalarT* wb,
Expand Down
8 changes: 4 additions & 4 deletions GridKit/Model/PhasorDynamics/Converter/REGCA/Regca.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ namespace GridKit

const Model::VariableMonitorBase* getMonitor() const override;

__attribute__((always_inline)) inline int evaluateInternalResidual(
FORCE_INLINE int evaluateInternalResidual(
const ScalarT* y, const ScalarT* yp, const ScalarT* wb, const ScalarT* ws, ScalarT* f);

__attribute__((always_inline)) inline int evaluateBusResidual(
FORCE_INLINE int evaluateBusResidual(
const ScalarT* y, const ScalarT* yp, const ScalarT* wb, ScalarT* h);

private:
Expand All @@ -149,7 +149,7 @@ namespace GridKit
*
* @todo Move this reusable limiter to CommonMath.
*/
static __attribute__((always_inline)) inline ScalarT rrpwr(
FORCE_INLINE static ScalarT rrpwr(
const ScalarT x,
const ScalarT f,
const RealT rate)
Expand Down Expand Up @@ -181,7 +181,7 @@ namespace GridKit
*
* @todo Move this one-sided anti-windup helper to CommonMath.
*/
static __attribute__((always_inline)) inline ScalarT awmax(
FORCE_INLINE static ScalarT awmax(
const ScalarT x,
const ScalarT f,
const ScalarT xmax,
Expand Down
4 changes: 2 additions & 2 deletions GridKit/Model/PhasorDynamics/Converter/REGCA/RegcaImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ namespace GridKit
* @param[out] f Internal residuals.
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) inline int
FORCE_INLINE int
Regca<scalar_type, index_type>::evaluateInternalResidual(
const ScalarT* y,
const ScalarT* yp,
Expand Down Expand Up @@ -701,7 +701,7 @@ namespace GridKit
* @param[out] h Current injected into the terminal bus.
*/
template <typename scalar_type, typename index_type>
__attribute__((always_inline)) inline int Regca<scalar_type, index_type>::evaluateBusResidual(
FORCE_INLINE int Regca<scalar_type, index_type>::evaluateBusResidual(
const ScalarT* y,
[[maybe_unused]] const ScalarT* yp,
[[maybe_unused]] const ScalarT* wb,
Expand Down
Loading