-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodel.cpp
More file actions
966 lines (738 loc) · 30.7 KB
/
model.cpp
File metadata and controls
966 lines (738 loc) · 30.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
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
/*
* This file is part of the ResOpt project.
*
* Copyright (C) 2011-2012 Aleksander O. Juell <aleksander.juell@ntnu.no>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "model.h"
#include <iostream>
#include "reservoir.h"
#include "well.h"
#include "capacity.h"
#include "intvariable.h"
#include "realvariable.h"
#include "binaryvariable.h"
#include "constraint.h"
#include "objective.h"
#include "cost.h"
#include "midpipe.h"
#include "endpipe.h"
#include "separator.h"
#include "pressurebooster.h"
#include "stream.h"
#include "productionwell.h"
#include "pipeconnection.h"
#include "userconstraint.h"
#include "logger.h"
using std::cout;
using std::endl;
namespace ResOpt
{
Model::Model()
: p_reservoir(0),
p_obj(0),
m_up_to_date(false)
{
p_logger = new Logger(Logger::DELEGATE);
}
//-----------------------------------------------------------------------------------------------
// Copy constructor
//-----------------------------------------------------------------------------------------------
Model::Model(const Model &m)
{
// logger
p_logger = new Logger(Logger::DELEGATE);
// setting the up to date status
m_up_to_date = false;
// copying driver path
m_driver_path = m.m_driver_path;
// copying the reservoir
p_reservoir = new Reservoir(*m.reservoir());
// copying the wells
for(int i = 0; i < m.numberOfWells(); i++)
{
m_wells.push_back(m.well(i)->clone());
}
// copying the pipes
for(int i = 0; i < m.numberOfPipes(); i++)
{
m_pipes.push_back(m.pipe(i)->clone());
}
// copying the capacity constraints
for(int i = 0; i < m.numberOfCapacities(); i++)
{
m_capacities.push_back(new Capacity(*m.m_capacities.at(i)));
}
// copying the objective
p_obj = m.p_obj->clone();
// copying the master schedule
m_master_schedule = m.m_master_schedule;
// copying the user defined constraints
for(int i = 0; i < m.m_user_constraints.size(); ++i)
{
m_user_constraints.push_back(new UserConstraint(*m.m_user_constraints.at(i), this));
}
}
Model::~Model()
{
delete p_logger;
if(p_reservoir != 0) delete p_reservoir;
for(int i = 0; i < m_wells.size(); i++) delete m_wells.at(i);
for(int i = 0; i < m_pipes.size(); i++) delete m_pipes.at(i);
for(int i = 0; i < m_capacities.size(); i++) delete m_capacities.at(i);
for(int i = 0; i < m_user_constraints.size(); ++i) delete m_user_constraints.at(i);
}
//-----------------------------------------------------------------------------------------------
// Validates the Model
//-----------------------------------------------------------------------------------------------
bool Model::validate()
{
bool ok = true;
// first checking that the reservoir is defined
if(p_reservoir == 0)
{
cout << endl << "### Model Validation Error ###" << endl
<< "No RESERVOIR defined..." << endl << endl;
ok = false;
}
// checking that at least one well is defined
if(numberOfWells() == 0)
{
cout << endl << "### Model Validation Error ###" << endl
<< "No WELL defined..." << endl << endl;
ok = false;
}
// checking that at least one pipe is defined
if(numberOfPipes() == 0)
{
cout << endl << "### Model Validation Error ###" << endl
<< "No PIPE defined..." << endl << endl;
ok = false;
}
// checking that the master schedule at least containts one entry
if(numberOfMasterScheduleTimes() < 1)
{
cout << endl << "### Model Validation Error ###" << endl
<< "MASTERSCHEDULE does not containt any entries..." << endl << endl;
ok = false;
}
// checking that the master schedule corresponds to all well schedules
for(int i = 0; i < numberOfWells(); ++i)
{
// first checking that the schedules have the same size
if(well(i)->numberOfControls() != numberOfMasterScheduleTimes())
{
cout << endl << "### Model Validation Error ###" << endl
<< "Well: " << well(i)->name().toLatin1().constData() << endl
<< "Does not have the same number of SHEDULE entries as the MASTERSCHEDULE..." << endl << endl;
ok = false;
break;
}
else
{
// checking that each time entry in the well is the same as in the master schedule
for(int j = 0; j < numberOfMasterScheduleTimes(); ++j)
{
if(well(i)->control(j)->endTime() != masterScheduleTime(j))
{
cout << endl << "### Model Validation Error ###" << endl
<< "Well: " << well(i)->name().toLatin1().constData() << endl
<< "SHEDULE entry: " << well(i)->control(j)->endTime() << endl
<< "Is not found in the MASTERSCHEDULE..." << endl << endl;
ok = false;
break;
}
} // master schedule entries
}
} // well
// checking that the master schedule corresponds to the gas lift schedule for the production wells that have gas lift
for(int i = 0; i < numberOfWells(); ++i)
{
// checking if this is a production well
ProductionWell *prod_well = dynamic_cast<ProductionWell*>(well(i));
if(prod_well != 0)
{
// checking if this production well has gas lift
if(prod_well->hasGasLift())
{
// first checking that the schedules have the same size
if(prod_well->numberOfGasLiftControls() != numberOfMasterScheduleTimes())
{
cout << endl << "### Model Validation Error ###" << endl
<< "Well: " << prod_well->name().toLatin1().constData() << endl
<< "Does not have the same number of GASLIFT entries as the MASTERSCHEDULE..." << endl << endl;
ok = false;
break;
}
else
{
// checking that each time entry in the well is the same as in the master schedule
for(int j = 0; j < numberOfMasterScheduleTimes(); ++j)
{
if(prod_well->gasLiftControl(j)->endTime() != masterScheduleTime(j))
{
cout << endl << "### Model Validation Error ###" << endl
<< "Well: " << prod_well->name().toLatin1().constData() << endl
<< "GASLIFT entry: " << prod_well->gasLiftControl(j)->endTime() << endl
<< "Is not found in the MASTERSCHEDULE..." << endl << endl;
ok = false;
break;
}
} // master schedule entries
}
} // has gas lift
} // production well
} // well
return ok;
}
//-----------------------------------------------------------------------------------------------
// Connects wells and pipes to the outlets
//-----------------------------------------------------------------------------------------------
bool Model::resolvePipeRouting()
{
cout << "Resolving the pipe routing..." << endl;
bool ok = true;
// first cleaning up the current feeds connected to the pipes
for(int k = 0; k < m_pipes.size(); ++k) m_pipes.at(k)->cleanFeedConnections();
// connecting the wells
for(int i = 0; i < m_wells.size(); ++i) // looping through each well
{
// checking if this is a production well
ProductionWell *prod_well = dynamic_cast<ProductionWell*>(well(i));
if(prod_well != 0) // skipping injection wells
{
// looping through the pipe connections for the well
for(int k = 0; k < prod_well->numberOfPipeConnections(); ++k)
{
bool connection_ok;
int pipe_num = prod_well->pipeConnection(k)->pipeNumber();
for(int j = 0; j < m_pipes.size(); j++) // looping through the pipes to find the correct one
{
if(m_pipes.at(j)->number() == pipe_num) // found the correct pipe
{
m_pipes.at(j)->addFeedWell(prod_well); // adding the well as a feed to the pipe
prod_well->pipeConnection(k)->setPipe(m_pipes.at(j)); // setting the pipe as outlet pipe for the pipe connection
connection_ok = true;
break;
}
} // pipe for pipe connection k
// checking if the well - pipe connection was ok
if(!connection_ok)
{
cout << endl << "### Runtime Error ###" << endl
<< "Well to Pipe connection could not be established..." << endl
<< "PIPE: " << pipe_num << endl
<< "WELL: " << prod_well->name().toLatin1().constData() << endl;
exit(1);
}
} // pipe connections for well i
// checking that the well is at least connected to one pipe
if(prod_well->numberOfPipeConnections() == 0)
{
cout << endl << "### Runtime Error ###" << endl
<< "Well " << prod_well->name().toLatin1().constData() << endl
<< "Is not connected to any pipe..." << endl << endl;
exit(1);
}
}
}
// connecting the pipes
for(int i = 0; i < m_pipes.size(); i++) // looping through the pipes
{
// only the MidPipe, Separator, and PressureBooster types should be checked
MidPipe *p_mid = dynamic_cast<MidPipe*>(m_pipes.at(i));
Separator *p_sep = dynamic_cast<Separator*>(m_pipes.at(i));
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(m_pipes.at(i));
if(p_mid != 0) // this is a MidPipe
{
// looping through the outlet connections for the pipe
for(int k = 0; k < p_mid->numberOfOutletConnections(); k++)
{
bool pipe_ok = false;
int pipe_num = p_mid->outletConnection(k)->pipeNumber();
// checking if it is connected to it self
if(pipe_num == p_mid->number())
{
cout << endl << "### Runtime Error ###" << endl
<< "Pipe " << pipe_num << " is connected to itself!" << endl
<< "OUTLETPIPE Connection: " << k << endl << endl;
exit(1);
}
// looping through the pipes to find the correct one
for(int j = 0; j < numberOfPipes(); j++)
{
if(pipe_num == pipe(j)->number())
{
p_mid->outletConnection(k)->setPipe(pipe(j));
pipe(j)->addFeedPipe(p_mid);
pipe_ok = true;
break;
}
}
// checking if the pipe - pipe connection was ok
if(!pipe_ok)
{
cout << endl << "### Runtime Error ###" << endl
<< "Pipe to Pipe connection could not be established..." << endl
<< "UPSTREAM PIPE: " << pipe_num << endl
<< "DOWNSTREAM PIPE: " << pipe(i)->number() << endl;
exit(1);
}
} // connection k
// checking that the pipe has at least one outlet connection
if(p_mid->numberOfOutletConnections() == 0)
{
cout << endl << "### Runtime Error ###" << endl
<< "Pipe " << p_mid->number() << endl
<< "Is not connected to any upstream pipe..." << endl << endl;
exit(1);
}
} // midpipe
else if(p_sep != 0) // this is a Separator
{
bool pipe_ok = false;
int pipe_num = p_sep->outletConnection()->pipeNumber();
// checking if it is connected to it self
if(pipe_num == p_sep->number())
{
cout << endl << "### Runtime Error ###" << endl
<< "Separator " << pipe_num << " is connected to itself!" << endl << endl;
exit(1);
}
// looping through the pipes to find the correct one
for(int j = 0; j < numberOfPipes(); j++)
{
if(pipe_num == pipe(j)->number())
{
p_sep->outletConnection()->setPipe(pipe(j));
pipe(j)->addFeedPipe(p_sep);
pipe_ok = true;
break;
}
}
// checking if the pipe - pipe connection was ok
if(!pipe_ok)
{
cout << endl << "### Runtime Error ###" << endl
<< "Separator to Pipe connection could not be established..." << endl
<< "SEPARATOR: " << pipe_num << endl
<< "DOWNSTREAM PIPE: " << pipe(i)->number() << endl;
exit(1);
}
} // separator
else if(p_boost != 0) // this is a PressureBooster
{
bool pipe_ok = false;
int pipe_num = p_boost->outletConnection()->pipeNumber();
// checking if it is connected to it self
if(pipe_num == p_boost->number())
{
cout << endl << "### Runtime Error ###" << endl
<< "Booster " << pipe_num << " is connected to itself!" << endl << endl;
exit(1);
}
// looping through the pipes to find the correct one
for(int j = 0; j < numberOfPipes(); ++j)
{
if(pipe_num == pipe(j)->number())
{
p_boost->outletConnection()->setPipe(pipe(j));
pipe(j)->addFeedPipe(p_boost);
pipe_ok = true;
break;
}
}
// checking if the pipe - pipe connection was ok
if(!pipe_ok)
{
cout << endl << "### Runtime Error ###" << endl
<< "Booster to Pipe connection could not be established..." << endl
<< "BOOSTER: " << pipe_num << endl
<< "DOWNSTREAM PIPE: " << pipe(i)->number() << endl;
exit(1);
}
} // booster
} // pipe i
return ok;
}
//-----------------------------------------------------------------------------------------------
// Calculates the pressures in all the pipes
//-----------------------------------------------------------------------------------------------
bool Model::calculatePipePressures()
{
bool ok = true;
// finding the endnodes
QVector<EndPipe*> end_pipes;
for(int i = 0; i < numberOfPipes(); i++)
{
EndPipe *p = dynamic_cast<EndPipe*>(pipe(i)); // trying to cast as EndPipe
if(p != 0) end_pipes.push_back(p); // adding if cast was ok
}
// if no endpipes were found, return error
if(end_pipes.size() == 0)
{
cout << endl << "### Warning ###" << endl
<< "From: Model" << endl
<< "No end nodes found in the pipe system..." << endl
<< "At least one PIPE must have an OUTLETPRESSURE defined..." << endl << endl;
ok = false;
}
else // found end pipes, getting on with the calculations...
{
// looping through the end nodes
for(int i = 0; i < end_pipes.size(); i++)
{
// finding the connected wells for this branch
//end_pipes.at(i)->findConnectedWells();
// calculating the inlet pressures for the end pipes, and the branch of pipes connected
end_pipes.at(i)->calculateBranchInletPressures();
}
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// Connects capacities to the pipes
//-----------------------------------------------------------------------------------------------
bool Model::resolveCapacityConnections()
{
cout << "Resolving capacity - pipe connections..." << endl;
bool ok = true;
for(int i = 0; i < m_capacities.size(); i++) // looping through all separators
{
Capacity *s = m_capacities.at(i);
for(int j = 0; j < s->numberOfFeedPipeNumbers(); j++) // looping through all the pipes specified in the driver file
{
int pipe_num = s->feedPipeNumber(j);
bool pipe_ok = false;
for(int k = 0; k < m_pipes.size(); k++) // looping through pipes to find the correct one
{
if(m_pipes.at(k)->number() == pipe_num) // this is the correct pipe
{
s->addFeedPipe(m_pipes.at(k));
pipe_ok = true;
break;
}
}
// checking if the pipe number was found
if(!pipe_ok)
{
cout << endl << "### Runtime Error ###" << endl
<< "Capacity to Pipe connection could not be established..." << endl
<< "PIPE: " << pipe_num << endl
<< "CAPACITY: " << s->name().toLatin1().constData() << endl;
exit(1);
}
}
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// returns the well with the given id
//-----------------------------------------------------------------------------------------------
Well* Model::wellById(int comp_id)
{
for(int i = 0; i < numberOfWells(); ++i)
{
if(well(i)->id() == comp_id) return well(i);
}
return 0;
}
//-----------------------------------------------------------------------------------------------
// returns the well with the given name
//-----------------------------------------------------------------------------------------------
Well* Model::wellByName(const QString &name)
{
for(int i = 0; i < numberOfWells(); ++i)
{
if(well(i)->name().compare(name) == 0) return well(i);
}
return 0;
}
//-----------------------------------------------------------------------------------------------
// Updates the capacity constraints
//-----------------------------------------------------------------------------------------------
bool Model::updateCapacityConstraints()
{
bool ok = true;
// cout << "Updating the capacity constraints..." << endl;
for(int i = 0; i < numberOfCapacities(); i++)
{
capacity(i)->updateConstraints();
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// Updates the Well BHP and connection constraints
//-----------------------------------------------------------------------------------------------
bool Model::updateWellConstaints()
{
bool ok = true;
// cout << "Updating the well BHP and connection constraints" << endl;
for(int i = 0; i < numberOfWells(); i++)
{
// checking if this is a production well
ProductionWell *prod_well = dynamic_cast<ProductionWell*>(well(i));
if(prod_well != 0)
{
prod_well->updateBhpConstraint();
prod_well->updatePipeConnectionConstraint();
}
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// Updates the Pipe connection constraints
//-----------------------------------------------------------------------------------------------
bool Model::updatePipeConstraints()
{
bool ok = true;
for(int i = 0; i < numberOfPipes(); ++i)
{
// checking if this is a mid pipe
MidPipe *p_mid = dynamic_cast<MidPipe*>(pipe(i));
if(p_mid != 0)
{
p_mid->updateOutletConnectionConstraint();
}
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// Updates the Booster capacity constraints
//-----------------------------------------------------------------------------------------------
bool Model::updateBoosterConstraints()
{
bool ok = true;
for(int i = 0; i < numberOfPipes(); ++i)
{
// checking if this is a booster
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(pipe(i));
if(p_boost != 0)
{
p_boost->updateCapacityConstraints();
}
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// Updates the user defined constraints
//-----------------------------------------------------------------------------------------------
bool Model::updateUserDefinedConstraints()
{
bool ok = true;
for(int i = 0; i < numberOfUserDefinedConstraints(); ++i)
{
if(!userDefinedConstraint(i)->update()) ok = false;
}
return ok;
}
//-----------------------------------------------------------------------------------------------
// Updates the constraints that are common for all model types
//-----------------------------------------------------------------------------------------------
bool Model::updateCommonConstraints()
{
bool ok = true;
if(!updateCapacityConstraints()) ok = false;
if(!updateWellConstaints()) ok = false;
if(!updatePipeConstraints()) ok = false;
if(!updateBoosterConstraints()) ok = false;
if(!updateUserDefinedConstraints()) ok = false;
return ok;
}
//-----------------------------------------------------------------------------------------------
// Reads all the pipe pressure drop files
//-----------------------------------------------------------------------------------------------
void Model::readPipeFiles()
{
for(int i = 0; i < numberOfPipes(); i++)
{
// this should not be done for separators or boosters
Separator *sep = dynamic_cast<Separator*>(pipe(i));
PressureBooster *boost = dynamic_cast<PressureBooster*>(pipe(i));
if(sep == 0 && boost == 0) pipe(i)->readInputFile();
}
}
//-----------------------------------------------------------------------------------------------
// Updates the value of the objective
//-----------------------------------------------------------------------------------------------
void Model::updateObjectiveValue()
{
QVector<Stream*> field_rates;
// finding the end pipes
QVector<EndPipe*> p_end_pipes;
for(int i = 0; i < numberOfPipes(); ++i)
{
EndPipe *p = dynamic_cast<EndPipe*>(pipe(i));
if(p != 0) p_end_pipes.push_back(p);
}
// adding together the streams from all the end pipes
for(int i = 0; i < masterSchedule().size(); ++i)
{
Stream *s = new Stream();
// looping through the end pipes
for(int j = 0; j < p_end_pipes.size(); ++j)
{
*s += *p_end_pipes.at(j)->stream(i);
//cout << "Rates used for objective value:" <<endl;
//s->printToCout();
}
field_rates.push_back(s);
}
// then collecting all the costs
QVector<Cost*> costs;
// collecting the cost of the separators:
for(int i = 0; i < numberOfPipes(); ++i)
{
Separator *p_sep = dynamic_cast<Separator*>(pipe(i));
if(p_sep != 0)
{
// updating the remove fraction and capacity in the cost according to the variable values in the separator
p_sep->cost()->setFraction(p_sep->removeFraction()->value());
p_sep->cost()->setCapacity(p_sep->removeCapacity()->value());
// updating the time of the cost according to the variable
int time_cost = p_sep->installTime()->value();
if(time_cost <= 0) p_sep->cost()->setTime(0.0);
else if(time_cost >= m_master_schedule.size()) p_sep->cost()->setTime(m_master_schedule.at(m_master_schedule.size()-1) + 1);
else p_sep->cost()->setTime(m_master_schedule.at(time_cost-1));
// adding the cost to the vector
costs.push_back(p_sep->cost());
//cout << "Cost for separator #" << p_sep->number() << " = " << p_sep->cost()->value() << endl;
//cout << "Install time = " << p_sep->cost()->time() << endl << endl;
}
}
// collecting the cost of the boosters:
for(int i = 0; i < numberOfPipes(); ++i)
{
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(pipe(i));
if(p_boost != 0)
{
// updating the pressure and capacity in the cost according to the variable values in the booster
//p_boost->cost()->setFraction(p_boost->pressureVariable()->value());
//p_boost->cost()->setCapacity(p_boost->capacityVariable()->value());
p_boost->cost()->setFraction(p_boost->pressureVariable()->value());
p_boost->cost()->setCapacity(p_boost->capacityVariable()->value());
// updating the time of the cost according to the variable
int time_cost = p_boost->installTime()->value();
if(time_cost <= 0) p_boost->cost()->setTime(0.0);
else if(time_cost >= m_master_schedule.size()) p_boost->cost()->setTime(m_master_schedule.at(m_master_schedule.size()-1) + 1);
else p_boost->cost()->setTime(m_master_schedule.at(time_cost-1));
// adding the cost to the vector
costs.push_back(p_boost->cost());
}
}
// collecting the installation cost of the wells:
for(int i = 0; i < numberOfWells(); ++i)
{
Well *w = well(i);
if(w->hasCost())
{
if(w->hasInstallTime())
{
// updating the time of the cost according to the variable
int time_cost = w->installTime()->value();
if(time_cost <= 0) w->cost()->setTime(0.0);
else if(time_cost >= m_master_schedule.size()) w->cost()->setTime(m_master_schedule.at(m_master_schedule.size()-1) + 1);
else w->cost()->setTime(m_master_schedule.at(time_cost));
}
else
{
w->cost()->setTime(0.0);
}
// adding the cost to the vector
costs.push_back(w->cost());
}
}
QVector<Cost*> costs_sorted = sortCosts(costs);
//qSort(costs.begin(), costs.end()); // sorting the costs wrt. time
// calculating the new objective value
objective()->calculateValue(field_rates, costs_sorted);
// cout << "Objective value = " << objective()->value() << endl;
// deleting the generated streams
for(int i = 0; i < field_rates.size(); i++) delete field_rates.at(i);
}
//-----------------------------------------------------------------------------------------------
// Sorts the costs wrt. time
//-----------------------------------------------------------------------------------------------
QVector<Cost*> Model::sortCosts(QVector<Cost *> c)
{
QVector<Cost*> result;
// adding the first element to the results
if(c.size() > 0) result.push_back(c.at(0));
// looping through the unsorted costs
for(int i = 1; i < c.size(); ++i)
{
Cost *current_unsorted = c.at(i);
// finding the correct place in the results:
bool inserted = false;
for(int j = 0; j < result.size(); ++j)
{
// checking if the current_unsorted < result
if(current_unsorted->time() <= result.at(j)->time())
{
result.insert(j, current_unsorted);
inserted = true;
break;
}
}
if(!inserted) result.push_back(current_unsorted);
}
return result;
}
//-----------------------------------------------------------------------------------------------
// Assignment operator
//-----------------------------------------------------------------------------------------------
Model& Model::operator =(const Model &rhs)
{
bool ok = this->numberOfWells() == rhs.numberOfWells() &&
this->numberOfPipes() == rhs.numberOfPipes() &&
this->numberOfRealVariables() == rhs.numberOfRealVariables() &&
this->numberOfBinaryVariables() == rhs.numberOfBinaryVariables() &&
this->numberOfIntegerVariables() == rhs.numberOfIntegerVariables() &&
this->numberOfConstraints() == rhs.numberOfConstraints();
if(this != &rhs && ok)
{
// updating wells
for(int i = 0; i < numberOfWells(); ++i)
{
// looping through the streams
for(int j = 0; j < well(i)->numberOfStreams(); ++j)
{
*well(i)->stream(j) = *rhs.well(i)->stream(j);
//well(i)->stream(j)->setPressure(rhs.well(i)->stream(j)->pressure(true));
}
}
// updating pipes
for(int i = 0; i < numberOfPipes(); ++i)
{
// looping through the streams
for(int j = 0; j < pipe(i)->numberOfStreams(); ++j)
{
*pipe(i)->stream(j) = *rhs.pipe(i)->stream(j);
//pipe(i)->stream(j)->setPressure(rhs.pipe(i)->stream(j)->pressure(true));
}
}
// updating real variable values
for(int i = 0; i < numberOfRealVariables(); ++i) realVariables().at(i)->setValue(rhs.realVariableValue(i));
// updating binary variable values
for(int i = 0; i < numberOfBinaryVariables(); ++i) binaryVariables().at(i)->setValue(rhs.binaryVariableValue(i));
// updating integer variable values
for(int i = 0; i < numberOfIntegerVariables(); ++i) integerVariables().at(i)->setValue(rhs.integerVariableValue(i));
// updating constraint values
for(int i = 0; i < numberOfConstraints(); ++i) constraints().at(i)->setValue(rhs.constraintValue(i));
}
return *this;
}
} // namespace ResOpt