Skip to content

Commit 1fc1722

Browse files
Merge pull request #579 from PowerGridModel/feature/use-nan-in-struct
Use NaN as default value in input/output/update struct
2 parents 887cf99 + 9b50bc5 commit 1fc1722

File tree

19 files changed

+399
-1255
lines changed

19 files changed

+399
-1255
lines changed

code_generation/code_gen.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
JINJA_ENV = Environment(loader=FileSystemLoader(TEMPLATE_DIR))
1717

1818

19+
def _data_type_nan(data_type: str):
20+
if data_type == "ID":
21+
return "na_IntID"
22+
elif data_type == "double" or "RealValue" in data_type:
23+
return "nan"
24+
elif data_type == "IntS":
25+
return "na_IntS"
26+
else:
27+
return f"static_cast<{data_type}>(na_IntS)"
28+
29+
1930
class CodeGenerator:
2031
all_classes: Dict[str, AttributeClass]
2132
base_output_path: Path
@@ -54,6 +65,7 @@ def render_attribute_classes(self, template_path: Path, data_path: Path, output_
5465
attribute_class.specification_names = [attribute_class.name]
5566
new_attribute_list = []
5667
for attribute in attribute_class.attributes:
68+
attribute.nan_value = _data_type_nan(attribute.data_type)
5769
if isinstance(attribute.names, str):
5870
new_attribute_list.append(attribute)
5971
else:

code_generation/meta_data.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# define dataclass for meta data
66

77
from dataclasses import dataclass
8-
from typing import List, Optional, Union
8+
from typing import Dict, List, Optional, Union
99

1010
from dataclasses_json import DataClassJsonMixin
1111

@@ -15,6 +15,7 @@ class Attribute(DataClassJsonMixin):
1515
data_type: str
1616
names: Union[str, List[str]]
1717
description: str
18+
nan_value: Optional[str] = None
1819

1920

2021
@dataclass
@@ -25,6 +26,8 @@ class AttributeClass(DataClassJsonMixin):
2526
base: Optional[str] = None
2627
is_template: bool = False
2728
full_name: Optional[str] = None
29+
specification_names: Optional[List[str]] = None
30+
base_attributes: Optional[Dict[str, List[Attribute]]] = None
2831

2932

3033
@dataclass

code_generation/templates/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/attribute_classes.hpp.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct {{ attribute_class.name }} {
2626
{%- endif -%}
2727

2828
{%- for attribute in attribute_class.full_attributes %}
29-
{{ attribute.data_type }} {{ attribute.names }};
29+
{{ attribute.data_type }} {{ attribute.names }}{ {{- attribute.nan_value -}} };
3030
{%- if attribute.description %} // {{ attribute.description }}{%- endif %}
3131
{%- endfor %}
3232
{% for base_class in attribute_class.base_attributes %}

code_generation/templates/power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/meta_gen/attribute_classes.hpp.jinja

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,6 @@ struct get_attributes_list<{{ attribute_class.name }}> {
4141

4242
{% endfor %}
4343

44-
// template specialization functors to get nan
45-
46-
{% for attribute_class in classes -%}
47-
48-
{%- if attribute_class.is_template -%}
49-
template <symmetry_tag sym_type>
50-
struct get_component_nan<{{ attribute_class.name }}<sym_type>> {
51-
using sym = sym_type;
52-
{% else -%}
53-
template<>
54-
struct get_component_nan<{{ attribute_class.name }}> {
55-
{%- endif %}
56-
{{ attribute_class.full_name }} operator() () const {
57-
{{ attribute_class.full_name }} comp;
58-
// all attributes including base class
59-
{% for attribute in attribute_class.full_attributes %}
60-
set_nan(comp.{{ attribute.names }});
61-
{%- endfor %}
62-
return comp;
63-
}
64-
};
65-
66-
{% endfor %}
6744

6845
} // namespace power_grid_model::meta_data
6946

power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/input.hpp

Lines changed: 181 additions & 181 deletions
Large diffs are not rendered by default.

power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/meta_data.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ template <class StructType, class ValueType> struct trait_pointer_to_member<Valu
2727
// which is a std::array
2828
// the specializations are automatically generated
2929
template <class T> struct get_attributes_list;
30-
// primary template functor classes to generate nan value for a component
31-
// the specializations will contain operator() to return a component instance with NaNs
32-
// the specializations are automatically generated
33-
template <class T> struct get_component_nan;
3430

3531
// ctype string
3632
template <class T> struct ctype_t;

power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/meta_gen/gen_getters.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ template <class StructType, auto component_name_getter> struct get_meta_componen
6161
.attributes = get_attributes_list<StructType>::value,
6262
.set_nan =
6363
[](RawDataPtr buffer_ptr, Idx pos, Idx size) {
64-
static StructType const nan_value = get_component_nan<StructType>{}();
6564
auto ptr = reinterpret_cast<StructType*>(buffer_ptr);
66-
std::fill(ptr + pos, ptr + pos + size, nan_value);
65+
std::fill(ptr + pos, ptr + pos + size, StructType{});
6766
},
6867
.create_buffer = [](Idx size) -> RawDataPtr { return new StructType[size]; },
6968
.destroy_buffer = [](RawDataConstPtr buffer_ptr) { delete[] reinterpret_cast<StructType const*>(buffer_ptr); },

0 commit comments

Comments
 (0)