NANDRAD Data Model Library  Version 2.0
NANDRAD
NANDRAD_LinearSplineParameter.h
1 /* The NANDRAD data model library.
2 
3  Copyright (c) 2012-today, Institut für Bauklimatik, TU Dresden, Germany
4 
5  Primary authors:
6  Andreas Nicolai <andreas.nicolai -[at]- tu-dresden.de>
7  Anne Paepcke <anne.paepcke -[at]- tu-dresden.de>
8 
9  This library is part of SIM-VICUS (https://github.com/ghorwin/SIM-VICUS)
10 
11  This library is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 */
21 
22 #ifndef NANDRAD_LinearSplineParameterH
23 #define NANDRAD_LinearSplineParameterH
24 
25 #include <string>
26 
27 #include <IBK_LinearSpline.h>
28 #include <IBK_Unit.h>
29 #include <IBK_Path.h>
30 
31 #include "NANDRAD_CodeGenMacros.h"
32 
33 namespace NANDRAD {
34 
35 /*! Class LinearSplineParameter stores a linear spline curve, the corresponding parameter name
36  and a unit name of both dependend and independend quantity (xUnit, yUnit).
37 
38  Note that the read and write functions perform unit conversions on the values, so that
39  the linear spline actually holds values always in Base-SI units (according to the
40  IBK Unit-Definition).
41 
42  So when reading a linear spline that defines temperatures in C, the actual spline will
43  then contain the values in K.
44 */
46 public:
47 
48  /*! Interpolation method to be used for this linear spline parameter. */
50  I_CONSTANT, // Keyword: constant
51  I_LINEAR, // Keyword: linear
52  NUM_I
53  };
54 
55  /*! How to treat the values in multi-year simulations. */
56  enum wrapMethod_t {
57  C_CONTINUOUS, // Keyword: continuous 'Continuous data'
58  C_CYCLIC, // Keyword: cyclic 'Annual cycle'
59  NUM_C
60  };
61 
62  // *** PUBLIC MEMBER FUNCTIONS ***
63 
64  /*! Default Constructor. */
66 
67  /*! Constructor with values. */
68  LinearSplineParameter(const std::string &name,
69  const interpolationMethod_t intPol,
70  const std::vector<double> &xVals,const std::vector<double> yVals,
71  const IBK::Unit &xUnit, const IBK::Unit &yUnit):
72  m_name(name),
73  m_interpolationMethod(intPol),
74  m_xUnit(xUnit),
75  m_yUnit(yUnit)
76  {
77  m_values.setValues(xVals, yVals);
78  }
79 
80  /*! Constructor with data file . */
81  LinearSplineParameter(const std::string &name,
82  const interpolationMethod_t intPol,
83  const IBK::Path &tsvFilePath):
84  m_name(name),
85  m_interpolationMethod(intPol),
86  m_tsvFile(tsvFilePath)
87  {
88  }
89 
90  void readXML(const TiXmlElement * element);
91  TiXmlElement * writeXML(TiXmlElement * parent) const;
92 
93  NANDRAD_COMP(LinearSplineParameter)
94 
95  /*! Function to check for correct parametrization of linear spline parameters.
96  This function checks if the spline is actually defined, i.e. m_name is not empty.
97  Then it tests, wether there is a valid tsv-filepath given, reads the according file and sets m_values and the units
98  Then it is checked if the x and y value units can be converted to targetX and targetY units. This can be skipped by
99  setting skipUnitChecks to true, which may be useful if the target units are not yet known.
100  These target units must be SI base units.
101  It then converts the input data into the target (base SI) units, using the convert2BaseUnits() function.
102 
103  Note: the m_xUnit and m_yUnit members are not modified here!
104  Then, the spline is generated, hereby testing for monotonically increasing x-values.
105  Finally, the y-value ranges are being checked.
106 
107  \note To ensure consistent object state, the x and y units are updated after the value conversion. Hence,
108  if you need m_xUnit and m_yUnit to represent input/output units, do not call this function!
109  */
110  void checkAndInitialize(const std::string & expectedName,
111  const IBK::Unit & targetXUnit, const IBK::Unit & targetYUnit,
112  const IBK::Unit & limitYUnit, double minYVal, bool isGreaterEqual,
113  double maxYVal, bool isLessEqual, const char * const errmsg, bool skipUnitChecks=false);
114 
115  /*! Reads externally referenced tsv-file.
116  m_tsvFile is expected to contain a valid absolute path, optionally with trailing ?n column identifier.
117  */
118  void readTsv();
119 
120  // *** PUBLIC MEMBER VARIABLES ***
121 
122  /*! Parameter name (in context of schedules used as scheduled quantity). */
123  std::string m_name;
124  /*! Interpolation method to be used when computing values of this spline. */
126  /*! Whether to wrap time around in multi-year simulations (cyclic use) or to assume continuous data. */
128  /*! Data vectors including linear spline functionality (i.e. interpolation at any given value).
129  When this class is used as data container, the x and y values are stored in the units m_xUnit and m_yUnit.
130  However, after the call to checkAndInitialize(), the values are stored in the respective Base-SI units
131  of the input/output units m_xUnit and m_yUnit. For example, if m_yUnit is 'C' (degree C), then the spline
132  holds values in 'K' (degree Kelvin).
133  \warning Do not call checkAndInitialize() on original data containers - always create a copy first!
134  */
135  IBK::LinearSpline m_values;
136  /*! Unit of the x-values. */
137  IBK::Unit m_xUnit;
138  /*! Unit of the y-values. */
139  IBK::Unit m_yUnit;
140  /*! Path to tsv-file, the file will be read during checkAndInitialize() call.
141  Path may have an appended column identifier, starting with 1 for the first data column after the time column.
142  */
143  IBK::Path m_tsvFile;
144 
145 
146 private:
147  /*! Converts x and y values from display/input/output units (m_xUnit and m_yUnit) to their respective
148  base SI units.
149 
150  \warning The m_xUnit and m_yUnit will not be changed and thus be inconsistent afterwards with
151  the values in the m_values spline (similarly to IBK::Parameter).
152  */
153  void convert2BaseUnits();
154 
155 };
156 
157 } // namespace NANDRAD
158 
159 #endif // NANDRAD_LinearSplineParameterH
IBK::Unit m_yUnit
Unit of the y-values.
wrapMethod_t
How to treat the values in multi-year simulations.
IBK::Unit m_xUnit
Unit of the x-values.
LinearSplineParameter(const std::string &name, const interpolationMethod_t intPol, const IBK::Path &tsvFilePath)
Constructor with data file .
IBK::Path m_tsvFile
Path to tsv-file, the file will be read during checkAndInitialize() call.
void checkAndInitialize(const std::string &expectedName, const IBK::Unit &targetXUnit, const IBK::Unit &targetYUnit, const IBK::Unit &limitYUnit, double minYVal, bool isGreaterEqual, double maxYVal, bool isLessEqual, const char *const errmsg, bool skipUnitChecks=false)
Function to check for correct parametrization of linear spline parameters.
Class LinearSplineParameter stores a linear spline curve, the corresponding parameter name and a unit...
std::string m_name
Parameter name (in context of schedules used as scheduled quantity).
void readTsv()
Reads externally referenced tsv-file.
interpolationMethod_t m_interpolationMethod
Interpolation method to be used when computing values of this spline.
wrapMethod_t m_wrapMethod
Whether to wrap time around in multi-year simulations (cyclic use) or to assume continuous data...
IBK::LinearSpline m_values
Data vectors including linear spline functionality (i.e.
interpolationMethod_t
Interpolation method to be used for this linear spline parameter.
LinearSplineParameter(const std::string &name, const interpolationMethod_t intPol, const std::vector< double > &xVals, const std::vector< double > yVals, const IBK::Unit &xUnit, const IBK::Unit &yUnit)
Constructor with values.
The namespace NANDRAD contains the data model classes that make up the NANDRAD solver input data...
void convert2BaseUnits()
Converts x and y values from display/input/output units (m_xUnit and m_yUnit) to their respective bas...