1.3.49
 
Loading...
Searching...
No Matches
InitializeSimulation.cpp
1#include "InitializeSimulation.h"
2
3using namespace helios;
4
5void InitializeSimulation(const std::string &xml_input_file, helios::Context *context_ptr) {
6
7 pugi::xml_document xmldoc;
8
9 std::string xml_error_string;
10 if (!open_xml_file(xml_input_file, xmldoc, xml_error_string)) {
11 helios_runtime_error(xml_error_string);
12 }
13
14 pugi::xml_node helios = xmldoc.child("helios");
15 pugi::xml_node node;
16
17 // ####### SET UP THE LOCATION ####### //
18
19 Location location;
20
21 float latitude;
22 node = helios.child("latitude");
23 if (node.empty()) {
24 std::cout << "WARNING: No value given for 'latitude'. Using default value of " << location.latitude_deg << std::endl;
25 } else {
26
27 const char *latitude_str = node.child_value();
28 if (!parse_float(latitude_str, latitude)) {
29 helios_runtime_error("ERROR: Value given for 'latitude' could not be parsed.");
30 } else {
31 location.latitude_deg = latitude;
32 }
33 }
34
35 float longitude;
36 node = helios.child("longitude");
37 if (node.empty()) {
38 std::cout << "WARNING: No value given for 'longitude'. Using default value of " << location.longitude_deg << std::endl;
39 } else {
40
41 const char *longitude_str = node.child_value();
42 if (!parse_float(longitude_str, longitude)) {
43 helios_runtime_error("ERROR: Value given for 'longitude' could not be parsed.");
44 } else {
45 location.longitude_deg = longitude;
46 }
47 }
48
49 float UTC_offset;
50 node = helios.child("UTC_offset");
51 if (node.empty()) {
52 std::cout << "WARNING: No value given for 'UTC_offset'. Using default value of " << location.UTC_offset << std::endl;
53 } else {
54
55 const char *UTC_offset_str = node.child_value();
56 if (!parse_float(UTC_offset_str, UTC_offset)) {
57 helios_runtime_error("ERROR: Value given for 'UTC_offset' could not be parsed.");
58 } else {
59 location.UTC_offset = UTC_offset;
60 }
61 }
62
63 context_ptr->setLocation(location);
64
65 /*
66
67 // ####### BUILDING THE DATE AND TIME ####### //
68
69 Date date;
70
71 int3 calendar_date;
72 bool calendar_date_read = false;
73 node = helios.child("calendar_date");
74 if( node.empty() ){
75 std::cout << "WARNING: No value given for 'calendar_date'. Using default value of " << date << std::endl;
76 }else {
77
78 const char *calendar_date_str = node.child_value();
79 if (!parse_int3(calendar_date_str, calendar_date)) {
80 helios_runtime_error("ERROR: Value given for 'calendar_date' could not be parsed.");
81 }else{
82 if( calendar_date.x < 1 || calendar_date.x > 31 || calendar_date.y < 1 || calendar_date.y > 12 || calendar_date.z < 1000 ) {
83 helios_runtime_error("ERROR: Value given for 'calendar_date' is out of range.");
84 }
85 date.day = calendar_date.x;
86 date.month = calendar_date.y;
87 date.year = calendar_date.z;
88 calendar_date_read = true;
89 }
90
91 }
92
93 int2 julian_date;
94 bool julian_date_read = false;
95 node = helios.child("julian_date");
96 if( node.empty() ){
97 std::cout << "WARNING: No value given for 'julian_date'. Using default value of <" << date.JulianDay() << "," << date.year << ">" << std::endl;
98 }else {
99
100 const char *julian_date_str = node.child_value();
101 if (!parse_int2(julian_date_str, julian_date)) {
102 helios_runtime_error("ERROR: Value given for 'julian_date' could not be parsed.");
103 }else{
104 if( julian_date.x < 1 || julian_date.x > 366 || julian_date.y < 1000 ) {
105 helios_runtime_error("ERROR: Value given for 'julian_date' is out of range.");
106 }
107 date = make_Date(julian_date.x,julian_date.y);
108 julian_date_read = true;
109 }
110
111 }
112
113 if( calendar_date_read && julian_date_read ) {
114 std::cout << "WARNING: Both 'calendar_date' and 'julian_date' were given. Using the Juilian date provided." << std::endl;
115 }
116
117 context_ptr->setDate(date);
118
119 Time time;
120
121 int3 time3;
122 bool time_read = false;
123 node = helios.child("time");
124 if( node.empty() ){
125 std::cout << "WARNING: No value given for 'time'. Using default value of " << time << std::endl;
126 }else {
127
128 const char *time_str = node.child_value();
129 if (!parse_int3(time_str, time3)) {
130 helios_runtime_error("ERROR: Value given for 'time' could not be parsed.");
131 }else{
132 if( time3.x < 0 || time3.x > 59 || time3.y < 0 || time3.y > 59 || time3.z < 0 || time3.z > 23 ) {
133 helios_runtime_error("ERROR: Value given for 'time' is out of range.");
134 }
135 time.second = time3.x;
136 time.minute = time3.y;
137 time.hour = time3.z;
138 time_read = true;
139 }
140
141 }
142
143 context_ptr->setTime(time);
144
145 */
146
147 // ####### READ IN WEATHER DATA ####### //
148
149 std::string weather_data_file;
150 node = helios.child("csv_weather_file");
151 if (!node.empty()) {
152
153 const char *weather_data_file_str = node.child_value();
154 weather_data_file = trim_whitespace(std::string(weather_data_file_str));
155
156 if (weather_data_file.empty()) {
157 helios_runtime_error("ERROR: Value given for 'weather_data_file' is empty.");
158 } else if (!std::filesystem::exists(weather_data_file)) {
159 helios_runtime_error("ERROR: File given for 'weather_data_file' does not exist.");
160 }
161
162 context_ptr->loadTabularTimeseriesData(weather_data_file, {}, ",", "YYYYMMDD", 1);
163 }
164
165 std::string cimis_data_file;
166 node = helios.child("cimis_weather_file");
167 if (!node.empty()) {
168
169 if (!weather_data_file.empty()) {
170 std::cout << "WARNING: Both 'csv_weather_file' and 'cimis_weather_file' were given, but only one weather data file can be loaded for the simulation. Using the CSV weather data file." << std::endl;
171 } else {
172
173 const char *cimis_data_file_str = node.child_value();
174 cimis_data_file = trim_whitespace(std::string(cimis_data_file_str));
175
176 if (cimis_data_file.empty()) {
177 helios_runtime_error("ERROR: Value given for 'cimis_data_file' is empty.");
178 } else if (!std::filesystem::exists(cimis_data_file)) {
179 helios_runtime_error("ERROR: File given for 'cimis_data_file' does not exist.");
180 }
181
182 context_ptr->loadTabularTimeseriesData(cimis_data_file, {"CIMIS"}, ",");
183 }
184 }
185}