Skip to content

Commit

Permalink
Merge branch 'main' into feature/doc_on_batch
Browse files Browse the repository at this point in the history
  • Loading branch information
zhen0427 authored Mar 7, 2024
2 parents 67924a3 + 88e4e32 commit c537a49
Show file tree
Hide file tree
Showing 81 changed files with 2,377 additions and 2,059 deletions.
4 changes: 2 additions & 2 deletions code_generation/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def render_attribute_classes(self, template_path: Path, data_path: Path, output_
if attribute_class.is_template:
attribute_class.full_name = f"{attribute_class.name}<sym>"
attribute_class.specification_names = [
f"{attribute_class.name}<true>",
f"{attribute_class.name}<false>",
f"{attribute_class.name}<symmetric_t>",
f"{attribute_class.name}<asymmetric_t>",
f"Sym{attribute_class.name}",
f"Asym{attribute_class.name}",
]
Expand Down
18 changes: 9 additions & 9 deletions code_generation/data/attribute_classes/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@
"is_template": false,
"attributes": [
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": "i_f",
"description": "three phase short circuit current magnitude"
},
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": "i_f_angle",
"description": "three phase short circuit current angle"
}
Expand All @@ -191,7 +191,7 @@
"is_template": false,
"attributes": [
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"u_pu",
"u",
Expand All @@ -207,15 +207,15 @@
"is_template": false,
"attributes": [
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"i_from",
"i_from_angle"
],
"description": "initial three phase short circuit current flow at from-side"
},
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"i_to",
"i_to_angle"
Expand All @@ -230,23 +230,23 @@
"is_template": false,
"attributes": [
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"i_1",
"i_1_angle"
],
"description": "initial three phase short circuit current flow at side 1"
},
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"i_2",
"i_2_angle"
],
"description": "initial three phase short circuit current flow at side 2"
},
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"i_3",
"i_3_angle"
Expand All @@ -261,7 +261,7 @@
"is_template": false,
"attributes": [
{
"data_type": "RealValue<false>",
"data_type": "RealValue<asymmetric_t>",
"names": [
"i",
"i_angle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ namespace power_grid_model {
{% for attribute_class in classes -%}

{%- if attribute_class.is_template -%}
template <bool sym>
{% endif -%}
template <symmetry_tag sym_type>
struct {{ attribute_class.name }} {
using sym = sym_type;
{% else -%}
struct {{ attribute_class.name }} {
{%- endif -%}

{%- for attribute in attribute_class.full_attributes %}
{{ attribute.data_type }} {{ attribute.names }};
{%- if attribute.description %} // {{ attribute.description }}{%- endif %}
Expand All @@ -32,8 +36,8 @@ struct {{ attribute_class.name }} {
{% endfor -%}
};
{% if attribute_class.is_template %}
using Sym{{ attribute_class.name }} = {{ attribute_class.name }}<true>;
using Asym{{ attribute_class.name }} = {{ attribute_class.name }}<false>;
using Sym{{ attribute_class.name }} = {{ attribute_class.name }}<symmetric_t>;
using Asym{{ attribute_class.name }} = {{ attribute_class.name }}<asymmetric_t>;
{% endif %}
{% endfor %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ namespace power_grid_model::meta_data {
{% for attribute_class in classes -%}

{%- if attribute_class.is_template -%}
template <bool sym>
struct get_attributes_list<{{ attribute_class.name }}<sym>> {
{%- else -%}
template <symmetry_tag sym_type>
struct get_attributes_list<{{ attribute_class.name }}<sym_type>> {
using sym = sym_type;
{% else -%}
template<>
struct get_attributes_list<{{ attribute_class.name }}> {
{%- endif %}
Expand All @@ -45,9 +46,10 @@ struct get_attributes_list<{{ attribute_class.name }}> {
{% for attribute_class in classes -%}

{%- if attribute_class.is_template -%}
template <bool sym>
struct get_component_nan<{{ attribute_class.name }}<sym>> {
{%- else -%}
template <symmetry_tag sym_type>
struct get_component_nan<{{ attribute_class.name }}<sym_type>> {
using sym = sym_type;
{% else -%}
template<>
struct get_component_nan<{{ attribute_class.name }}> {
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// define dataset classes with void pointers

#include "dataset_fwd.hpp"

#include "../common/common.hpp"

#include <cassert>
Expand All @@ -25,8 +27,19 @@ namespace power_grid_model {
// the indptr is a nullptr, for i-th sets,
// the set data is in the range [ i * elements_per_scenario, (i + 1) * elements_per_scenario )

template <bool is_const> class DataPointer {
template <class T> using ptr_t = std::conditional_t<is_const, T const*, T*>;
template <dataset_type_tag T> constexpr bool is_const_dataset_v = std::same_as<T, const_dataset_t>;

static_assert(dataset_type_tag<const_dataset_t>);
static_assert(dataset_type_tag<mutable_dataset_t>);
static_assert(is_const_dataset_v<const_dataset_t>);
static_assert(!is_const_dataset_v<mutable_dataset_t>);

template <dataset_type_tag dataset_type_> class DataPointer {
public:
using dataset_type = dataset_type_;

private:
template <class T> using ptr_t = std::conditional_t<is_const_dataset_v<dataset_type>, T const*, T*>;

public:
DataPointer() : ptr_{nullptr}, indptr_{nullptr}, batch_size_{}, elements_per_scenario_{} {}
Expand Down Expand Up @@ -85,10 +98,10 @@ template <bool is_const> class DataPointer {
}

// conversion to const iterator
explicit operator DataPointer<true>() const
requires(!is_const)
explicit operator DataPointer<const_dataset_t>() const
requires(!is_const_dataset_v<dataset_type>)
{
return DataPointer<true>{ptr_, indptr_, batch_size_, elements_per_scenario_};
return DataPointer<const_dataset_t>{ptr_, indptr_, batch_size_, elements_per_scenario_};
}

private:
Expand All @@ -98,8 +111,8 @@ template <bool is_const> class DataPointer {
Idx elements_per_scenario_; // number of data points per batch, -1 for variable batches
};

using MutableDataPointer = DataPointer<false>;
using ConstDataPointer = DataPointer<true>;
using MutableDataPointer = DataPointer<mutable_dataset_t>;
using ConstDataPointer = DataPointer<const_dataset_t>;

using Dataset = std::map<std::string, MutableDataPointer>;
using ConstDataset = std::map<std::string, ConstDataPointer>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
//
// SPDX-License-Identifier: MPL-2.0

#pragma once

// forward declarations for dataset (handler) and buffer related stuff

#include <concepts>

namespace power_grid_model {

struct const_dataset_t {};
struct mutable_dataset_t {};
struct writable_dataset_t {};

template <typename T>
concept dataset_type_tag = std::same_as<T, const_dataset_t> || std::same_as<T, mutable_dataset_t>;

namespace meta_data {
template <typename T>
concept dataset_handler_tag =
std::same_as<T, const_dataset_t> || std::same_as<T, mutable_dataset_t> || std::same_as<T, writable_dataset_t>;
} // namespace meta_data

} // namespace power_grid_model
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

namespace power_grid_model::meta_data {

template <dataset_handler_tag T>
constexpr bool is_data_mutable_v = std::same_as<T, mutable_dataset_t> || std::same_as<T, writable_dataset_t>;
template <dataset_handler_tag T> constexpr bool is_indptr_mutable_v = std::same_as<T, writable_dataset_t>;

static_assert(dataset_handler_tag<const_dataset_t>);
static_assert(dataset_handler_tag<mutable_dataset_t>);
static_assert(dataset_handler_tag<writable_dataset_t>);
static_assert(!is_data_mutable_v<const_dataset_t>);
static_assert(is_data_mutable_v<mutable_dataset_t>);
static_assert(is_data_mutable_v<writable_dataset_t>);
static_assert(!is_indptr_mutable_v<const_dataset_t>);
static_assert(!is_indptr_mutable_v<mutable_dataset_t>);
static_assert(is_indptr_mutable_v<writable_dataset_t>);

struct ComponentInfo {
MetaComponent const* component;
// for non-uniform component, this is -1, we use indptr to describe the elements per scenario
Expand All @@ -31,12 +45,16 @@ struct DatasetInfo {
std::vector<ComponentInfo> component_info;
};

template <bool data_mutable, bool indptr_mutable>
requires(data_mutable || !indptr_mutable)
class DatasetHandler {
template <dataset_handler_tag dataset_handler_type_> class DatasetHandler {
struct immutable_t {};
struct mutable_t {};

public:
using Data = std::conditional_t<data_mutable, void, void const>;
using Indptr = std::conditional_t<indptr_mutable, Idx, Idx const>;
using dataset_handler_type = dataset_handler_type_;

using Data = std::conditional_t<is_data_mutable_v<dataset_handler_type>, void, void const>;
using Indptr = std::conditional_t<is_indptr_mutable_v<dataset_handler_type>, Idx, Idx const>;

struct Buffer {
Data* data;
// for uniform buffer, indptr is empty
Expand All @@ -54,37 +72,35 @@ class DatasetHandler {
}

// implicit conversion constructor to const
template <bool indptr_mutable_other>
DatasetHandler(DatasetHandler<true, indptr_mutable_other> const& other)
requires(!data_mutable)
: dataset_info_{other.get_description()} {
template <dataset_handler_tag other_dataset_handler_type>
requires(is_data_mutable_v<other_dataset_handler_type> && !is_data_mutable_v<dataset_handler_type>)
DatasetHandler(DatasetHandler<other_dataset_handler_type> const& other) : dataset_info_{other.get_description()} {
for (Idx i{}; i != other.n_components(); ++i) {
auto const& buffer = other.get_buffer(i);
buffers_.push_back(Buffer{.data = buffer.data, .indptr = buffer.indptr});
}
}

template <bool dataset_const>
std::map<std::string, DataPointer<dataset_const>> export_dataset(Idx scenario = -1) const
requires(dataset_const || data_mutable)
{
template <dataset_type_tag dataset_type>
requires(is_const_dataset_v<dataset_type> || is_data_mutable_v<dataset_handler_type>)
std::map<std::string, DataPointer<dataset_type>> export_dataset(Idx scenario = -1) const {
if (!is_batch() && scenario > 0) {
throw DatasetError{"Cannot export a single dataset with multiple scenarios!\n"};
}
std::map<std::string, DataPointer<dataset_const>> dataset;
std::map<std::string, DataPointer<dataset_type>> dataset;
for (Idx i{}; i != n_components(); ++i) {
ComponentInfo const& component = get_component_info(i);
Buffer const& buffer = get_buffer(i);
if (scenario < 0) {
dataset[component.component->name] = DataPointer<dataset_const>{
dataset[component.component->name] = DataPointer<dataset_type>{
buffer.data, buffer.indptr.data(), batch_size(), component.elements_per_scenario};
} else {
if (component.elements_per_scenario < 0) {
dataset[component.component->name] = DataPointer<dataset_const>{
dataset[component.component->name] = DataPointer<dataset_type>{
component.component->advance_ptr(buffer.data, buffer.indptr[scenario]),
buffer.indptr[scenario + 1] - buffer.indptr[scenario]};
} else {
dataset[component.component->name] = DataPointer<dataset_const>{
dataset[component.component->name] = DataPointer<dataset_type>{
component.component->advance_ptr(buffer.data, component.elements_per_scenario * scenario),
component.elements_per_scenario};
}
Expand Down Expand Up @@ -121,16 +137,16 @@ class DatasetHandler {
}

void add_component_info(std::string_view component, Idx elements_per_scenario, Idx total_elements)
requires indptr_mutable
requires is_indptr_mutable_v<dataset_handler_type>
{
add_component_info_impl(component, elements_per_scenario, total_elements);
}

void add_buffer(std::string_view component, Idx elements_per_scenario, Idx total_elements, Indptr* indptr,
Data* data)
requires(!indptr_mutable)
requires(!is_indptr_mutable_v<dataset_handler_type>)
{
check_non_uniform_integrity<true>(elements_per_scenario, total_elements, indptr);
check_non_uniform_integrity<immutable_t>(elements_per_scenario, total_elements, indptr);
add_component_info_impl(component, elements_per_scenario, total_elements);
buffers_.back().data = data;
if (indptr) {
Expand All @@ -141,11 +157,11 @@ class DatasetHandler {
}

void set_buffer(std::string_view component, Indptr* indptr, Data* data)
requires indptr_mutable
requires is_indptr_mutable_v<dataset_handler_type>
{
Idx const idx = find_component(component, true);
ComponentInfo const& info = dataset_info_.component_info[idx];
check_non_uniform_integrity<false>(info.elements_per_scenario, info.total_elements, indptr);
check_non_uniform_integrity<mutable_t>(info.elements_per_scenario, info.total_elements, indptr);
buffers_[idx].data = data;
if (indptr) {
buffers_[idx].indptr = {indptr, static_cast<size_t>(batch_size() + 1)};
Expand All @@ -165,13 +181,14 @@ class DatasetHandler {
}
}

template <bool check_indptr_content>
template <typename check_indptr_content>
requires std::same_as<check_indptr_content, mutable_t> || std::same_as<check_indptr_content, immutable_t>
void check_non_uniform_integrity(Idx elements_per_scenario, Idx total_elements, Indptr* indptr) {
if (elements_per_scenario < 0) {
if (!indptr) {
throw DatasetError{"For a non-uniform buffer, indptr should be supplied !\n"};
}
if constexpr (check_indptr_content) {
if constexpr (std::same_as<check_indptr_content, immutable_t>) {
if (indptr[0] != 0 || indptr[batch_size()] != total_elements) {
throw DatasetError{
"For a non-uniform buffer, indptr should begin with 0 and end with total_elements !\n"};
Expand All @@ -195,8 +212,8 @@ class DatasetHandler {
}
};

using ConstDatasetHandler = DatasetHandler<false, false>;
using MutableDatasetHandler = DatasetHandler<true, false>;
using WritableDatasetHandler = DatasetHandler<true, true>;
using ConstDatasetHandler = DatasetHandler<const_dataset_t>;
using MutableDatasetHandler = DatasetHandler<mutable_dataset_t>;
using WritableDatasetHandler = DatasetHandler<writable_dataset_t>;

} // namespace power_grid_model::meta_data
Loading

0 comments on commit c537a49

Please sign in to comment.