forked from NREL/ssc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LayoutSimulateThread.cpp
359 lines (301 loc) · 11.3 KB
/
LayoutSimulateThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*******************************************************************************************************
* Copyright 2017 Alliance for Sustainable Energy, LLC
*
* NOTICE: This software was developed at least in part by Alliance for Sustainable Energy, LLC
* (“Alliance”) under Contract No. DE-AC36-08GO28308 with the U.S. Department of Energy and the U.S.
* The Government retains for itself and others acting on its behalf a nonexclusive, paid-up,
* irrevocable worldwide license in the software to reproduce, prepare derivative works, distribute
* copies to the public, perform publicly and display publicly, and to permit others to do so.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, the above government
* rights notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, the above government
* rights notice, this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. The entire corresponding source code of any redistribution, with or without modification, by a
* research entity, including but not limited to any contracting manager/operator of a United States
* National Laboratory, any institution of higher learning, and any non-profit organization, must be
* made publicly available under this license for as long as the redistribution is made available by
* the research entity.
*
* 4. Redistribution of this software, without modification, must refer to the software by the same
* designation. Redistribution of a modified version of this software (i) may not refer to the modified
* version by the same designation, or by any confusingly similar designation, and (ii) must refer to
* the underlying software originally provided by Alliance as “System Advisor Model” or “SAM”. Except
* to comply with the foregoing, the terms “System Advisor Model”, “SAM”, or any confusingly similar
* designation may not be used to refer to any modified version of this software or any modified
* version of the underlying software originally provided by Alliance without the prior written consent
* of Alliance.
*
* 5. The name of the copyright holder, contributors, the United States Government, the United States
* Department of Energy, or any of their employees may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER,
* CONTRIBUTORS, UNITED STATES GOVERNMENT OR UNITED STATES DEPARTMENT OF ENERGY, NOR ANY OF THEIR
* EMPLOYEES, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
#include "LayoutSimulateThread.h"
#include "SolarField.h"
#ifdef SP_USE_THREADS
using namespace std;
void LayoutSimThread::Setup(string &tname, SolarField *SF, sim_results *results, WeatherData *wdata,
int sim_first, int sim_last, bool is_shadow_detail, bool is_flux_detail)
{
/*
Assign all of the arguments to local memory
*/
_thread_id = tname;
_SF = SF;
_results = results;
_wdata = wdata;
_sol_azzen = 0;
_sim_first = sim_first;
_sim_last = sim_last;
Finished = false;
CancelFlag = false;
Nsim_complete = 0;
Nsim_total = _sim_last - _sim_first;
_is_user_sun_pos = false;
_is_shadow_detail = is_shadow_detail;
_is_flux_detail = is_flux_detail;
_is_flux_normalized = true;
};
void LayoutSimThread::Setup(string &tname, SolarField *SF, sim_results *results, matrix_t<double> *sol_azzen,
sim_params &simpars, int sim_first, int sim_last, bool is_shadow_detail, bool is_flux_detail)
{
/*
overload to allow specification of simulation sun positions.
Sun positions provided in a matrix_t
| Azimuth | Elevation
Row | (rad) | (rad)
--------------------------------
1 | az1 | el1
2 | az2 | el2
...
Args:
args[0] | DNI | W/m2
args[1] | Tdb | C
args[2] | Vwind | m/s
args[3] | Pres | bar
*/
_thread_id = tname;
_SF = SF;
_results = results;
_wdata = 0;
_sol_azzen = sol_azzen;
_sim_first = sim_first;
_sim_last = sim_last;
Finished = false;
CancelFlag = false;
Nsim_complete = 0;
Nsim_total = _sim_last - _sim_first;
_is_user_sun_pos = true;
_sim_params = simpars;
_is_shadow_detail = is_shadow_detail;
_is_flux_detail = is_flux_detail;
_is_flux_normalized = true;
};
void LayoutSimThread::IsFluxmapNormalized(bool is_normal)
{
_is_flux_normalized = is_normal;
}
void LayoutSimThread::CancelSimulation()
{
CancelLock.lock();
CancelFlag = true;
CancelLock.unlock();
}
bool LayoutSimThread::IsSimulationCancelled()
{
bool r;
CancelLock.lock();
r = CancelFlag;
CancelLock.unlock();
return r;
}
bool LayoutSimThread::IsFinished()
{
bool f;
FinishedLock.lock();
f = Finished;
FinishedLock.unlock();
return f;
}
bool LayoutSimThread::IsFinishedWithErrors()
{
bool f;
FinErrLock.lock();
f = FinishedWithErrors;
FinErrLock.unlock();
return f;
}
void LayoutSimThread::UpdateStatus(int nsim_complete, int nsim_total)
{
StatusLock.lock();
Nsim_complete = nsim_complete;
Nsim_total = nsim_total;
StatusLock.unlock();
}
void LayoutSimThread::GetStatus( int *nsim_complete, int *nsim_total)
{
StatusLock.lock();
*nsim_complete = this->Nsim_complete;
*nsim_total = this->Nsim_total;
StatusLock.unlock();
}
vector<string> *LayoutSimThread::GetSimMessages()
{
return &_sim_messages;
}
void LayoutSimThread::StartThread() //Entry()
{
/*
This method duplicates the functionality of SolarField::LayoutSimulate(...)
This method is intended to be thread safe and can be called by the GUI directly. Each thread must have
its own instance of _SF. Before running multiple threads, create a solar field object, prepare it with
PrepareFieldLayout(...), and use the deep copy constructor in SolarField to create as many duplicate
objects as there are threads. Call this method for each duplicate object.
*/
try{
FinErrLock.lock();
FinishedWithErrors = false;
FinErrLock.unlock();
_sim_messages.clear();
double pi = PI;
//Run the simulation
double dom, doy, hour, month;
double az, zen;
bool is_pmt_factors = _SF->getVarMap()->fin.is_pmt_factors.val;
vector<double> *tous = &_SF->getVarMap()->fin.pricing_array.Val();
int Npos = (int)_SF->getHeliostats()->size();
//Simulate for each time
StatusLock.lock();
bool is_cancel = this->CancelFlag; //check for cancelled simulation
StatusLock.unlock();
if(is_cancel){
FinishedLock.lock();
Finished = true;
FinishedLock.unlock();
return; // (wxThread::ExitCode)-1;
}
if(_sim_first < 0) _sim_first = 0;
if(_sim_last < 0) _sim_last = _wdata->size();
int nsim = _sim_last - _sim_first + 1;
for(int i=_sim_first; i<_sim_last; i++){
//_SF->getSimInfoObject()->setCurrentSimulation(i+1);
//double args[5];
sim_params P;
DateTime DT;
//either calculate the sun position based on weather data steps, or use user-defined values
if(! _is_user_sun_pos){
//---- Calculate sun positions
//Get the design-point day, hour, and DNI
_wdata->getStep(i, dom, hour, month, P.dni, P.Tamb, P.Patm, P.Vwind, P.Simweight);
P.Patm*=.001;
//Convert the day of the month to a day of year
doy = DT.GetDayOfYear(2011,int(month),int(dom));
//Calculate the sun position
Ambient::setDateTime(DT, hour, doy);
if(is_pmt_factors)
P.TOUweight = tous->at(DT.GetHourOfYear());
//latitude, longitude, and elevation should be set in the input file
Ambient::calcSunPosition(*_SF->getVarMap(), DT, &az, &zen, true );
//If the sun is not above the horizon, don't continue
if( zen > 90. )
continue;
az *= D2R;
zen *= D2R;
}
else{
//set the user-specified values
az = _sol_azzen->at(i,0);
zen = _sol_azzen->at(i,1);
P = _sim_params;
}
bool is_cancel;
StatusLock.lock();
is_cancel = this->CancelFlag;
StatusLock.unlock();
/*if( (_is_shadow_detail || _is_flux_detail ) && !is_cancel)
interop::AimpointUpdateHandler(*_SF);
StatusLock.lock();
is_cancel = this->CancelFlag;
StatusLock.unlock();*/
P.is_layout = !_is_shadow_detail;
if(! is_cancel)
_SF->Simulate(az, zen, P);
if((! is_cancel) && _is_flux_detail)
_SF->HermiteFluxSimulation( *_SF->getHeliostats() );
StatusLock.lock();
is_cancel = this->CancelFlag;
StatusLock.unlock();
//store the _results
double azzen[] = {az, zen};
if(! is_cancel)
_results->at(i).process_analytical_simulation(*_SF, _is_flux_detail ? 2 : 0, azzen); //2);
StatusLock.lock();
is_cancel = this->CancelFlag;
StatusLock.unlock();
//optionally post-process the flux results as well
if(_is_flux_detail && !is_cancel)
_results->at(i).process_flux(_SF, _is_flux_normalized);
//Update progress
UpdateStatus(i-_sim_first+1,nsim);
//Check for user cancel
StatusLock.lock();
is_cancel = this->CancelFlag;
StatusLock.unlock();
if(is_cancel){
FinishedLock.lock();
Finished = true;
FinishedLock.unlock();
return;
}
}
FinishedLock.lock();
Finished = true;
FinishedLock.unlock();
}
catch(spexception &e)
{
/* Handle exceptions within a thread by adding the exception to a list and returning normally */
StatusLock.lock();
this->CancelFlag = true;
StatusLock.unlock();
FinishedLock.lock();
Finished = true;
FinishedLock.unlock();
FinErrLock.lock();
FinishedWithErrors = true;
FinErrLock.unlock();
_sim_messages.push_back( "Thread " + this->_thread_id + ": " + e.what() );
}
catch(...)
{
/* Handle exceptions within a thread by adding the exception to a list and returning normally */
StatusLock.lock();
this->CancelFlag = true;
StatusLock.unlock();
FinishedLock.lock();
Finished = true;
FinishedLock.unlock();
FinErrLock.lock();
FinishedWithErrors = true;
FinErrLock.unlock();
_sim_messages.push_back( "Thread " + this->_thread_id + ": " + "Caught unspecified error in a simulation thread. Simulation was not successful." );
}
return;
};
#endif // SP_USE_THREADS