-
Notifications
You must be signed in to change notification settings - Fork 23
/
ModelSystemImpl.cpp
1429 lines (1208 loc) · 50.2 KB
/
ModelSystemImpl.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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// =============================================================================
// CADET
//
// Copyright © 2008-2021: The CADET Authors
// Please see the AUTHORS and CONTRIBUTORS file.
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the GNU Public License v3.0 (or, at
// your option, any later version) which accompanies this distribution, and
// is available at http://www.gnu.org/licenses/gpl.html
// =============================================================================
#include "model/ModelSystemImpl.hpp"
#include "cadet/ParameterProvider.hpp"
#include "cadet/Exceptions.hpp"
#include "cadet/ExternalFunction.hpp"
#include "ConfigurationHelper.hpp"
#include "graph/GraphAlgos.hpp"
#include "SensParamUtil.hpp"
#include "SimulationTypes.hpp"
#include <sstream>
#include <iomanip>
#include <iterator>
#include "LoggingUtils.hpp"
#include "Logging.hpp"
#include "model/ModelSystemImpl-Helper.hpp"
namespace
{
/**
* @brief Find array index of unit operation with given id
* @details Unit operation index does not need to match index of unit operation in an array.
* @param [in] models List of models
* @param [in] unitOpIdx Unit operation index to look for
* @return Array index of the unit operation identified by unit operation index or invalid array index if the unit operation has not been found
*/
inline unsigned int indexOfUnitOp(const std::vector<cadet::IUnitOperation*>& models, unsigned int unitOpIdx)
{
for (std::size_t i = 0; i < models.size(); ++i)
{
if (models[i]->unitOperationId() == unitOpIdx)
return i;
}
return models.size();
}
/**
* @brief Returns whether a given unit operation is a terminal node in the network
* @param [in] conn Connection list
* @param [in] size Number of rows in the connection list
* @param [in] unitOpIdx Index of the unit operation to check
* @return @c true if the unit operation is a terminal node in the network, @c false otherwise
*/
inline bool isTerminal(int const* const conn, unsigned int size, int unitOpIdx)
{
for (unsigned int i = 0; i < size; ++i)
{
if (conn[6*i] == unitOpIdx)
return false;
}
return true;
}
/**
* @brief Returns whether one or more unit operations have multiple ports
* @param [in] models List of unit operation models
* @return @c true if the list contains at least one unit operation model with multiple ports, @c false otherwise
*/
inline bool hasMultiPortUnits(const std::vector<cadet::IUnitOperation*>& models)
{
for (cadet::IUnitOperation const* m : models)
{
if ((m->numInletPorts() > 1) || (m->numOutletPorts() > 1))
return true;
}
return false;
}
template <typename T>
std::string toSciString(const T val, const int prec = 6)
{
std::ostringstream out;
out << std::scientific << std::setprecision(prec) << val;
return out.str();
}
}
namespace cadet
{
namespace model
{
ModelSystem::ModelSystem() : _jacNF(nullptr), _jacFN(nullptr), _jacActiveFN(nullptr), _curSwitchIndex(0), _tempState(nullptr), _initState(0, 0.0), _initStateDot(0, 0.0)
{
}
ModelSystem::~ModelSystem() CADET_NOEXCEPT
{
for (IUnitOperation* model : _models)
delete model;
for (IExternalFunction* extFun : _extFunctions)
delete extFun;
delete[] _tempState;
delete[] _jacNF;
delete[] _jacFN;
delete[] _jacActiveFN;
}
void ModelSystem::addModel(IModel* unitOp)
{
// Check for unique unit operation id
if (indexOfUnitOp(_models, unitOp->unitOperationId()) < _models.size())
throw InvalidParameterException("Cannot add model because of already existing unit operation id " + std::to_string(unitOp->unitOperationId()));
IUnitOperation* const uo = static_cast<IUnitOperation*>(unitOp);
_models.push_back(uo);
if (uo->hasInlet() && uo->hasOutlet())
_inOutModels.push_back(_models.size() - 1);
// Propagate external functions to submodel
uo->setExternalFunctions(_extFunctions.data(), _extFunctions.size());
}
IModel* ModelSystem::getModel(unsigned int index)
{
if (index < _models.size())
return _models[index];
else
return nullptr;
}
IModel const* ModelSystem::getModel(unsigned int index) const
{
if (index < _models.size())
return _models[index];
else
return nullptr;
}
IUnitOperation* ModelSystem::getUnitOperationModel(unsigned int unitOpIdx)
{
for (IUnitOperation* m : _models)
{
if (m->unitOperationId() == unitOpIdx)
return m;
}
return nullptr;
}
IUnitOperation const* ModelSystem::getUnitOperationModel(unsigned int unitOpIdx) const
{
for (IUnitOperation* m : _models)
{
if (m->unitOperationId() == unitOpIdx)
return m;
}
return nullptr;
}
unsigned int ModelSystem::numModels() const CADET_NOEXCEPT
{
return _models.size();
}
void ModelSystem::removeModel(IModel const* unitOp)
{
std::vector<IUnitOperation*>::iterator it = std::find(_models.begin(), _models.end(), static_cast<IUnitOperation const*>(unitOp));
if (it != _models.end())
_models.erase(it);
}
IModel* ModelSystem::removeModel(UnitOpIdx unitOp)
{
for (std::vector<IUnitOperation*>::iterator it = _models.begin(); it != _models.end(); ++it)
{
if ((*it)->unitOperationId() == unitOp)
{
IModel* const mod = static_cast<IModel*>(*it);
_models.erase(it);
return mod;
}
}
return nullptr;
}
UnitOpIdx ModelSystem::maxUnitOperationId() const CADET_NOEXCEPT
{
if (_models.empty())
return UnitOpIndep;
UnitOpIdx id = 0;
for (IUnitOperation* m : _models)
id = std::max(id, m->unitOperationId());
return id;
}
unsigned int ModelSystem::totalNumInletPorts() const CADET_NOEXCEPT
{
unsigned int nPorts = 0;
for (IUnitOperation const* m : _models)
nPorts += m->numInletPorts();
return nPorts;
}
unsigned int ModelSystem::totalNumOutletPorts() const CADET_NOEXCEPT
{
unsigned int nPorts = 0;
for (IUnitOperation const* m : _models)
nPorts += m->numOutletPorts();
return nPorts;
}
unsigned int ModelSystem::addExternalFunction(IExternalFunction& extFun)
{
_extFunctions.push_back(&extFun);
// Propagate external functions to submodels
for (IUnitOperation* m : _models)
m->setExternalFunctions(_extFunctions.data(), _extFunctions.size());
return _extFunctions.size() - 1;
}
IExternalFunction* ModelSystem::getExternalFunction(unsigned int index)
{
if (index < _extFunctions.size())
return _extFunctions[index];
else
return nullptr;
}
IExternalFunction const* ModelSystem::getExternalFunction(unsigned int index) const
{
if (index < _extFunctions.size())
return _extFunctions[index];
else
return nullptr;
}
unsigned int ModelSystem::numExternalFunctions() const CADET_NOEXCEPT
{
return _extFunctions.size();
}
void ModelSystem::removeExternalFunction(IExternalFunction const* extFun)
{
for (std::vector<IExternalFunction*>::iterator it = _extFunctions.begin(); it != _extFunctions.end(); ++it)
{
if (*it == extFun)
{
_extFunctions.erase(it);
break;
}
}
// Update external functions in submodels
for (IUnitOperation* m : _models)
m->setExternalFunctions(_extFunctions.data(), _extFunctions.size());
}
unsigned int ModelSystem::numDofs() const CADET_NOEXCEPT
{
return _dofOffset.back() + numCouplingDOF();
}
unsigned int ModelSystem::numPureDofs() const CADET_NOEXCEPT
{
unsigned int dofs = 0;
for (IUnitOperation* m : _models)
dofs += m->numPureDofs();
return dofs;
}
std::tuple<unsigned int, unsigned int> ModelSystem::getModelStateOffsets(UnitOpIdx unitOp) const CADET_NOEXCEPT
{
for (int i = 0; i < _models.size(); ++i)
{
if (_models[i]->unitOperationId() == unitOp)
return std::make_tuple(_dofOffset[i], _dofOffset[i+1]);
}
return std::make_tuple(-1u, -1u);
}
bool ModelSystem::usesAD() const CADET_NOEXCEPT
{
for (IUnitOperation* m : _models)
{
if (m->usesAD())
return true;
}
return false;
}
/**
* @brief Create data structures to keep track of entries and locations in the state vector
* @details Three data structures are created. One keeps track of the offsets to each unit operation.
* The second keeps track of the size of each unit operation. The third is mapping from
* unit operation and component index to unique inlet DOF index.
*/
void ModelSystem::rebuildInternalDataStructures()
{
// Calculate array with DOF offsets
_dofOffset.clear();
_dofs.clear();
_conDofOffset.clear();
// The additional entry holds the offset for the superstructure
_dofOffset.reserve(_models.size()+1);
_dofs.reserve(_models.size() + 1);
_conDofOffset.reserve(_models.size() + 1);
// Process DOF from models
unsigned int totalDof = 0;
unsigned int conTotalDof = 0;
for (IUnitOperation const* m : _models)
{
_dofOffset.push_back(totalDof);
_conDofOffset.push_back(conTotalDof);
totalDof += m->numDofs();
conTotalDof += m->numInletPorts() * m->numComponents();
_dofs.push_back(m->numDofs());
}
// Process DOF from superstructure
_dofOffset.push_back(totalDof);
_conDofOffset.push_back(conTotalDof);
/*
A mapping is required that turns a local model, port index, and component index into the location of the inlet DOF in
the global state vector. Some unit operations do not have inlet DOFs (e.g., inlet unit operation). Hence,
a map is constructed which converts local indices into global ones.
*/
// Build a mapping (unitOpIdx, PortIdx, CompIdx) -> local coupling DOF index
_couplingIdxMap.clear();
unsigned int counter = 0;
for (unsigned int i = 0; i < numModels(); ++i)
{
IUnitOperation const* const model = _models[i];
//Only unit operations with an inlet have dedicated inlet DOFs
if (model->hasInlet())
{
for (unsigned int port = 0; port < model->numInletPorts(); ++port)
{
for (unsigned int comp = 0; comp < model->numComponents(); ++comp)
{
_couplingIdxMap.insert({ std::make_tuple(i, port, comp), counter });
++counter;
}
}
}
}
_dofs.push_back(numCouplingDOF());
// Allocate error indicator vector
_errorIndicator.resize(_models.size(), 0);
LOG(Debug) << "DOF offsets: " << _dofOffset;
}
/**
* @brief Allocates memory for the superstructure matrices
* @details How many connections each unit has determines how much memory has to be allocated for the coupling matrices.
* This function walks the connections over the entire simulation in order to determine the maximum number of
* connections over the whole simulation which governs the number of entries in the sparse superstructure coupling
* matrices. Finally, the required memory is allocated in the matrices.
*/
void ModelSystem::allocateSuperStructMatrices()
{
// Step 1: Calculate number of connections per unit operation per valve switch
// We record the number of outgoing connections (i.e., components) which act as
// sources in the bottom macro-row of the superstructure
const unsigned int nModels = numModels();
std::vector<unsigned int> sourcesPerUnitOpPerConfig(nModels * _switchSectionIndex.size(), 0u);
for (std::size_t idx = 0; idx < _switchSectionIndex.size(); ++idx)
{
int const* ptrConn = _connections[idx];
for (unsigned int i = 0; i < _connections.sliceSize(idx) / 6; ++i, ptrConn += 6)
{
// Extract current connection
const int uoSource = ptrConn[0];
const int portSource = ptrConn[2];
const int compSource = ptrConn[4];
IUnitOperation const* const model = _models[uoSource];
if (portSource == -1)
{
if (compSource == -1)
sourcesPerUnitOpPerConfig[nModels * idx + uoSource] += model->numOutletPorts() * model->numComponents();
else
sourcesPerUnitOpPerConfig[nModels * idx + uoSource] += model->numOutletPorts();
}
else
{
if (compSource == -1)
sourcesPerUnitOpPerConfig[nModels * idx + uoSource] += model->numComponents();
else
sourcesPerUnitOpPerConfig[nModels * idx + uoSource] += 1;
}
}
}
// Step 2: Take maximum over valve switches to obtain maximum number of matrix entries per unit operation
std::vector<unsigned int> numOutgoing(nModels, 0u);
// Assign the first row of sourcesPerUnitOpPerConfig to the maximum number of outputs
std::copy(sourcesPerUnitOpPerConfig.begin(), sourcesPerUnitOpPerConfig.begin() + nModels, numOutgoing.begin());
// Loop over remaining lines and take maximum per element
for (std::size_t sectionIdx = 1; sectionIdx < _switchSectionIndex.size(); ++sectionIdx)
{
for (unsigned int i = 0; i < nModels; ++i)
{
numOutgoing[i] = std::max(numOutgoing[i], sourcesPerUnitOpPerConfig[nModels * sectionIdx + i]);
}
}
// Step 3: Allocate memory based on maximum number of connections for each unit operation
for (unsigned int i = 0; i < numModels(); ++i)
{
// Bottom macro-row
_jacActiveFN[i].resize(numOutgoing[i]);
// Right macro-column
// Each unit operation has inlets equal to numComponents * numInletPorts so long as the unit operation has an inlet
IUnitOperation const* const model = _models[i];
if (model->hasInlet())
_jacNF[i].resize(model->numComponents() * model->numInletPorts());
}
}
bool ModelSystem::configureModelDiscretization(IParameterProvider& paramProvider, IConfigHelper& helper)
{
// Discretizations of unit operation models are already configured
rebuildInternalDataStructures();
_parameters.clear();
paramProvider.pushScope("solver");
readLinearSolutionMode(paramProvider);
paramProvider.popScope();
configureSwitches(paramProvider);
_curSwitchIndex = 0;
// Allocate memory to coupling matrices
_jacActiveFN = new linalg::SparseMatrix<active>[numModels()];
_jacNF = new linalg::SparseMatrix<double>[numModels()];
_jacFN = new linalg::SparseMatrix<double>[numModels()];
// Calculate the sizes that need to be allocated for all the inlets and outlets
allocateSuperStructMatrices();
// Create and configure all external functions
bool success = true;
if (paramProvider.exists("external"))
{
paramProvider.pushScope("external");
unsigned int i = 0;
std::ostringstream oss;
oss << "source_" << std::setfill('0') << std::setw(3) << std::setprecision(0) << i;
while (paramProvider.exists(oss.str()))
{
// Create and configure unit operation
paramProvider.pushScope(oss.str());
const std::string extType = paramProvider.getString("EXTFUN_TYPE");
IExternalFunction* const func = helper.createExternalFunction(extType);
if (func)
{
const bool confSuccess = func->configure(¶mProvider);
if (confSuccess)
_extFunctions.push_back(func);
else
{
// Ignore the external function and delete this instance
_extFunctions.push_back(nullptr);
delete func;
success = false;
LOG(Error) << "Failed to configure external source " << i << " (" << extType << "), source is ignored";
}
}
else
{
// Unknown type of external function
_extFunctions.push_back(nullptr);
success = false;
LOG(Error) << "Failed to create external source " << i << " as type " << extType << " is unknown, source is ignored";
}
paramProvider.popScope();
// Next group in file format
++i;
oss.str("");
oss << "source_" << std::setfill('0') << std::setw(3) << std::setprecision(0) << i;
}
paramProvider.popScope();
}
// Propagate external functions to submodels
for (IUnitOperation* m : _models)
m->setExternalFunctions(_extFunctions.data(), _extFunctions.size());
// Read solver settings
paramProvider.pushScope("solver");
const int maxKrylov = paramProvider.getInt("MAX_KRYLOV");
const int gsType = paramProvider.getInt("GS_TYPE");
const int maxRestarts = paramProvider.getInt("MAX_RESTARTS");
_schurSafety = paramProvider.getDouble("SCHUR_SAFETY");
paramProvider.popScope();
// Initialize and configure GMRES for solving the Schur-complement
_gmres.initialize(numCouplingDOF(), maxKrylov, linalg::toOrthogonalization(gsType), maxRestarts);
// Allocate tempState vector
delete[] _tempState;
_tempState = new double[numDofs()];
// _tempSchur = new double[*std::max_element(_dofs.begin(), _dofs.end())];
_flowRateIn.reserve(totalNumInletPorts(), _models.size());
_flowRateOut.reserve(totalNumOutletPorts(), _models.size());
_totalInletFlow.reserve(totalNumInletPorts(), _models.size());
_totalInletFlowLin.reserve(totalNumInletPorts(), _models.size());
_totalInletFlowQuad.reserve(totalNumInletPorts(), _models.size());
_totalInletFlowCub.reserve(totalNumInletPorts(), _models.size());
_totalOutletFlow.reserve(totalNumOutletPorts(), _models.size());
_totalOutletFlowLin.reserve(totalNumOutletPorts(), _models.size());
_totalOutletFlowQuad.reserve(totalNumOutletPorts(), _models.size());
_totalOutletFlowCub.reserve(totalNumOutletPorts(), _models.size());
for (IUnitOperation const* m : _models)
{
_flowRateIn.pushBackSlice(m->numInletPorts());
_totalInletFlow.pushBackSlice(m->numInletPorts());
_totalInletFlowLin.pushBackSlice(m->numInletPorts());
_totalInletFlowQuad.pushBackSlice(m->numInletPorts());
_totalInletFlowCub.pushBackSlice(m->numInletPorts());
_flowRateOut.pushBackSlice(m->numOutletPorts());
_totalOutletFlow.pushBackSlice(m->numOutletPorts());
_totalOutletFlowLin.pushBackSlice(m->numOutletPorts());
_totalOutletFlowQuad.pushBackSlice(m->numOutletPorts());
_totalOutletFlowCub.pushBackSlice(m->numOutletPorts());
}
assembleRightMacroColumn();
#ifdef CADET_DEBUG
LOG(Debug) << "Num units " << _models.size() << " max ID " << maxUnitOperationId() << " total DOF " << numDofs();
LOG(Debug) << "Unit op state vector size: uoDOFs = " << log::VectorPtr<unsigned int>(_dofs.data(), _dofs.size());
LOG(Debug) << "Unit op state vector offsets: uoOffset = " << log::VectorPtr<unsigned int>(_dofOffset.data(), _dofOffset.size());
#endif
return success;
}
bool ModelSystem::configure(IParameterProvider& paramProvider)
{
_parameters.clear();
// Reconfigure all external functions
bool success = true;
if (paramProvider.exists("external"))
{
paramProvider.pushScope("external");
std::ostringstream oss;
for (std::size_t i = 0; i < _extFunctions.size(); ++i)
{
IExternalFunction* const func = _extFunctions[i];
if (!func)
continue;
oss.str("");
oss << "source_" << std::setfill('0') << std::setw(3) << std::setprecision(0) << i;
if (!paramProvider.exists(oss.str()))
continue;
paramProvider.pushScope(oss.str());
const bool localSuccess = func->configure(¶mProvider);
paramProvider.popScope();
if (!localSuccess)
{
LOG(Error) << "Failed to reconfigure external source " << i;
}
success = localSuccess && success;
}
paramProvider.popScope();
}
// Reconfigure solver settings
paramProvider.pushScope("solver");
const int gsType = paramProvider.getInt("GS_TYPE");
const int maxRestarts = paramProvider.getInt("MAX_RESTARTS");
_schurSafety = paramProvider.getDouble("SCHUR_SAFETY");
readLinearSolutionMode(paramProvider);
paramProvider.popScope();
_gmres.orthoMethod(linalg::toOrthogonalization(gsType));
_gmres.maxRestarts(maxRestarts);
// Reconfigure switches
configureSwitches(paramProvider);
return success;
}
/**
* @brief Reads valve switches from the given parameter provider
* @param [in] paramProvider Parameter provider
*/
void ModelSystem::configureSwitches(IParameterProvider& paramProvider)
{
// Read connections of unit operations
paramProvider.pushScope("connections");
const unsigned int numSwitches = paramProvider.getInt("NSWITCHES");
// TODO Improve those very conservative size estimates
_switchSectionIndex.clear();
_switchSectionIndex.reserve(numSwitches);
_connections.clear();
_connections.reserve(numSwitches * 6 * _models.size() * _models.size(), numSwitches);
_flowRates.clear();
_flowRates.reserve(numSwitches * _models.size() * _models.size(), numSwitches);
_linearModelOrdering.reserve(numSwitches * _models.size(), numSwitches);
_linearModelOrdering.clear();
#if CADET_COMPILER_CXX_CONSTEXPR
constexpr StringHash flowHash = hashString("CONNECTION");
constexpr StringHash flowHashLin = hashString("CONNECTION_LIN");
constexpr StringHash flowHashQuad = hashString("CONNECTION_QUAD");
constexpr StringHash flowHashCub = hashString("CONNECTION_CUBE");
#else
const StringHash flowHash = hashString("CONNECTION");
const StringHash flowHashLin = hashString("CONNECTION_LIN");
const StringHash flowHashQuad = hashString("CONNECTION_QUAD");
const StringHash flowHashCub = hashString("CONNECTION_CUBE");
#endif
// Default: ports are not given in connection list
bool conListHasPorts = false;
// Override default by user option
if (paramProvider.exists("CONNECTIONS_INCLUDE_PORTS"))
conListHasPorts = paramProvider.getBool("CONNECTIONS_INCLUDE_PORTS");
// If we have unit operations with multiple ports, we require ports in connection list
if (hasMultiPortUnits(_models))
conListHasPorts = true;
// Default: no dynamic flow rates
_hasDynamicFlowRates = false;
// Override default by user option
if (paramProvider.exists("CONNECTIONS_INCLUDE_DYNAMIC_FLOW"))
_hasDynamicFlowRates = paramProvider.getBool("CONNECTIONS_INCLUDE_DYNAMIC_FLOW");
std::ostringstream oss;
for (unsigned int i = 0; i < numSwitches; ++i)
{
oss.str("");
oss << "switch_" << std::setfill('0') << std::setw(3) << std::setprecision(0) << i;
paramProvider.pushScope(oss.str());
_switchSectionIndex.push_back(paramProvider.getInt("SECTION"));
if ((i > 0) && (_switchSectionIndex.back() <= _switchSectionIndex[_switchSectionIndex.size() - 2]))
throw InvalidParameterException("SECTION index has to be monotonically increasing (switch " + std::to_string(i) + ")");
std::vector<double> connFlow = paramProvider.getDoubleArray("CONNECTIONS");
if (!conListHasPorts)
{
if (_hasDynamicFlowRates)
{
if ((connFlow.size() % 8) != 0)
throw InvalidParameterException("CONNECTIONS matrix has to have 8 columns if CONNECTIONS_INCLUDE_PORTS is disabled and CONNECTIONS_INCLUDE_DYNAMIC_FLOW is enabled");
}
else
{
if ((connFlow.size() % 5) != 0)
throw InvalidParameterException("CONNECTIONS matrix has to have 5 columns if CONNECTIONS_INCLUDE_PORTS is disabled and CONNECTIONS_INCLUDE_DYNAMIC_FLOW is disabled");
}
addDefaultPortsToConnectionList(connFlow);
}
if (!_hasDynamicFlowRates)
{
if ((connFlow.size() % 7) != 0)
throw InvalidParameterException("CONNECTIONS matrix has to have 5 or 7 columns if CONNECTIONS_INCLUDE_DYNAMIC_FLOW is disabled");
addDefaultDynamicFlowRatesToConnectionList(connFlow);
}
if ((connFlow.size() % 10) != 0)
throw InvalidParameterException("CONNECTIONS matrix has to have 10 columns");
std::vector<int> conn(connFlow.size() / 10 * 6, 0);
std::vector<double> fr(connFlow.size() / 10, 0.0);
std::vector<double> frLin(connFlow.size() / 10, 0.0);
std::vector<double> frQuad(connFlow.size() / 10, 0.0);
std::vector<double> frCub(connFlow.size() / 10, 0.0);
checkConnectionList(connFlow, conn, fr, frLin, frQuad, frCub, i);
_connections.pushBackSlice(conn);
// Convert double to active while pushing into the SlicedVector
// also register parameter to enable sensitivities
if (fr.size() > 0)
{
_flowRates.pushBack(fr[0]);
_flowRatesLin.pushBack(frLin[0]);
_flowRatesQuad.pushBack(frQuad[0]);
_flowRatesCub.pushBack(frCub[0]);
_parameters[makeParamId(flowHash, UnitOpIndep, conn[2], conn[3], conn[0], conn[1], _switchSectionIndex.back())] = _flowRates.back();
_parameters[makeParamId(flowHashLin, UnitOpIndep, conn[2], conn[3], conn[0], conn[1], _switchSectionIndex.back())] = _flowRatesLin.back();
_parameters[makeParamId(flowHashQuad, UnitOpIndep, conn[2], conn[3], conn[0], conn[1], _switchSectionIndex.back())] = _flowRatesQuad.back();
_parameters[makeParamId(flowHashCub, UnitOpIndep, conn[2], conn[3], conn[0], conn[1], _switchSectionIndex.back())] = _flowRatesCub.back();
for (std::size_t j = 1; j < fr.size(); ++j)
{
_flowRates.pushBackInLastSlice(fr[j]);
_flowRatesLin.pushBackInLastSlice(frLin[j]);
_flowRatesQuad.pushBackInLastSlice(frQuad[j]);
_flowRatesCub.pushBackInLastSlice(frCub[j]);
// Check if a previous identical connection (except for component indices) exists
bool found = false;
for (unsigned int k = 0; k < j; ++k)
{
if ((conn[6*k] == conn[6*j]) && (conn[6*k+1] == conn[6*j+1]) && (conn[6*k+2] == conn[6*j+2]) && (conn[6*k+3] == conn[6*j+3]))
{
found = true;
break;
}
}
// Only register the first occurrence of a flow parameter
if (!found)
{
_parameters[makeParamId(flowHash, UnitOpIndep, conn[6*j+2], conn[6*j+3], conn[6*j], conn[6*j+1], _switchSectionIndex.back())] = (_flowRates.back() + j);
_parameters[makeParamId(flowHashLin, UnitOpIndep, conn[6*j+2], conn[6*j+3], conn[6*j], conn[6*j+1], _switchSectionIndex.back())] = (_flowRatesLin.back() + j);
_parameters[makeParamId(flowHashQuad, UnitOpIndep, conn[6*j+2], conn[6*j+3], conn[6*j], conn[6*j+1], _switchSectionIndex.back())] = (_flowRatesQuad.back() + j);
_parameters[makeParamId(flowHashCub, UnitOpIndep, conn[6*j+2], conn[6*j+3], conn[6*j], conn[6*j+1], _switchSectionIndex.back())] = (_flowRatesCub.back() + j);
}
}
}
else
{
// Add empty slice
_flowRates.pushBackSlice(nullptr, 0);
_flowRatesLin.pushBackSlice(nullptr, 0);
_flowRatesQuad.pushBackSlice(nullptr, 0);
_flowRatesCub.pushBackSlice(nullptr, 0);
}
if (_linearSolutionMode == 1)
{
// Parallel solution method
_linearModelOrdering.pushBackSlice(0);
LOG(Debug) << "Select parallel solution method for switch " << i;
}
else if (_linearSolutionMode == 2)
{
// Sequential solution method
const util::SlicedVector<int> adjList = graph::adjacencyListFromConnectionList(conn.data(), _models.size(), conn.size() / 6);
std::vector<int> topoOrder;
const bool hasCycles = graph::topologicalSort(adjList, topoOrder);
if (hasCycles)
{
LOG(Warning) << "Detected cycle in connections of switch " << i << ", reverting to parallel solution method";
_linearModelOrdering.pushBackSlice(0);
}
else
{
_linearModelOrdering.pushBackSlice(topoOrder);
LOG(Debug) << "Select sequential solution method for switch " << i;
LOG(Debug) << "Reversed ordering: " << topoOrder;
}
}
else
{
// Auto detect solution method
// TODO: Add heuristic for number and complexity of unit operations
// Simple heuristic: At least 6 models (regardless of complexity) => Parallelize
if (_models.size() >= 6)
{
_linearModelOrdering.pushBackSlice(0);
LOG(Debug) << "Select parallel solution method for switch " << i << " (at least 6 models)";
}
else
{
// Less than 6 models => Select depending on existence of cycles
const util::SlicedVector<int> adjList = graph::adjacencyListFromConnectionList(conn.data(), _models.size(), conn.size() / 6);
std::vector<int> topoOrder;
const bool hasCycles = graph::topologicalSort(adjList, topoOrder);
if (hasCycles)
{
_linearModelOrdering.pushBackSlice(0);
LOG(Debug) << "Select parallel solution method for switch " << i << " (cycles found)";
}
else
{
_linearModelOrdering.pushBackSlice(topoOrder);
LOG(Debug) << "Select sequential solution method for switch " << i << " (no cycles found, less than 6 models)";
LOG(Debug) << "Reversed ordering: " << topoOrder;
}
}
}
paramProvider.popScope();
}
paramProvider.popScope();
if (_switchSectionIndex[0] != 0)
throw InvalidParameterException("First element of SECTION in connections group has to be 0");
}
/**
* @brief Add default ports to connection list
* @details Adds source and destination ports of @c -1 to the connection list. The list is
* overwritten, that is, all pointers and iterators are invalidated.
* @param [in,out] conList On entry, list of connections without ports;
* on exit, list of connections including default ports
*/
void ModelSystem::addDefaultPortsToConnectionList(std::vector<double>& conList) const
{
const int strideSrc = _hasDynamicFlowRates ? 8 : 5;
const int strideDest = _hasDynamicFlowRates ? 10 : 7;
const int numRows = conList.size() / strideSrc;
std::vector<double> newList(numRows * strideDest, -1.0);
double const* src = conList.data();
double* dest = newList.data();
for (int i = 0; i < numRows; ++i, src += strideSrc, dest += strideDest)
{
// Always copy unit operation indices
dest[0] = src[0];
dest[1] = src[1];
// Skip ports (default filled with -1)
// Copy the rest of the line
std::copy_n(src + 2, strideDest - 4, dest + 4);
}
conList = std::move(newList);
}
/**
* @brief Add default dynamic flow rates to connection list
* @details Adds linear, quadratic, and cubic flow rate coefficients to the list
* The list is overwritten, that is, all pointers and iterators are invalidated.
* @param [in,out] conList On entry, list of connections without dynamic flow rates;
* on exit, list of connections including dynamic flow rates
*/
void ModelSystem::addDefaultDynamicFlowRatesToConnectionList(std::vector<double>& conList) const
{
const int numRows = conList.size() / 7;
std::vector<double> newList(numRows * 10, 0.0);
double const* src = conList.data();
double* dest = newList.data();
for (int i = 0; i < numRows; ++i, src += 7, dest += 10)
{
std::copy_n(src, 7, dest);
}
conList = std::move(newList);
}
bool ModelSystem::configureModel(IParameterProvider& paramProvider, unsigned int unitOpIdx)
{
IUnitOperation* const model = getUnitOperationModel(unitOpIdx);
if (!model)
return false;
return model->configure(paramProvider);
}
void ModelSystem::readLinearSolutionMode(IParameterProvider& paramProvider)
{
// Default: automatic
_linearSolutionMode = 0;
// Override default by user option
if (paramProvider.exists("LINEAR_SOLUTION_MODE"))
_linearSolutionMode = paramProvider.getInt("LINEAR_SOLUTION_MODE");
}
/**
* @brief Checks the given unit operation connection list and reformats it
* @details Throws an exception if something is incorrect. Reformats the connection list by
* substituting unit operation IDs with local indices.
* @param [in] conn Matrix with 7 columns holding all connections. The matrix is expected
* to be in row-major storage format.
* @param [out] connOnly Matrix with 6 columns holding all connections. While @p conn contains
* connection indices and flow rate, this matrix only holds connection indices.
* It is assumed to be pre-allocated (same number of rows as @p conn). The unit
* operation IDs are substituted by the corresponding indices of the unit operations
* in the local _models vector.
* @param [out] flowRates Vector with flow rates for each connection in the list. It is assumed
* to be pre-allocated (same number of rows as @p conn).
* @param [out] flowRatesLin Vector with linear flow rate coefficients for each connection in the list. It is assumed
* to be pre-allocated (same number of rows as @p conn).
* @param [out] flowRatesQuad Vector with quadratic flow rate coefficients for each connection in the list. It is assumed
* to be pre-allocated (same number of rows as @p conn).
* @param [out] flowRatesCub Vector with cubic flow rate coefficients for each connection in the list. It is assumed
* to be pre-allocated (same number of rows as @p conn).
* @param [in] idxSwitch Index of the valve switch that corresponds to this connection list
*/
void ModelSystem::checkConnectionList(const std::vector<double>& conn, std::vector<int>& connOnly, std::vector<double>& flowRates, std::vector<double>& flowRatesLin, std::vector<double>& flowRatesQuad, std::vector<double>& flowRatesCub, unsigned int idxSwitch) const
{
std::vector<double> totalInflow(_models.size(), 0.0);
std::vector<double> totalInflowLin(_models.size(), 0.0);
std::vector<double> totalInflowQuad(_models.size(), 0.0);
std::vector<double> totalInflowCub(_models.size(), 0.0);
std::vector<double> totalOutflow(_models.size(), 0.0);
std::vector<double> totalOutflowLin(_models.size(), 0.0);
std::vector<double> totalOutflowQuad(_models.size(), 0.0);
std::vector<double> totalOutflowCub(_models.size(), 0.0);
for (std::size_t i = 0; i < conn.size() / 10; ++i)
{
// Extract current connection
int uoSource = static_cast<int>(conn[10*i]);
int uoDest = static_cast<int>(conn[10*i+1]);
const int portSource = static_cast<int>(conn[10*i+2]);
const int portDest = static_cast<int>(conn[10*i+3]);
const int compSource = static_cast<int>(conn[10*i+4]);
const int compDest = static_cast<int>(conn[10*i+5]);
double fr = conn[10*i + 6];
double frLin = conn[10*i + 7];
double frQuad = conn[10*i + 8];
double frCub = conn[10*i + 9];
if (uoSource < 0)
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Source unit operation id has to be at least 0 in connection");
if (uoDest < 0)
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Destination unit operation id has to be at least 0 in connection");
// Convert to index
uoSource = indexOfUnitOp(_models, uoSource);
uoDest = indexOfUnitOp(_models, uoDest);
if (static_cast<unsigned int>(uoSource) >= _models.size())
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Source unit operation id " + std::to_string(uoSource) + " not found in connection");
if (static_cast<unsigned int>(uoDest) >= _models.size())
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Destination unit operation id " + std::to_string(uoDest) + " not found in connection");
// Check if unit operations have inlets and outlets
if (!_models[uoSource]->hasOutlet())
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Source unit operation " + std::to_string(_models[uoSource]->unitOperationId()) + " does not have an outlet");
if (!_models[uoDest]->hasInlet())
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Source unit operation " + std::to_string(_models[uoDest]->unitOperationId()) + " does not have an inlet");
// Check port indices
if ((portSource >= 0) && (static_cast<unsigned int>(portSource) >= _models[uoSource]->numOutletPorts()))
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Source port index (" + std::to_string(portSource) + ") exceeds number of outlet ports (" + std::to_string(_models[uoSource]->numOutletPorts()) + ")");
if ((portDest >= 0) && (static_cast<unsigned int>(portDest) >= _models[uoDest]->numInletPorts()))
throw InvalidParameterException("In CONNECTIONS matrix (switch " + std::to_string(idxSwitch) + " row " + std::to_string(i) + "): Destination port index (" + std::to_string(portDest) + ") exceeds number of inlet ports (" + std::to_string(_models[uoDest]->numInletPorts()) + ")");