-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathptesdesignptdialog.cpp
More file actions
900 lines (761 loc) · 26.7 KB
/
ptesdesignptdialog.cpp
File metadata and controls
900 lines (761 loc) · 26.7 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
BSD 3-Clause License
Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NatLabRockies/SAM/blob/develop/LICENSE
All rights reserved.
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, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may 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 OR CONTRIBUTORS 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 <wx/sizer.h>
#include <wx/checklst.h>
#include <wx/checkbox.h>
#include <wx/spinctrl.h>
#include <wex/metro.h>
#include "ptesdesignptdialog.h"
#include "main.h"
#include "script.h"
#include <ssc/sscapi.h>
#include <vector>
#include <functional>
#include <sstream>
using std::vector;
using std::string;
BEGIN_EVENT_TABLE(PTESDesignPtDialog, wxDialog)
EVT_BUTTON(wxID_OK, PTESDesignPtDialog::OnEvt)
EVT_BUTTON(wxID_HELP, PTESDesignPtDialog::OnEvt)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(PTESDesignPtDialog::ConfirmDlg, wxDialog)
EVT_BUTTON(wxID_OK, PTESDesignPtDialog::ConfirmDlg::OnEvt)
END_EVENT_TABLE()
// ----------------------------- VarModel
/// <summary>
/// Default Constructor
/// </summary>
PTESDesignPtDialog::VarModel::VarModel(string var_name, string display_name, string description, double default_value) :
kVarName(var_name),
kDisplayName(display_name),
kDescription(description),
default_value_(default_value)
{
}
/// <summary>
/// Get Double from TextCtrl
/// </summary>
/// <param name="flag">success flag</param>
/// <returns></returns>
double PTESDesignPtDialog::VarModel::GetValue(bool& flag)
{
if (this->txt_ctrl_ == nullptr)
{
flag = true;
return this->default_value_;
}
// Parse String
std::stringstream s(this->txt_ctrl_->GetValue().ToStdString());
double d = 0;
s >> d;
if (s.fail())
{
flag = false;
return 0;
}
else
{
flag = true;
return d;
}
}
/// <summary>
/// Set Variable Model's text value
/// </summary>
/// <param name="val"></param>
void PTESDesignPtDialog::VarModel::SetTextValue(string val)
{
this->txt_ctrl_->SetValue(val);
}
/// <summary>
/// Set Variable to be Ready Only on GUI
/// </summary>
/// <param name="flag"></param>
void PTESDesignPtDialog::VarModel::SetReadonly(bool flag)
{
this->txt_ctrl_->SetEditable(!flag);
}
/// <summary>
/// Set Whether Variable Text Control is Displayed
/// Text Control must already be defined
/// </summary>
/// <param name="flag"></param>
void PTESDesignPtDialog::VarModel::SetVisible(bool flag)
{
if (flag)
this->txt_ctrl_->Show();
else
this->txt_ctrl_->Hide();
}
// ---------------------------- Fluid Var Model
/// <summary>
/// Default Constructor
/// </summary>
/// <param name="name">Display Name of Fluid</param>
/// <param name="type">Type of Fluid (WF, HF, CF)</param>
PTESDesignPtDialog::FluidVarModel::FluidVarModel(string name, FluidType type)
:
kVarName(name),
kType(type)
{
SetFluidTypeString(type);
}
/// <summary>
/// Get Fluid Material Options
/// </summary>
/// <returns></returns>
vector<string> PTESDesignPtDialog::FluidVarModel::GetFluidMaterials()
{
vector<string> fluid_types;
switch (this->kType)
{
case(kWF):
{
fluid_types.push_back("Air");
fluid_types.push_back("Nitrogen");
fluid_types.push_back("Argon");
fluid_types.push_back("Hydrogen");
fluid_types.push_back("Helium");
break;
}
case(kHF):
{
fluid_types.push_back("NitrateSalt");
fluid_types.push_back("ChlorideSalt");
break;
}
case(kCF):
{
fluid_types.push_back("Glycol");
fluid_types.push_back("Methanol");
}
}
return fluid_types;
}
/// <summary>
/// Get User Selected Material from wxChoice
/// </summary>
/// <returns></returns>
string PTESDesignPtDialog::FluidVarModel::GetSelectedMaterial()
{
if (this->choice_ != nullptr)
{
return choice_->GetStringSelection().ToStdString();
}
}
/// <summary>
/// Set Type of Fluid (WF, HF, CF)
/// </summary>
/// <param name="type"></param>
void PTESDesignPtDialog::FluidVarModel::SetFluidTypeString(FluidType type)
{
switch (type)
{
case (kWF):
{
this->fluid_type_string_ = "Working Fluid";
break;
}
case (kHF):
{
this->fluid_type_string_ = "Hot Fluid";
break;
}
case (kCF):
{
this->fluid_type_string_ = "Cold Fluid";
break;
}
}
}
// --------------------------- Confirm Dialog
/// <summary>
/// Constructor for dialog that displays results and asks to confirm them
/// </summary>
/// <param name="parent"></param>
/// <param name="title"></param>
/// <param name="cmod_io"></param>
/// <param name="margin"></param>
PTESDesignPtDialog::ConfirmDlg::ConfirmDlg(wxWindow* parent, const wxString& title, vector<CmodIOModel> cmod_io, double margin)
:
wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxMINIMIZE_BOX | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
kMargin(margin)
{
// Initialize
result_code_ = -1;
this->SetBackgroundColour(*wxWHITE);
// Populate Dialog
wxBoxSizer* window_szr = new wxBoxSizer(wxVERTICAL);
wxStaticText* header_text = new wxStaticText(this, wxID_ANY, "Accept Design Point Results?");
header_text->SetFont(wxMetroTheme::Font(wxMT_LIGHT, 17));
window_szr->Add(header_text, 0, wxALIGN_LEFT | wxALL, kMargin * 2);
wxFlexGridSizer* flx = new wxFlexGridSizer(2);
flx->SetNonFlexibleGrowMode(wxFLEX_GROWMODE_SPECIFIED);
bool is_left = true;
int index = 0;
// Loop through Result Data
int size = cmod_io.size();
for (int i = 0; i < size; i += 2)
{
// Left Column
wxTextCtrl* val_label_left;
{
CmodIOModel& io = cmod_io[i];
string name = io.name_;
string desc = io.label_;
string unit = io.unit_;
string label_text = desc;
if (unit != "")
label_text += " (" + unit + ")";
double value = io.val_num_;
wxStaticText* name_label_left = new wxStaticText(this, wxID_ANY, label_text, wxDefaultPosition, wxDefaultSize);
name_label_left->Wrap(175);
val_label_left = new wxTextCtrl(this, wxID_ANY, std::to_string(value), wxDefaultPosition, wxSize(150, -1));
val_label_left->SetEditable(false);
val_label_left->SetForegroundColour(UIColorCalculatedFore);
val_label_left->SetBackgroundColour(UIColorCalculatedBack);
// Add Left Label
flx->Add(name_label_left, 1, wxALIGN_LEFT | wxRIGHT | wxBOTTOM, margin);
}
// Right Column
wxTextCtrl* val_label_right;
if (i < cmod_io.size() - 1)
{
CmodIOModel& io_right = cmod_io[i + 1];
string name_right = io_right.name_;
string desc_right = io_right.label_;
string unit_right = io_right.unit_;
string label_text_right = desc_right;
if (unit_right != "")
label_text_right += " (" + unit_right + ")";
double value_right = io_right.val_num_;
wxStaticText* name_label_right = new wxStaticText(this, wxID_ANY, label_text_right, wxDefaultPosition, wxDefaultSize);
name_label_right->Wrap(175);
val_label_right = new wxTextCtrl(this, wxID_ANY, std::to_string(value_right), wxDefaultPosition, wxSize(150, -1));
val_label_right->SetEditable(false);
val_label_right->SetForegroundColour(UIColorCalculatedFore);
val_label_right->SetBackgroundColour(UIColorCalculatedBack);
// Add Right Label
flx->Add(name_label_right, 1, wxALIGN_LEFT | wxLEFT | wxBOTTOM, margin);
}
else
{
// Add filler label
wxStaticText* filler = new wxStaticText(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize);
flx->Add(filler, 1, wxALIGN_LEFT | wxLEFT | wxBOTTOM, margin);
}
// Add Left Value
flx->Add(val_label_left, 0, wxALIGN_LEFT | wxRIGHT | wxBOTTOM, margin);
// Add Right Value (if necessary)
if (i < cmod_io.size() - 1)
flx->Add(val_label_right, 0, wxALIGN_LEFT | wxLEFT | wxBOTTOM, margin);
// Make dummy panels for spacing
for (int i : {0, 1})
{
wxPanel* dummy_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(0, 0));
flx->Add(dummy_panel, 0, wxBOTTOM, margin * 2);
}
}
window_szr->Add(flx, 0, wxALL, kMargin * 4);
window_szr->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, kMargin * 4);
this->SetSizer(window_szr);
Fit();
}
/// <summary>
/// Catch Confirm Dialog Btn Events
/// </summary>
/// <param name="e"></param>
void PTESDesignPtDialog::ConfirmDlg::OnEvt(wxCommandEvent& e)
{
switch (e.GetId())
{
case wxID_OK:
{
int x = 0;
result_code_ = 0;
EndModal(wxID_OK);
}
}
}
// --------------------------- PTES Design Point Dialog
/// <summary>
/// Construct PTESDesignPtDialog without cxt
/// </summary>
/// <param name="parent"></param>
/// <param name="title"></param>
PTESDesignPtDialog::PTESDesignPtDialog(wxWindow* parent, const wxString& title)
:
wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxMINIMIZE_BOX | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
kMargin(5),
kTxtCtrlHeight(-1),
kTxtCtrlWidth(150),
working_fluid_("working_fluid_type", FluidVarModel::kWF)
//hot_fluid_("hot_fluid_type", FluidVarModel::kHF),
//cold_fluid_("cold_fluid_type", FluidVarModel::kCF)
{
// Initialize
result_code_ = -1;
// Generate Component Variables
{
component_var_vec_.push_back(VarModel("hx_eff", "HX Eff", "Heat Exchanger Effectiveness", 0.98));
component_var_vec_.push_back(VarModel("eta", "ETA", "Polytropic Efficiency of Compressors and Expanders", 0.90));
component_var_vec_.push_back(VarModel("eta_pump", "Pump ETA", "Polytropic Efficiency of Air Pump", 0.70));
component_var_vec_.push_back(VarModel("ploss_working", "Pressure Loss Fraction WF", "Fractional Pressure Loss of Working Fluid in Each Heat Exchanger", 0.01));
component_var_vec_.push_back(VarModel("ploss_air", "Pressure Loss Fraction Air", "Fractional Pressure Loss of Air", 0.005));
component_var_vec_.push_back(VarModel("ploss_liquid", "Pressure Loss Fraction Liquid", "Fractional Pressure Loss of Hot and Cold Reservoir HX", 0.02));
component_var_vec_.push_back(VarModel("motor_eff", "Motor Efficiency", "Motor Efficiency", 0.97216));
component_var_vec_.push_back(VarModel("gen_eff", "Generator Efficiency", "Generator Efficiency", 0.97216));
}
// Generate Cycle Variables
{
cycle_var_vec_.push_back(VarModel("T0", "Ambient Temperature (C)", "Ambient Temperature (C)", 288 - 273.15));
cycle_var_vec_.push_back(VarModel("P1", "P1 (Pa)", "Lowest Pressure in Cycle (Pa)", 5e5));
cycle_var_vec_.push_back(VarModel("T_compressor_inlet", "Temperature Compressor Inlet (C)", "Compressor Inlet Temperature (C)", 600 - 273.15));
cycle_var_vec_.push_back(VarModel("T_compressor_outlet", "Temperature Compressor Outlet (C)", "Compressor Outlet Temperature (C)", 800 - 273.15));
cycle_var_vec_.push_back(VarModel("power_output", "Power Output (MW)", "Power Output (MW)", 100));
cycle_var_vec_.push_back(VarModel("charge_time_hr", "Charge Time (hr)", "Charge Time (hr)", 10));
cycle_var_vec_.push_back(VarModel("discharge_time_hr", "Discharge Time (hr)", "Discharge Time (hr)", 10));
//cycle_var_vec_.push_back(VarModel("P0", "Ambient Pressure (Pa)", "Ambient Pressure (Pa)", 1e5));
}
// Setup SSC
this->SetupSSC();
// Setup UI
this->InitializeUI();
}
/// <summary>
/// Destructor
/// </summary>
PTESDesignPtDialog::~PTESDesignPtDialog()
{
ssc_module_free(module_);
}
/// <summary>
/// Get Design Point Calculation
/// </summary>
/// <param name="key">Result Name</param>
/// <returns></returns>
ssc_number_t PTESDesignPtDialog::GetResult(string key)
{
if (ssc_num_result_map_.find(key) != ssc_num_result_map_.end())
return ssc_num_result_map_[key];
else
return NULL;
}
/// <summary>
/// Get Map of Result Values
/// </summary>
/// <returns></returns>
std::map<string, ssc_number_t> PTESDesignPtDialog::GetResultNumMap()
{
if (has_run_ == false)
return std::map<string, ssc_number_t>();
return this->ssc_num_result_map_;
}
/// <summary>
/// Changes Default Value
/// </summary>
/// <param name="name">variable name</param>
/// <param name="value">value</param>
/// <returns></returns>
bool PTESDesignPtDialog::SetInputVal(string name, double value, bool is_readonly, bool is_visible)
{
// Check in Cycle
for (vector<VarModel> vec : { component_var_vec_, cycle_var_vec_ })
{
for (VarModel& var : vec)
{
if (var.kVarName == name)
{
var.SetTextValue(std::to_string(value));
var.SetReadonly(is_readonly);
var.SetVisible(is_visible);
return true;
}
}
}
return false;
}
/// <summary>
/// Add input variable that is passed on to the CMOD
/// Not Shown in dialog
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void PTESDesignPtDialog::AddHiddenInputVar(string name, double value)
{
VarModel var(name, "", "", value);
cycle_var_vec_.push_back(var);
}
/// <summary>
/// Set the Storage Fluid Material Properties
/// </summary>
/// <param name="hot_fluid_id">Hot Storage Fluid ID</param>
/// <param name="cold_fluid_id">Cold Storage Fluid ID</param>
/// <param name="hot_ud_props">Hot Storage User Defined Properties</param>
/// <param name="cold_ud_props">Cold Storage User Defined Properties</param>
void PTESDesignPtDialog::SetHTFProps(int hot_fluid_id, int cold_fluid_id, vector<vector<double>> hot_ud_props, vector<vector<double>> cold_ud_props)
{
this->hot_fluid_id_ = hot_fluid_id;
this->cold_fluid_id_ = cold_fluid_id;
this->hot_ud_fluid_props_ = hot_ud_props;
this->cold_ud_fluid_props_ = cold_ud_props;
this->is_htf_set_ = true;
}
/// <summary>
/// Set Up SSC Compute Module Connection
/// </summary>
void PTESDesignPtDialog::SetupSSC()
{
// Create SSC Data
data_ = ssc_data_create();
// Create SSC Module
module_ = ssc_module_create("ptes_design_point");
// Fill Result Variable Map
int i = 0;
ssc_info_t p_inf = NULL;
while (p_inf = ssc_module_var_info(module_, i++))
{
int var_type = ssc_info_var_type(p_inf); // SSC_INPUT, SSC_OUTPUT, SSC_INOUT
int data_type = ssc_info_data_type(p_inf); // SSC_INVALID, SSC_STRING, SSC_NUMBER, SSC_ARRAY, SSC_MATRIX
const char* name = ssc_info_name(p_inf);
const char* label = ssc_info_label(p_inf);
const char* units = ssc_info_units(p_inf);
const char* meta = ssc_info_meta(p_inf);
const char* group = ssc_info_group(p_inf);
// Store Number Outputs in result map
if (var_type == SSC_OUTPUT && data_type == SSC_NUMBER)
{
ssc_number_t x = 0;
ssc_num_result_map_.insert(std::pair<string, ssc_number_t>(name, x));
}
CmodIOModel io(var_type, data_type, name, label, units, meta, group);
cmod_vec_.push_back(io);
}
int x = 0;
}
/// <summary>
/// Run Compute Module
/// </summary>
string PTESDesignPtDialog::RunSSCModule()
{
// Check if Hot and Cold Fluid Materials have been defined
if (this->is_htf_set_ == false)
return "Reservoir fluids not set";
// Collect Input Variables Values and save to SSC Data
string error_var = "";
for (vector<VarModel> var_vec : { component_var_vec_, cycle_var_vec_ }) // Cycle and Component Variables
{
for (VarModel& var : var_vec)
{
// Try to Parse
bool flag = false;
double val = var.GetValue(flag);
// Set Data
if (flag == true)
{
ssc_data_set_number(data_, var.kVarName.c_str(), val);
}
else
{
error_var = var.kDisplayName;
break;
}
}
if (error_var != "")
break;
}
// Send variables not included in dialog
ssc_data_set_number(data_, "alpha", 2);
// Set Working Fluid
ssc_data_set_string(data_, working_fluid_.kVarName.c_str(), working_fluid_.GetSelectedMaterial().c_str());
if (error_var != "")
{
// There is an invalid user input
return "Invalid User Input: " + error_var;
}
// Send hot and cold fluid properties
{
// Convert User Defined Props to array
int hot_cols = hot_ud_fluid_props_[0].size();
int hot_rows = hot_ud_fluid_props_.size();
double_vec hot_flat;
for (double_vec row : hot_ud_fluid_props_)
for (double val : row)
hot_flat.push_back(val);
int cold_cols = cold_ud_fluid_props_[0].size();
int cold_rows = cold_ud_fluid_props_.size();
double_vec cold_flat;
for (double_vec row : cold_ud_fluid_props_)
for (double val : row)
cold_flat.push_back(val);
ssc_data_set_number(data_, "hot_fluid_id", hot_fluid_id_);
ssc_data_set_number(data_, "cold_fluid_id", cold_fluid_id_);
ssc_data_set_matrix(data_, "hot_ud_fluid_props", hot_flat.data(), hot_rows, hot_cols);
ssc_data_set_matrix(data_, "cold_ud_fluid_props", cold_flat.data(), cold_rows, cold_cols);
}
// Run Module
if (ssc_module_exec(module_, data_) == 0)
{
// Check for Error Messages
{
const char* text;
int type;
float time;
int index = 0;
while ((text = ssc_module_log(module_, index++, &type, &time)))
{
switch (type) // determines type
{
case SSC_NOTICE:
break;
case SSC_WARNING:
return text;
break;
case SSC_ERROR:
return text;
break;
default:
break;
}
}
}
// Error
return "Error Calculating Design Point";
}
// Collect Results from cmod
vector<ssc_bool_t> flag_vec;
for (auto& val : ssc_num_result_map_)
{
ssc_bool_t flag = ssc_data_get_number(data_, val.first.c_str(), &val.second);
flag_vec.push_back(flag);
}
for (CmodIOModel& val : cmod_vec_)
{
ssc_data_get_number(data_, val.name_.c_str(), &val.val_num_);
}
bool result_flag = true;
for (ssc_bool_t b : flag_vec)
{
if (b == false)
{
result_flag = false;
break;
}
}
if (result_flag == false)
{
// Unsuccessful Result Retrievel
return "Error Retrieving Result Data";
}
has_run_ = true;
result_code_ = 0;
return "";
}
/// <summary>
/// Build GUI
/// </summary>
void PTESDesignPtDialog::InitializeUI()
{
// Combine all into main vertical sizer
wxBoxSizer* szmain = new wxBoxSizer(wxVERTICAL);
this->SetBackgroundColour(*wxWHITE);
wxStaticText* header_text = new wxStaticText(this, wxID_ANY, "PTES Design Point");
header_text->SetFont(wxMetroTheme::Font(wxMT_LIGHT, 17));
szmain->Add(header_text, 0, wxALIGN_LEFT | wxALL, kMargin * 2);
// Tabs
wxPanel* tabpanel = new wxPanel(this, wxID_ANY);
ntbook_ = new wxMetroNotebook(tabpanel, wxID_ANY);
{
vector<wxTextCtrl*> component_textctrl_vec;
wxWindow* component_tab = GenerateTabWindow(component_var_vec_);
vector<wxTextCtrl*> cycle_textctrl_vec;
wxWindow* cycle_tab = GenerateTabWindow(cycle_var_vec_);
wxWindow* fluid_tab = GenerateFluidTab(working_fluid_);
ntbook_->AddPage(cycle_tab, "Cycle");
ntbook_->AddPage(component_tab, "Component");
ntbook_->AddPage(fluid_tab, "Fluid");
wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
panelSizer->Add(ntbook_, 1, wxEXPAND);
panelSizer->SetMinSize(400, 350);
tabpanel->SetSizer(panelSizer);
tabpanel->Fit();
szmain->Add(tabpanel, 1, wxEXPAND);
}
// Okay Cancel Buttons
szmain->Add(CreateButtonSizer(wxOK | wxCANCEL | wxHELP), 0, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, kMargin * 4);
SetSizer(szmain);
Fit();
}
/// <summary>
/// Generates A Tab Window
/// </summary>
/// <param name="var_vec">Vector of VarModels</param>
/// <returns></returns>
wxWindow* PTESDesignPtDialog::GenerateTabWindow(vector<VarModel>& var_vec)
{
// Make Window
wxWindow* tab_window = new wxWindow(ntbook_, wxID_ANY);
tab_window->SetBackgroundColour(*wxWHITE);
wxBoxSizer* window_szr = new wxBoxSizer(wxHORIZONTAL);
// Have Two Vertical Sizers Side By Side
wxBoxSizer* h_body = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* v_left = new wxBoxSizer(wxVERTICAL); // Left Column
wxBoxSizer* v_right = new wxBoxSizer(wxVERTICAL); // Right Column
// Loop through variables, adding text controls
int count = var_vec.size();
int count_left = std::round(count / 2.0);
int count_right = count - count_left;
int index = 0;
wxBoxSizer* column = v_left;
for (VarModel& var : var_vec)
{
if (index >= count_left)
column = v_right;
// Collect Variable Info
string name = var.kDisplayName;
string desc = var.kDescription;
wxStaticText* label = new wxStaticText(tab_window, wxID_ANY, name, wxDefaultPosition, wxSize(200, -1));
label->SetToolTip(desc);
column->Add(label, 0, wxTOP, kMargin);
wxTextCtrl* text_ctrl = new wxTextCtrl(tab_window, wxID_ANY, "", wxDefaultPosition, wxSize(kTxtCtrlWidth, kTxtCtrlHeight));
text_ctrl->SetToolTip(desc);
// Set Default Value
std::ostringstream val;
val << var.default_value_;
val.precision(2);
text_ctrl->SetValue(val.str());
column->Add(text_ctrl, 0, wxTOP, kMargin);
var.SetTextCtrl(text_ctrl);
index++;
}
h_body->Add(v_left, 0, wxRIGHT, kMargin);
h_body->Add(v_right, 0, wxLEFT, kMargin);
window_szr->Add(h_body, 0, wxALL, kMargin * 4);
tab_window->SetSizer(window_szr);
tab_window->Fit();
return tab_window;
}
/// <summary>
/// Generates the Fluid Tab Window and returns the window
/// </summary>
/// <param name="wf">Working Fluid</param>
/// <param name="hf">Hot Fluid</param>
/// <param name="cf">Cold Fluid</param>
/// <returns></returns>
wxWindow* PTESDesignPtDialog::GenerateFluidTab(FluidVarModel& wf)
{
// Group Fluids
//vector<std::reference_wrapper<FluidVarModel>> fluids = { wf, hf, cf };
vector<std::reference_wrapper<FluidVarModel>> fluids = { wf };
// Make Window
wxWindow* tab_window = new wxWindow(ntbook_, wxID_ANY);
tab_window->SetBackgroundColour(*wxWHITE);
wxBoxSizer* window_szr = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* content_szr = new wxBoxSizer(wxVERTICAL);
// Create Combo Box for each fluid Type (WF HF CF)
for (FluidVarModel& fluid : fluids)
{
// Label
string lable_string = fluid.GetFluidTypeString();
wxStaticText* label = new wxStaticText(tab_window, wxID_ANY, lable_string, wxDefaultPosition, wxSize(kTxtCtrlWidth * 1.25, -1));
content_szr->Add(label, 0, wxTOP, kMargin);
// Combo Box
//wxComboBox* combo = new wxComboBox(tab_window, wxID_ANY, "", wxDefaultPosition, wxSize(kTxtCtrlWidth, -1));
wxChoice* choice = new wxChoice(tab_window, wxID_ANY, wxDefaultPosition, wxSize(kTxtCtrlWidth, -1));
vector<string> fluid_options = fluid.GetFluidMaterials();
for (string s : fluid_options)
{
//combo->AppendString(s);
choice->AppendString(s);
}
//combo->SetValue(fluid_options[0]);
choice->SetSelection(0);
fluid.SetChoice(choice);
// Add to tab
content_szr->Add(choice, 0, wxTOP, kMargin);
}
window_szr->Add(content_szr, 0, wxALL, kMargin * 4);
tab_window->SetSizer(window_szr);
return tab_window;
}
/// <summary>
/// Show Confirm Results Dialog Box
/// </summary>
/// <returns>True: accept results, False: cancel results</returns>
bool PTESDesignPtDialog::LaunchConfirmDlg()
{
vector<CmodIOModel> result_vec;
// Send Results to dialog
for (CmodIOModel& io : cmod_vec_)
{
if (io.group_ == "SAM")
result_vec.push_back(io);
}
ConfirmDlg dlg(this, "", result_vec, this->kMargin);
dlg.CenterOnParent();
int code = dlg.ShowModal();
if (code == wxID_OK)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Catch GUI Events
/// </summary>
/// <param name="e"></param>
void PTESDesignPtDialog::OnEvt(wxCommandEvent& e)
{
switch (e.GetId())
{
case wxID_OK:
{
string error_msg = RunSSCModule();
// Show Confirm Screen
bool is_confirmed = false;
if (error_msg == "")
{
is_confirmed = LaunchConfirmDlg();
}
// Issue running module
else
{
wxMessageBox(error_msg);
}
if(is_confirmed)
EndModal(wxID_OK);
break;
}
case wxID_HELP:
{
SamApp::ShowHelp("window-reference/win_ptes_design_pt");
break;
}
}
}