ThermalCompartmentTest.cpp 38.8 KB
Newer Older
Aaron Bray's avatar
Aaron Bray committed
1
2
/* Distributed under the Apache License, Version 2.0.
   See accompanying NOTICE file for details.*/
Aaron Bray's avatar
Aaron Bray committed
3
4
5
6
7
8
9
10
11

#include "CommonDataModelTest.h"
#include "substance/SESubstanceManager.h"
#include "substance/SESubstance.h"
#include "utils/TimingProfile.h"
#include "utils/testing/SETestReport.h"
#include "utils/testing/SETestCase.h"
#include "utils/testing/SETestSuite.h"
#include "utils/GeneralMath.h"
Aaron Bray's avatar
Aaron Bray committed
12
#include "circuit/SECircuitManager.h"
Aaron Bray's avatar
Aaron Bray committed
13
14
15
16
17
18
19
#include "circuit/thermal/SEThermalCircuitNode.h"
#include "circuit/thermal/SEThermalCircuitPath.h"
#include "circuit/thermal/SEThermalCircuit.h"
#include "compartment/thermal/SEThermalCompartment.h"
#include "compartment/thermal/SEThermalCompartmentLink.h"
#include "compartment/SECompartmentManager.h"

20
#include "properties/SEScalar0To1.h"
Aaron Bray's avatar
Aaron Bray committed
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
#include "properties/SEScalarEnergy.h"
#include "properties/SEScalarPower.h"
#include "properties/SEScalarTime.h"

// Maybe we want to test a couple compartements that overlap 
// i.e. 2 compartments containing the same node and or path


//void CheckTemperatureAndHeat(SETestCase& testCase, SEThermalCompartment& cmpt);
//void TestFlow(SETestCase& testCase, SEThermalCompartment& cmpt, double inflow_kcal_Per_s, double outflow_kcal_Per_s);

void CommonDataModelTest::TestThermalFlows(SETestSuite& testSuite, SESubstanceManager& subMgr)
{
  SETestCase& testCase = testSuite.CreateTestCase();
  testCase.SetName("HeatTemperatureFlows");

  TimingProfile pTimer;
  pTimer.Start("Test");

  // Heat and Temperature are just one to one mapping to a single scalar

  SECompartmentManager cmptMgr(subMgr);
  // Left Compartment
  SEThermalCompartment* left = &cmptMgr.CreateThermalCompartment("Left");
  // Middle Compartment
  SEThermalCompartment* middle = &cmptMgr.CreateThermalCompartment("Middle");
  // Right Compartment
  SEThermalCompartment* right = &cmptMgr.CreateThermalCompartment("Right");
  // Compartment Links
  double largeInflow_kcal_Per_s = 5.0;
  double smallOutflow_kcal_Per_s = 1.0;
  double largeOutflow_kcal_Per_s = 7.0;
  double smallInflow_kcal_Per_s = 2.0;
  double inflow_kcal_Per_s;
  double outflow_kcal_Per_s;
  SEThermalCompartmentLink* left2middle = &cmptMgr.CreateThermalLink(*left, *middle, "Left2Middle");
  left2middle->GetHeatTransferRate().SetValue(largeInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  SEThermalCompartmentLink* middle2right = &cmptMgr.CreateThermalLink(*middle, *right, "Middle2Right");
  middle2right->GetHeatTransferRate().SetValue(smallOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  SEThermalCompartmentLink* middle2left = &cmptMgr.CreateThermalLink(*middle, *left, "Middle2Left");
  middle2left->GetHeatTransferRate().SetValue(largeOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  SEThermalCompartmentLink* right2middle = &cmptMgr.CreateThermalLink(*right, *middle, "Right2Middle");
  right2middle->GetHeatTransferRate().SetValue(smallInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  cmptMgr.StateChange();

66
  TestCompartmentSerialization(cmptMgr, m_OutDirectory + "/TestThermalFlows.json");
Aaron Bray's avatar
Aaron Bray committed
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
  left = cmptMgr.GetThermalCompartment("Left");
  middle = cmptMgr.GetThermalCompartment("Middle");
  right = cmptMgr.GetThermalCompartment("Right");
  left2middle = cmptMgr.GetThermalLink("Left2Middle");
  middle2right = cmptMgr.GetThermalLink("Middle2Right");
  middle2left = cmptMgr.GetThermalLink("Middle2Left");
  right2middle = cmptMgr.GetThermalLink("Right2Middle");

  inflow_kcal_Per_s = largeInflow_kcal_Per_s + smallInflow_kcal_Per_s;
  outflow_kcal_Per_s = largeOutflow_kcal_Per_s + smallOutflow_kcal_Per_s;
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }

  // Change direction of the 2 small flows
  middle2right->GetHeatTransferRate().SetValue(-smallOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  right2middle->GetHeatTransferRate().SetValue(-smallInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  inflow_kcal_Per_s = largeInflow_kcal_Per_s + smallOutflow_kcal_Per_s;
  outflow_kcal_Per_s = largeOutflow_kcal_Per_s + smallInflow_kcal_Per_s;
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }

  // Change direction of the 2 large flows
  left2middle->GetHeatTransferRate().SetValue(-largeInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  middle2left->GetHeatTransferRate().SetValue(-largeOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  inflow_kcal_Per_s = largeOutflow_kcal_Per_s + smallOutflow_kcal_Per_s;
  outflow_kcal_Per_s = largeInflow_kcal_Per_s + smallInflow_kcal_Per_s;
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  testCase.GetDuration().SetValue(pTimer.GetElapsedTime_s("Test"), TimeUnit::s);
}

void CommonDataModelTest::TestThermalFlowHierarchy(SETestSuite& testSuite, SESubstanceManager& subMgr)
{
  SETestCase& testCase = testSuite.CreateTestCase();
  testCase.SetName("ThermalFlowHierarchy");

  TimingProfile pTimer;
  pTimer.Start("Test");

  //                L0C0
  //               /    \
  //           L1C0      L1C1
  //           /  \      /  \
  //        L2C0  L2C1 L2C2 L2C3 <-- Only these cmpts have data
  //        L2C4  L2C5 L2C6 L2C7 <-- Have Data, same level as above, but these will not be in the cmpt hierarchy
  //                                 They define the L0 and L1 Level compartment input/output links

  SECompartmentManager cmptMgr(subMgr);
  SEThermalCompartment* L0C0 = &cmptMgr.CreateThermalCompartment("L0C0");
  SEThermalCompartment* L1C0 = &cmptMgr.CreateThermalCompartment("L1C0");
  SEThermalCompartment* L1C1 = &cmptMgr.CreateThermalCompartment("L1C1");
  SEThermalCompartment* L2C0 = &cmptMgr.CreateThermalCompartment("L2C0");
  SEThermalCompartment* L2C1 = &cmptMgr.CreateThermalCompartment("L2C1");
  SEThermalCompartment* L2C2 = &cmptMgr.CreateThermalCompartment("L2C2");
  SEThermalCompartment* L2C3 = &cmptMgr.CreateThermalCompartment("L2C3");
  SEThermalCompartment* L2C4 = &cmptMgr.CreateThermalCompartment("L2C4");
  SEThermalCompartment* L2C5 = &cmptMgr.CreateThermalCompartment("L2C5");
  SEThermalCompartment* L2C6 = &cmptMgr.CreateThermalCompartment("L2C6");
  SEThermalCompartment* L2C7 = &cmptMgr.CreateThermalCompartment("L2C7");

  double L2C0toL2C4_kcal_Per_s = 2;
  SEThermalCompartmentLink* L2C0toL2C4 = &cmptMgr.CreateThermalLink(*L2C0, *L2C4, "L2C0toL2C4");
  L2C0toL2C4->GetHeatTransferRate().SetValue(L2C0toL2C4_kcal_Per_s, PowerUnit::kcal_Per_s);
  double L2C4toL2C0_kcal_Per_s = 4;
  SEThermalCompartmentLink* L2C4toL2C0 = &cmptMgr.CreateThermalLink(*L2C4, *L2C0, "L2C4toL2C0");
  L2C4toL2C0->GetHeatTransferRate().SetValue(L2C4toL2C0_kcal_Per_s, PowerUnit::kcal_Per_s);

  double L2C1toL2C5_kcal_Per_s = 6;
  SEThermalCompartmentLink* L2C1toL2C5 = &cmptMgr.CreateThermalLink(*L2C1, *L2C5, "L2C1toL2C5");
  L2C1toL2C5->GetHeatTransferRate().SetValue(L2C1toL2C5_kcal_Per_s, PowerUnit::kcal_Per_s);
  double L2C5toL2C1_kcal_Per_s = 8;
  SEThermalCompartmentLink* L2C5toL2C1 = &cmptMgr.CreateThermalLink(*L2C5, *L2C1, "L2C5toL2C1");
  L2C5toL2C1->GetHeatTransferRate().SetValue(L2C5toL2C1_kcal_Per_s, PowerUnit::kcal_Per_s);

  double L2C2toL2C6_kcal_Per_s = 10;
  SEThermalCompartmentLink* L2C2toL2C6 = &cmptMgr.CreateThermalLink(*L2C2, *L2C6, "L2C2toL2C6");
  L2C2toL2C6->GetHeatTransferRate().SetValue(L2C2toL2C6_kcal_Per_s, PowerUnit::kcal_Per_s);
  double L2C6toL2C2_kcal_Per_s = 12;
  SEThermalCompartmentLink* L2C6toL2C2 = &cmptMgr.CreateThermalLink(*L2C6, *L2C2, "L2C6toL2C2");
  L2C6toL2C2->GetHeatTransferRate().SetValue(L2C6toL2C2_kcal_Per_s, PowerUnit::kcal_Per_s);

  double L2C3toL2C7_kcal_Per_s = 14;
  SEThermalCompartmentLink* L2C3toL2C7 = &cmptMgr.CreateThermalLink(*L2C3, *L2C7, "L2C3toL2C7");
  L2C3toL2C7->GetHeatTransferRate().SetValue(L2C3toL2C7_kcal_Per_s, PowerUnit::kcal_Per_s);
  double L2C7toL2C3_kcal_Per_s = 18;
  SEThermalCompartmentLink* L2C7toL2C3 = &cmptMgr.CreateThermalLink(*L2C7, *L2C3, "L2C7toL2C3");
  L2C7toL2C3->GetHeatTransferRate().SetValue(L2C7toL2C3_kcal_Per_s, PowerUnit::kcal_Per_s);

  // Build up the hierarchy, note L2C4-7 are not in the hierarchy
  L0C0->AddChild(*L1C0);
  L0C0->AddChild(*L1C1);
  L1C0->AddChild(*L2C0);
  L1C0->AddChild(*L2C1);
  L1C1->AddChild(*L2C2);
  L1C1->AddChild(*L2C3);
  cmptMgr.StateChange();// Call this, AFTER YOU SET UP YOUR HIERARCHY, to ensure all parent compartments have their link data

230
  TestCompartmentSerialization(cmptMgr, m_OutDirectory + "/TestThermalFlowHierarchy.json");
Aaron Bray's avatar
Aaron Bray committed
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
  L0C0 = cmptMgr.GetThermalCompartment("L0C0");
  L1C0 = cmptMgr.GetThermalCompartment("L1C0");
  L1C1 = cmptMgr.GetThermalCompartment("L1C1");
  L2C0 = cmptMgr.GetThermalCompartment("L2C0");
  L2C1 = cmptMgr.GetThermalCompartment("L2C1");
  L2C2 = cmptMgr.GetThermalCompartment("L2C2");
  L2C3 = cmptMgr.GetThermalCompartment("L2C3"); 
  L2C4 = cmptMgr.GetThermalCompartment("L2C4");
  L2C5 = cmptMgr.GetThermalCompartment("L2C5");
  L2C6 = cmptMgr.GetThermalCompartment("L2C6");
  L2C7 = cmptMgr.GetThermalCompartment("L2C7");

  //                       Expected Inflow,       Expected Outflow
  TestFlow(testCase, *L2C0, L2C4toL2C0_kcal_Per_s, L2C0toL2C4_kcal_Per_s);
  TestFlow(testCase, *L2C1, L2C5toL2C1_kcal_Per_s, L2C1toL2C5_kcal_Per_s);
  TestFlow(testCase, *L2C2, L2C6toL2C2_kcal_Per_s, L2C2toL2C6_kcal_Per_s);
  TestFlow(testCase, *L2C3, L2C7toL2C3_kcal_Per_s, L2C3toL2C7_kcal_Per_s);
  TestFlow(testCase, *L2C4, L2C0toL2C4_kcal_Per_s, L2C4toL2C0_kcal_Per_s);
  TestFlow(testCase, *L2C5, L2C1toL2C5_kcal_Per_s, L2C5toL2C1_kcal_Per_s);
  TestFlow(testCase, *L2C6, L2C2toL2C6_kcal_Per_s, L2C6toL2C2_kcal_Per_s);
  TestFlow(testCase, *L2C7, L2C3toL2C7_kcal_Per_s, L2C7toL2C3_kcal_Per_s);

  TestFlow(testCase, *L1C0, L2C4toL2C0_kcal_Per_s + L2C5toL2C1_kcal_Per_s, L2C0toL2C4_kcal_Per_s + L2C1toL2C5_kcal_Per_s);
  TestFlow(testCase, *L1C1, L2C6toL2C2_kcal_Per_s + L2C7toL2C3_kcal_Per_s, L2C2toL2C6_kcal_Per_s + L2C3toL2C7_kcal_Per_s);
  TestFlow(testCase, *L0C0, L2C4toL2C0_kcal_Per_s + L2C5toL2C1_kcal_Per_s + L2C6toL2C2_kcal_Per_s + L2C7toL2C3_kcal_Per_s, L2C0toL2C4_kcal_Per_s + L2C1toL2C5_kcal_Per_s + L2C2toL2C6_kcal_Per_s + L2C3toL2C7_kcal_Per_s);

  testCase.GetDuration().SetValue(pTimer.GetElapsedTime_s("Test"), TimeUnit::s);
}

void CommonDataModelTest::TestFlow(SETestCase& testCase, SEThermalCompartment& cmpt, double inflow_kcal_Per_s, double outflow_kcal_Per_s)
{
  if (!cmpt.HasHeatTransferRateIn())
    testCase.AddFailure(cmpt.GetName() + " does not have Inflow");
  if (!cmpt.HasHeatTransferRateOut())
    testCase.AddFailure(cmpt.GetName() + " does not have Outflow");
  m_ss << cmpt.GetName() + " Inflow : " << cmpt.GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " const InFlow is not correct : " << cmpt.GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << cmpt.GetName() + " Inflow : " << cmpt.GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " const InFlow is not correct : " << cmpt.GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << cmpt.GetName() + " Outflow : " << cmpt.GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " const OutFlow is not correct : " << cmpt.GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << cmpt.GetName() + " Outflow : " << cmpt.GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " const OutFlow is not correct : " << cmpt.GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
}

void CommonDataModelTest::TestThermalHierarchy(SETestSuite& testSuite, SESubstanceManager& subMgr)
{
  SETestCase& testCase = testSuite.CreateTestCase();
  testCase.SetName("HierarchyHeatTemperatureFlows");

  SEScalarTemperature partialTemperature;

  TimingProfile pTimer;
  pTimer.Start("Test");

  //         L0C0
  //        /    \
  //    L1C0      L1C1
  //    /  \      /  \
  // L2C0  L2C1 L2C3 L2C4 <-- Only these cmpts have data

  SECompartmentManager cmptMgr(subMgr);
  // Level 0
  SEThermalCompartment* L0C0 = &cmptMgr.CreateThermalCompartment("L0C0");
  // Level 1
  SEThermalCompartment* L1C0 = &cmptMgr.CreateThermalCompartment("L1C0");
  SEThermalCompartment* L1C1 = &cmptMgr.CreateThermalCompartment("L1C1");
  // Level 2
  double L2C0_kcal = 3;
  double L2C0_C = 3;
  SEThermalCompartment* L2C0 = &cmptMgr.CreateThermalCompartment("L2C0");
  L2C0->GetHeat().SetValue(L2C0_kcal, EnergyUnit::kcal);
  L2C0->GetTemperature().SetValue(L2C0_C, TemperatureUnit::C);

  double L2C1_kcal = 6;
  double L2C1_C = 6;
  SEThermalCompartment* L2C1 = &cmptMgr.CreateThermalCompartment("L2C1");
  L2C1->GetHeat().SetValue(L2C1_kcal, EnergyUnit::kcal);
  L2C1->GetTemperature().SetValue(L2C1_C, TemperatureUnit::C);

  double L2C2_kcal = 9;
  double L2C2_C = 9;
  SEThermalCompartment* L2C2 = &cmptMgr.CreateThermalCompartment("L2C2");
  L2C2->GetHeat().SetValue(L2C2_kcal, EnergyUnit::kcal);
  L2C2->GetTemperature().SetValue(L2C2_C, TemperatureUnit::C);

  double L2C3_kcal = 12;
  double L2C3_C = 12;
  SEThermalCompartment* L2C3 = &cmptMgr.CreateThermalCompartment("L2C3");
  L2C3->GetHeat().SetValue(L2C3_kcal, EnergyUnit::kcal);
  L2C3->GetTemperature().SetValue(L2C3_C, TemperatureUnit::C);

  // Build up the hierarchy
  L0C0->AddChild(*L1C0);
  L0C0->AddChild(*L1C1);
  L1C0->AddChild(*L2C0);
  L1C0->AddChild(*L2C1);
  L1C1->AddChild(*L2C2);
  L1C1->AddChild(*L2C3);
  cmptMgr.StateChange();

348
  TestCompartmentSerialization(cmptMgr, m_OutDirectory + "/TestThermalHierarchy.json");
Aaron Bray's avatar
Aaron Bray committed
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
  L0C0 = cmptMgr.GetThermalCompartment("L0C0");
  L1C0 = cmptMgr.GetThermalCompartment("L1C0");
  L1C1 = cmptMgr.GetThermalCompartment("L1C1");
  L2C0 = cmptMgr.GetThermalCompartment("L2C0");
  L2C1 = cmptMgr.GetThermalCompartment("L2C1");
  L2C2 = cmptMgr.GetThermalCompartment("L2C2");
  L2C3 = cmptMgr.GetThermalCompartment("L2C3");

  // Check our Heat 
  double L1C0_expected_kcal = L2C0_kcal + L2C1_kcal;
  if (L1C0->GetHeat(EnergyUnit::kcal) != L1C0_expected_kcal)
  {
    m_ss << L1C0->GetName() << " const Heat is not correct : " << L1C0->GetHeat(EnergyUnit::kcal) << " expected " << L1C0_expected_kcal;
    testCase.AddFailure(m_ss);
  }
  if (L1C0->GetHeat().GetValue(EnergyUnit::kcal) != L1C0_expected_kcal)
  {
    m_ss << L1C0->GetName() << " Heat is not correct : " << L1C0->GetHeat().GetValue(EnergyUnit::kcal) << " expected " << L1C0_expected_kcal;
    testCase.AddFailure(m_ss);
  }
  double L1C1_expected_kcal = L2C2_kcal + L2C3_kcal;
  if (L1C1->GetHeat(EnergyUnit::kcal) != L1C1_expected_kcal)
  {
    m_ss << L1C1->GetName() << " const Heat is not correct : " << L1C1->GetHeat(EnergyUnit::kcal) << " expected " << L1C1_expected_kcal;
    testCase.AddFailure(m_ss);
  }
  if (L1C1->GetHeat().GetValue(EnergyUnit::kcal) != L1C1_expected_kcal)
  {
    m_ss << L1C1->GetName() << " Heat is not correct : " << L1C1->GetHeat().GetValue(EnergyUnit::kcal) << " expected " << L1C1_expected_kcal;
    testCase.AddFailure(m_ss);
  }
  double L0C0_expected_kcal = L1C0_expected_kcal + L1C1_expected_kcal;
  if (L0C0->GetHeat(EnergyUnit::kcal) != L0C0_expected_kcal)
  {
    m_ss << L0C0->GetName() << " const Heat is not correct : " << L0C0->GetHeat(EnergyUnit::kcal) << " expected " << L0C0_expected_kcal;
    testCase.AddFailure(m_ss);
  }
  if (L0C0->GetHeat().GetValue(EnergyUnit::kcal) != L0C0_expected_kcal)
  {
    m_ss << L0C0->GetName() << " Heat is not correct : " << L0C0->GetHeat().GetValue(EnergyUnit::kcal) << " expected " << L0C0_expected_kcal;
    testCase.AddFailure(m_ss);
  }

  // Check our Temperature
  double L1C0_expected_C = (L2C0_C * (L2C0_kcal / L1C0_expected_kcal)) + (L2C1_C * (L2C1_kcal / L1C0_expected_kcal));
  if (L1C0->GetTemperature(TemperatureUnit::C) != L1C0_expected_C)
  {
    m_ss << L1C0->GetName() << " const Temperature is not correct : " << L1C0->GetTemperature(TemperatureUnit::C) << " expected " << L1C0_expected_C;
    testCase.AddFailure(m_ss);
  }
  if (L1C0->GetTemperature().GetValue(TemperatureUnit::C) != L1C0_expected_C)
  {
    m_ss << L1C0->GetName() << " Temperature is not correct : " << L1C0->GetTemperature().GetValue(TemperatureUnit::C) << " expected " << L1C0_expected_C;
    testCase.AddFailure(m_ss);
  }
  double L1C1_expected_C = (L2C2_C * (L2C2_kcal / L1C1_expected_kcal)) + (L2C3_C * (L2C3_kcal / L1C1_expected_kcal));
  if (L1C1->GetTemperature(TemperatureUnit::C) != L1C1_expected_C)
  {
    m_ss << L1C1->GetName() << " const Temperature is not correct : " << L1C1->GetTemperature(TemperatureUnit::C) << " expected " << L1C1_expected_C;
    testCase.AddFailure(m_ss);
  }
  if (L1C1->GetTemperature().GetValue(TemperatureUnit::C) != L1C1_expected_C)
  {
    m_ss << L1C1->GetName() << " Temperature is not correct : " << L1C1->GetTemperature().GetValue(TemperatureUnit::C) << " expected " << L1C1_expected_C;
    testCase.AddFailure(m_ss);
  }
  double L0C0_expected_C = (L1C0_expected_C * (L1C0_expected_kcal / L0C0_expected_kcal)) + (L1C1_expected_C * (L1C1_expected_kcal / L0C0_expected_kcal));
  if (L0C0->GetTemperature(TemperatureUnit::C) != L0C0_expected_C)
  {
    m_ss << L0C0->GetName() << " const Temperature is not correct : " << L0C0->GetTemperature(TemperatureUnit::C) << " expected " << L0C0_expected_C;
    testCase.AddFailure(m_ss);
  }
  if (L0C0->GetTemperature().GetValue(TemperatureUnit::C) != L0C0_expected_C)
  {
    m_ss << L0C0->GetName() << " Temperature is not correct : " << L0C0->GetTemperature().GetValue(TemperatureUnit::C) << " expected " << L0C0_expected_C;
    testCase.AddFailure(m_ss);
  }

  testCase.GetDuration().SetValue(pTimer.GetElapsedTime_s("Test"), TimeUnit::s);
}

void CommonDataModelTest::TestCircuitHeatTemperatureAndFlows(SETestSuite& testSuite, SESubstanceManager& subMgr)
{
  SETestCase& testCase = testSuite.CreateTestCase();
  testCase.SetName("HeatTemperatureFlows");

  TimingProfile pTimer;
  pTimer.Start("Test");

Aaron Bray's avatar
Aaron Bray committed
438
  SEThermalCircuit& circuit = m_Circuits->CreateThermalCircuit("TestCircuit");
Aaron Bray's avatar
Aaron Bray committed
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
  // Left Nodes
  SEThermalCircuitNode& l1 = circuit.CreateNode("Left1");
  l1.GetNextTemperature().SetValue(10., TemperatureUnit::C);
  l1.GetHeatBaseline().SetValue(10., EnergyUnit::kcal);
  SEThermalCircuitNode l2 = circuit.CreateNode("Left2");// No Heat
  l2.GetNextTemperature().SetValue(22., TemperatureUnit::C);
  // Middle Nodes
  SEThermalCircuitNode& m1 = circuit.CreateNode("Middle1");
  m1.GetNextTemperature().SetValue(4.0, TemperatureUnit::C);
  m1.GetHeatBaseline().SetValue(8.0, EnergyUnit::kcal);
  SEThermalCircuitNode& m2 = circuit.CreateNode("Middle2");
  m2.GetNextTemperature().SetValue(5.0, TemperatureUnit::C);
  m2.GetHeatBaseline().SetValue(10., EnergyUnit::kcal);
  // Right Nodes
  SEThermalCircuitNode& r1 = circuit.CreateNode("Right1");
  r1.GetNextTemperature().SetValue(10., TemperatureUnit::C);
  r1.GetHeatBaseline().SetValue(10., EnergyUnit::kcal);
  SEThermalCircuitNode& r2 = circuit.CreateNode("Right2");
  r2.GetNextTemperature().SetValue(22., TemperatureUnit::C);
  r2.GetHeatBaseline().SetValue(10., EnergyUnit::kcal);
  // Paths
  double largeInflow_kcal_Per_s = 5.0;
  double smallOutflow_kcal_Per_s = 1.0;
  double largeOutflow_kcal_Per_s = 7.0;
  double smallInflow_kcal_Per_s = 2.0;
  double inflow_kcal_Per_s;
  double outflow_kcal_Per_s;
  SEThermalCircuitPath& p1 = circuit.CreatePath(l1, m1, "l1->m1");//Large Inflow
  p1.GetNextHeatTransferRate().SetValue(largeInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  SEThermalCircuitPath& p2 = circuit.CreatePath(m1, r1, "m1->r1");//Small Outflow
  p2.GetNextHeatTransferRate().SetValue(smallOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  SEThermalCircuitPath& p3 = circuit.CreatePath(m2, l2, "m2->l2");// Large Outflow
  p3.GetNextHeatTransferRate().SetValue(largeOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  SEThermalCircuitPath& p4 = circuit.CreatePath(r2, m2, "r2->m2");// Small Inflow
  p4.GetNextHeatTransferRate().SetValue(smallInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  circuit.SetNextAndCurrentFromBaselines();
  circuit.StateChange();

  SECompartmentManager cmptMgr(subMgr);
  // Left Compartment
  SEThermalCompartment* left = &cmptMgr.CreateThermalCompartment("Left");
  left->MapNode(l1);
  left->MapNode(l2);
  // Middle Compartment
  SEThermalCompartment* middle = &cmptMgr.CreateThermalCompartment("Middle");
  middle->MapNode(m1);
  middle->MapNode(m2);
  // Right Compartment
  SEThermalCompartment* right = &cmptMgr.CreateThermalCompartment("Right");
  right->MapNode(r1);
  right->MapNode(r2);
  // Compartment Links
  SEThermalCompartmentLink* left2middle = &cmptMgr.CreateThermalLink(*left, *middle, "Left2Middle");
  left2middle->MapPath(p1);
  SEThermalCompartmentLink* middle2right = &cmptMgr.CreateThermalLink(*middle, *right, "Middle2Right");
  middle2right->MapPath(p2);
  SEThermalCompartmentLink* middle2left = &cmptMgr.CreateThermalLink(*middle, *left, "Middle2Left");
  middle2left->MapPath(p3);
  SEThermalCompartmentLink* right2middle = &cmptMgr.CreateThermalLink(*right, *middle, "Right2Middle");
  right2middle->MapPath(p4);
  cmptMgr.StateChange();

501
  TestCompartmentSerialization(cmptMgr, m_OutDirectory + "/TestThermalCircuitHeatTemperatureAndFlows.json");
Aaron Bray's avatar
Aaron Bray committed
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
  left = cmptMgr.GetThermalCompartment("Left");
  middle = cmptMgr.GetThermalCompartment("Middle");
  right = cmptMgr.GetThermalCompartment("Right");
  left2middle = cmptMgr.GetThermalLink("Left2Middle");
  middle2right = cmptMgr.GetThermalLink("Middle2Right");
  middle2left = cmptMgr.GetThermalLink("Middle2Left");

  CheckTemperatureAndHeat(testCase, *left);
  CheckTemperatureAndHeat(testCase, *middle);
  CheckTemperatureAndHeat(testCase, *right);

  inflow_kcal_Per_s = largeInflow_kcal_Per_s + smallInflow_kcal_Per_s;
  outflow_kcal_Per_s = largeOutflow_kcal_Per_s + smallOutflow_kcal_Per_s;
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }

  // Change direction of the 2 small flows
  p2.GetNextHeatTransferRate().SetValue(-smallOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  p4.GetNextHeatTransferRate().SetValue(-smallInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  inflow_kcal_Per_s = largeInflow_kcal_Per_s + smallOutflow_kcal_Per_s;
  outflow_kcal_Per_s = largeOutflow_kcal_Per_s + smallInflow_kcal_Per_s;
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }

  // Change direction of the 2 large flows
  p1.GetNextHeatTransferRate().SetValue(-largeInflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  p3.GetNextHeatTransferRate().SetValue(-largeOutflow_kcal_Per_s, PowerUnit::kcal_Per_s);
  inflow_kcal_Per_s = largeOutflow_kcal_Per_s + smallOutflow_kcal_Per_s;
  outflow_kcal_Per_s = largeInflow_kcal_Per_s + smallInflow_kcal_Per_s;
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Inflow : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " vs. inflow_kcal_Per_s " << inflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s), inflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const InFlow is not correct : " <<middle->GetHeatTransferRateIn().GetValue(PowerUnit::kcal_Per_s) << " expected " << inflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Middle Outflow : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " vs. outflow_kcal_Per_s " << outflow_kcal_Per_s; Info(m_ss);
  if (GeneralMath::PercentTolerance(middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s), outflow_kcal_Per_s)>m_PercentTolerance)
  {
    m_ss <<middle->GetName() << " const OutFlow is not correct : " <<middle->GetHeatTransferRateOut().GetValue(PowerUnit::kcal_Per_s) << " expected " << outflow_kcal_Per_s;
    testCase.AddFailure(m_ss);
  }
  testCase.GetDuration().SetValue(pTimer.GetElapsedTime_s("Test"), TimeUnit::s);
}

void CommonDataModelTest::CheckTemperatureAndHeat(SETestCase& testCase, SEThermalCompartment& cmpt)
{
  // Check Heat
  const std::vector<SEThermalCircuitNode*>& vNodes = cmpt.GetNodeMapping().GetNodes();
  double nHeat_kcal = 0;
  for (SEThermalCircuitNode* n : vNodes)
  {
    if (n->HasNextHeat())
      nHeat_kcal += n->GetNextHeat().GetValue(EnergyUnit::kcal);
  }
  m_ss << "Cmpt Heat : " << cmpt.GetHeat(EnergyUnit::kcal) << " vs. Node Heat " << nHeat_kcal;
  Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetHeat(EnergyUnit::kcal), nHeat_kcal)>m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " const Heat is not correct : " << cmpt.GetHeat(EnergyUnit::kcal) << " expected " << nHeat_kcal;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Cmpt Heat : " << cmpt.GetHeat().GetValue(EnergyUnit::kcal) << " vs. Node Heat " << nHeat_kcal;
  Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetHeat().GetValue(EnergyUnit::kcal), nHeat_kcal)>m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " Heat is not correct : " << cmpt.GetHeat().GetValue(EnergyUnit::kcal) << " expected " << nHeat_kcal;
    testCase.AddFailure(m_ss);
  }

  // We can do Temperature in two ways
  // We look for nodes with Heat and Temperature
  // -- if multiple nodes are found we Heat weight the Temperature sum
  // If no nodes have Heat and Temperature, we look for nodes with Temperature
  // -- if multiple nodes are found we average the Temperature
  bool HeatWeightedTemperature = false;
  bool averageTemperature = false;
  int TemperatureNodes = 0;
  for (SEThermalCircuitNode* n : cmpt.GetNodeMapping().GetNodes())
  {
    if (n->HasNextPotential())
    {
      averageTemperature = true;
      if (n->HasNextQuantity())
        HeatWeightedTemperature = true;
    }
  }
  if (HeatWeightedTemperature == false && averageTemperature == false)
  {
    Info("No Temperature values found on compartment");
    return;
  }
  const std::vector<SEThermalCircuitNode*>& pNodes = cmpt.GetNodeMapping().GetNodes();
  double nTemperature_C = 0;
  for (SEThermalCircuitNode* n : pNodes)
  {
    if (HeatWeightedTemperature)
    {
      if (n->HasNextTemperature() && n->HasNextHeat())
        nTemperature_C += n->GetNextTemperature().GetValue(TemperatureUnit::C) * (n->GetNextHeat(EnergyUnit::kcal) / nHeat_kcal);
    }
    else
    {
      if (n->HasNextTemperature())
      {
        TemperatureNodes++;
        nTemperature_C += n->GetNextTemperature().GetValue(TemperatureUnit::C);
      }
    }
  }
  if (!HeatWeightedTemperature && averageTemperature)
    nTemperature_C /= TemperatureNodes;
  m_ss << "Cmpt Temperature : " << cmpt.GetTemperature(TemperatureUnit::C) << " vs. Node Temperature " << nTemperature_C;
  Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetTemperature(TemperatureUnit::C), nTemperature_C) > m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " const Temperature is not correct : " << cmpt.GetTemperature(TemperatureUnit::C) << " expected " << nTemperature_C;
    testCase.AddFailure(m_ss);
  }
  m_ss << "Cmpt Temperature : " << cmpt.GetTemperature().GetValue(TemperatureUnit::C) << " vs. Node Temperature " << nTemperature_C;
  Info(m_ss);
  if (GeneralMath::PercentTolerance(cmpt.GetTemperature().GetValue(TemperatureUnit::C), nTemperature_C) > m_PercentTolerance)
  {
    m_ss << cmpt.GetName() << " Temperature Get methods are not equal! : " << cmpt.GetTemperature().GetValue(TemperatureUnit::C) << " expected " << nTemperature_C;
    testCase.AddFailure(m_ss);
  }
}

void CommonDataModelTest::ThermalCompartmentTest(const std::string& rptDirectory)
{
  m_PercentTolerance = 2.0;
  m_OutDirectory = rptDirectory;

690
  m_Logger->SetLogFile(rptDirectory + "/ThermalCompartmentTest.log");
Aaron Bray's avatar
Aaron Bray committed
691
692
693
  SETestReport testReport(m_Logger);

  SESubstanceManager subMgr(m_Logger);
694
  subMgr.LoadSubstanceDirectory();
Aaron Bray's avatar
Aaron Bray committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711

  SETestSuite&  Flows = testReport.CreateTestSuite();
  Flows.SetName("ThermalCompartmentFlows");
  TestThermalFlows(Flows, subMgr);

  SETestSuite&  FlowHierarchy = testReport.CreateTestSuite();
  FlowHierarchy.SetName("ThermalCompartmentFlowHierarchy");
  TestThermalFlowHierarchy(FlowHierarchy, subMgr);

  SETestSuite&  Hierarchy = testReport.CreateTestSuite();
  Hierarchy.SetName("ThermalCompartmentHierarchy");
  TestThermalHierarchy(Hierarchy, subMgr);

  SETestSuite&  CircuitHeatTemperatureAndFlows = testReport.CreateTestSuite();
  CircuitHeatTemperatureAndFlows.SetName("ThermalCompartmentHeatTemperatureAndFlows");
  TestCircuitHeatTemperatureAndFlows(CircuitHeatTemperatureAndFlows, subMgr);

712
  testReport.SerializeToFile(rptDirectory + "/ThermalCompartmentTestReport.json");
Aaron Bray's avatar
Aaron Bray committed
713
}