-
Notifications
You must be signed in to change notification settings - Fork 5
/
decoupledmodel.cpp
666 lines (499 loc) · 22.7 KB
/
decoupledmodel.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
/*
* 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 "decoupledmodel.h"
#include "materialbalanceconstraint.h"
#include "inputratevariable.h"
#include "realvariable.h"
#include "binaryvariable.h"
#include "intvariable.h"
#include "productionwell.h"
#include "midpipe.h"
#include "capacity.h"
#include "stream.h"
#include "pipeconnection.h"
#include "midpipe.h"
#include "productionwell.h"
#include "separator.h"
#include "userconstraint.h"
#include <iostream>
using std::cout;
using std::endl;
namespace ResOpt
{
DecoupledModel::DecoupledModel()
{
}
DecoupledModel::DecoupledModel(const DecoupledModel &m)
: Model(m)
{
//copying the input rate variables
initializeVarsAndCons();
}
DecoupledModel::~DecoupledModel()
{
for(int i = 0; i < m_mb_cons.size(); ++i) delete m_mb_cons.at(i);
for(int i = 0; i < m_rate_vars.size(); ++i) delete m_rate_vars.at(i);
}
//-----------------------------------------------------------------------------------------------
// Initializes the model, sets up constraints
//-----------------------------------------------------------------------------------------------
void DecoupledModel::initialize()
{
// initializing all wells, setting up constraints for production wells
for(int i = 0; i < numberOfWells(); ++i)
{
// initializing the well
well(i)->initialize();
// casting the well to production well, setting up constraints if cast is ok
ProductionWell *prod_well = dynamic_cast<ProductionWell*>(well(i));
if(prod_well != 0) prod_well->setupConstraints();
}
// initializing the pipes
for(int i = 0; i < numberOfPipes(); ++i)
{
pipe(i)->initialize(masterSchedule());
}
// setting up the constraints for the capacities
for(int i = 0; i < numberOfCapacities(); ++i)
{
capacity(i)->setupConstraints(masterSchedule());
}
// setting up the rate input variables
initializeVarsAndCons();
// initializing the user defined constraints
for(int i = 0; i < numberOfUserDefinedConstraints(); ++i) userDefinedConstraint(i)->initialize();
}
//-----------------------------------------------------------------------------------------------
// setting up the input rate variables and mbc
//-----------------------------------------------------------------------------------------------
void DecoupledModel::initializeVarsAndCons()
{
for(int i = 0; i < numberOfPipes(); ++i)
{
Pipe *p = pipe(i);
for(int j = 0; j < p->numberOfStreams(); ++j)
{
InputRateVariable *irv = new InputRateVariable();
irv->setPipe(p);
irv->setStream(p->stream(j));
shared_ptr<RealVariable> var_oil = shared_ptr<RealVariable>(new RealVariable(p));
var_oil->setMax(1e6);
var_oil->setMin(0);
var_oil->setValue(100);
var_oil->setName("Input oil rate variable for Pipe #" + QString::number(p->number()) + ", time = " + QString::number(p->stream(j)->time()));
irv->setOilVariable(var_oil);
shared_ptr<RealVariable> var_gas = shared_ptr<RealVariable>(new RealVariable(p));
var_gas->setMax(1e7);
var_gas->setMin(0);
var_gas->setValue(1000);
var_gas->setName("Input gas rate variable for Pipe #" + QString::number(p->number()) + ", time = " + QString::number(p->stream(j)->time()));
irv->setGasVariable(var_gas);
shared_ptr<RealVariable> var_water = shared_ptr<RealVariable>(new RealVariable(p));
var_water->setMax(1e5);
var_water->setMin(0);
var_water->setValue(10);
var_water->setName("Input water rate variable for Pipe #" + QString::number(p->number()) + ", time = " + QString::number(p->stream(j)->time()));
irv->setWaterVariable(var_water);
// creating the constraints asociates with the input rate variable
MaterialBalanceConstraint *mbc = new MaterialBalanceConstraint();
mbc->setInputRateVariable(irv);
m_mb_cons.push_back(mbc);
// adding the input rate variable to the vector
m_rate_vars.push_back(irv);
}
}
}
//-----------------------------------------------------------------------------------------------
// processes the model after the reservoir simulator is run
//-----------------------------------------------------------------------------------------------
void DecoupledModel::process()
{
// update the streams in the pipe network
updateStreams();
// calculating pressures in the Pipe network
calculatePipePressures();
// updating the constraints (this must be done after the pressure calc)
updateConstraints();
// updating the objective
updateObjectiveValue();
// updating the status of the model
setUpToDate(true);
}
//-----------------------------------------------------------------------------------------------
// updates the values of the constraints
//-----------------------------------------------------------------------------------------------
bool DecoupledModel::updateConstraints()
{
bool ok = true;
// first updating the non-material balance constraints
ok = updateCommonConstraints();
cout << "updating the material balance streams..." << endl;
// updating the streams in the material balance constraints
updateMaterialBalanceStreams();
cout << "done updating the material balance streams..." << endl;
// last need to update the value of the material balance constraints
for(int i = 0; i < m_mb_cons.size(); ++i) m_mb_cons.at(i)->updateConstraints();
return ok;
}
//-----------------------------------------------------------------------------------------------
// updates the rates flowing through every element in the model
//-----------------------------------------------------------------------------------------------
void DecoupledModel::updateStreams()
{
for(int i = 0; i < m_rate_vars.size(); ++i)
{
m_rate_vars.at(i)->updateStream();
}
}
//-----------------------------------------------------------------------------------------------
// finds the material balance constraint that is connected to this stream
//-----------------------------------------------------------------------------------------------
MaterialBalanceConstraint* DecoupledModel::find(Stream *s)
{
MaterialBalanceConstraint *mbc = 0;
for(int i = 0; i < m_mb_cons.size(); ++i)
{
if(s == m_mb_cons.at(i)->inputRateVariable()->stream())
{
mbc = m_mb_cons.at(i);
break;
}
}
if(mbc == 0)
{
cout << "find mbc error!" << endl;
s->printToCout();
}
return mbc;
}
//-----------------------------------------------------------------------------------------------
// updates the streams in the material balance constraints
//-----------------------------------------------------------------------------------------------
void DecoupledModel::updateMaterialBalanceStreams()
{
// emptying the streams in the mbcs
for(int i = 0; i < m_mb_cons.size(); ++i) m_mb_cons.at(i)->emptyStream();
// updating the streams in the mbc, starting from the production wells, and working its way up the system
for(int i = 0; i < numberOfWells(); ++i)
{
// trying to cast to production well
ProductionWell *prod_well = dynamic_cast<ProductionWell*>(well(i));
if(prod_well != 0) // this is a production well
{
// adding the streams from this well to the upstream pipes connected to it
addToMaterialBalanceStreamsUpstream(prod_well);
// looping through the outlet connections of the well, doing the same
for(int j = 0; j < prod_well->numberOfPipeConnections(); ++j)
{
// checking if it is a midpipe or separator
MidPipe *p_mid = dynamic_cast<MidPipe*>(prod_well->pipeConnection(j)->pipe());
Separator *p_sep = dynamic_cast<Separator*>(prod_well->pipeConnection(j)->pipe());
if(p_mid != 0) addToMaterialBalanceStreamsUpstream(p_mid, prod_well, prod_well->pipeConnection(i)->variable()->value());
else if(p_sep != 0) addToMaterialBalanceStreamsUpstream(p_sep, prod_well, prod_well->pipeConnection(i)->variable()->value());
} // pipe connection
} // production well
} // well
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the well to the direct upstream connections
//-----------------------------------------------------------------------------------------------
void DecoupledModel::addToMaterialBalanceStreamsUpstream(ProductionWell *w)
{
// looping through the pipes connected to the well
for(int i = 0; i < w->numberOfPipeConnections(); ++i)
{
Pipe *p = w->pipeConnection(i)->pipe(); // pointer to the pipe
// finding the flow fraction from this well to the pipe
double frac = w->pipeConnection(i)->variable()->value();
// calculating the rate from this well to the pipe vs. time
for(int j = 0; j < w->numberOfStreams(); ++j)
{
Stream s = *w->stream(j) * frac;
// finding the material balance constraint that corresponds to this pipe and time
MaterialBalanceConstraint *mbc = find(p->stream(j));
// adding the rate contribution from this well to what is allready in the mbc
mbc->setStream(s + mbc->stream());
}
}
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the pipe to all upstream connections
//-----------------------------------------------------------------------------------------------
void DecoupledModel::addToMaterialBalanceStreamsUpstream(MidPipe *p, Well *from_well, double flow_frac)
{
// looping through the pipes connected to the pipe
for(int i = 0; i < p->numberOfOutletConnections(); ++i)
{
Pipe *upstream = p->outletConnection(i)->pipe(); // pointer to the upstream pipe
// finding the flow fraction from this pipe to the upstream pipe
double frac = p->outletConnection(i)->variable()->value();
double total_frac = frac * flow_frac;
// looping through the streams, adding the rate from this pipe
for(int j = 0; j < p->numberOfStreams(); ++j)
{
Stream s = *from_well->stream(j) * total_frac;
// finding the material balance constraint that corresponds to this pipe and time
MaterialBalanceConstraint *mbc = find(upstream->stream(j));
// adding the rate contribution from this well to what is allready in the mbc
mbc->setStream(s + mbc->stream());
}
// then checking if the outlet pipe connection is a midpipe or separator
MidPipe *p_mid = dynamic_cast<MidPipe*>(upstream);
Separator *p_sep = dynamic_cast<Separator*>(upstream);
if(p_mid != 0) addToMaterialBalanceStreamsUpstream(p_mid, from_well, total_frac);
else if(p_sep != 0) addToMaterialBalanceStreamsUpstream(p_sep, from_well, total_frac);
}
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the pipe to all upstream connections
//-----------------------------------------------------------------------------------------------
void DecoupledModel::addToMaterialBalanceStreamsUpstream(Separator *s, Well *from_well, double flow_frac)
{
// pointer to the upstream connected pipe
Pipe *upstream = s->outletConnection()->pipe();
// looping through the streams, adding the contribution from the separator
for(int i = 0; i < s->numberOfStreams(); ++i)
{
Stream str = *from_well->stream(i) * flow_frac;
// checking if the separator is installed
if(i >= s->installTime()->value())
{
// checking if this is a water or gas separator
if(s->type() == Separator::WATER)
{
// how much water should be removed
double qw_remove = str.waterRate(true) * s->removeFraction()->value();
if(qw_remove > s->removeCapacity()->value()) qw_remove = s->removeCapacity()->value();
// subtracting the removed water
str.setWaterRate(str.waterRate(true) - qw_remove);
}
else if(s->type() == Separator::GAS)
{
// how much gas should be removed
double qg_remove = str.gasRate(true) * s->removeFraction()->value();
if(qg_remove > s->removeCapacity()->value()) qg_remove = s->removeCapacity()->value();
// subtracting the removed gas
str.setGasRate(str.gasRate(true) - qg_remove);
}
}
// finding the material balance constraint that corresponds to this pipe and time
MaterialBalanceConstraint *mbc = find(upstream->stream(i));
// adding the rate contribution from this well to what is allready in the mbc
mbc->setStream(str + mbc->stream());
}
// then checking if the upstream pipe is a midpipe or separator
MidPipe *p_mid = dynamic_cast<MidPipe*>(upstream);
Separator *p_sep = dynamic_cast<Separator*>(upstream);
if(p_mid != 0) addToMaterialBalanceStreamsUpstream(p_mid, from_well, flow_frac);
else if(p_sep != 0) addToMaterialBalanceStreamsUpstream(p_sep, from_well, flow_frac);
}
//-----------------------------------------------------------------------------------------------
// Collects all the binary variables
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<BinaryVariable> >& DecoupledModel::binaryVariables(bool force_refresh)
{
if(m_vars_binary.size() == 0 || force_refresh)
{
if(force_refresh) m_vars_binary.resize(0);
// finding well routnig variables
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)
{
// looping through the pipe connections
for(int j = 0; j < prod_well->numberOfPipeConnections(); j++)
{
if(prod_well->pipeConnection(j)->variable()->isVariable()) m_vars_binary.push_back(prod_well->pipeConnection(j)->variable());
}
}
}
// finding pipe routing variables
for(int j = 0; j < numberOfPipes(); ++j)
{
MidPipe *p_mid = dynamic_cast<MidPipe*>(pipe(j)); // end pipes do not have routing
if(p_mid != 0)
{
// looping through the outlet connections
for(int j = 0; j < p_mid->numberOfOutletConnections(); j++)
{
if(p_mid->outletConnection(j)->variable()->isVariable()) m_vars_binary.push_back(p_mid->outletConnection(j)->variable());
}
}
}
}
return m_vars_binary;
}
//-----------------------------------------------------------------------------------------------
// Collects all the real variables
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<RealVariable> >& DecoupledModel::realVariables(bool force_refresh)
{
if(m_vars_real.size() == 0 || force_refresh)
{
if(force_refresh) m_vars_real.resize(0);
// getting the control variables for the wells
for(int i = 0; i < numberOfWells(); ++i) // looping through all the wells
{
Well *w = well(i);
for(int j = 0; j < w->numberOfControls(); j++) // looping through each element in the wells schedule
{
// checking if this shcedule entry is a variable
if(w->control(j)->controlVar()->isVariable()) m_vars_real.push_back(w->control(j)->controlVar());
}
// checking if this is a production well, and if it has gas lift controls
ProductionWell *prod_well = dynamic_cast<ProductionWell*>(w);
if(prod_well != 0)
{
for(int j = 0; j < prod_well->numberOfGasLiftControls(); ++j)
{
if(prod_well->gasLiftControl(j)->controlVar()->isVariable()) m_vars_real.push_back(prod_well->gasLiftControl(j)->controlVar());
}
}
}
// getting the remove fraction and capacity variables for the separators
for(int i = 0; i < numberOfPipes(); ++i) // looping through the pipes, finding the separators
{
Separator *s = dynamic_cast<Separator*>(pipe(i));
if(s != 0) // this is a separator
{
if(s->removeFraction()->isVariable()) m_vars_real.push_back(s->removeFraction());
if(s->removeCapacity()->isVariable()) m_vars_real.push_back(s->removeCapacity());
}
}
// getting the input rate variables
for(int i = 0; i < m_rate_vars.size(); ++i)
{
m_vars_real.push_back(m_rate_vars.at(i)->oilVariable());
m_vars_real.push_back(m_rate_vars.at(i)->gasVariable());
m_vars_real.push_back(m_rate_vars.at(i)->waterVariable());
}
}
return m_vars_real;
}
//-----------------------------------------------------------------------------------------------
// Collects all the integer variables
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<IntVariable> >& DecoupledModel::integerVariables(bool force_refresh)
{
if(m_vars_integer.size() == 0 || force_refresh)
{
if(force_refresh) m_vars_integer.resize(0);
// collecting the install time variables for the separators
for(int i = 0; i < numberOfPipes(); ++i) // looping through all the pipes
{
// checking if this is a separator
Separator *s = dynamic_cast<Separator*>(pipe(i));
if(s != 0)
{
if(s->installTime()->isVariable()) m_vars_integer.push_back(s->installTime()); // adding install time if it is a variable
}
}
// collecting the install time variables for the wells
for(int i = 0 ; i < numberOfWells(); ++i)
{
// checking if the well has an install time variable
if(well(i)->hasInstallTime())
{
if(well(i)->installTime()->isVariable()) m_vars_integer.push_back(well(i)->installTime()); // adding install time if it is a variable
}
}
}
return m_vars_integer;
}
//-----------------------------------------------------------------------------------------------
// Collects all the constraints
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<Constraint> >& DecoupledModel::constraints(bool force_refresh)
{
// TODO: the part of this function that is common between Coupled and Decoupled model should be put back into the Model class
if(m_cons.size() == 0 || force_refresh)
{
if(force_refresh) m_cons.resize(0);
// getting the well bhp constraints
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)
{
for(int i = 0; i < prod_well->numberOfBhpConstraints(); ++i) m_cons.push_back(prod_well->bhpConstraint(i));
}
}
// getting the well pipe connection constraints
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)
{
if(prod_well->pipeConnectionConstraint() != 0) m_cons.push_back(prod_well->pipeConnectionConstraint());
}
}
// getting the mid pipe connection constraints
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) m_cons.push_back(p_mid->outletConnectionConstraint());
}
// getting the separator capacity constraints
for(int i = 0; i < numberOfCapacities(); ++i)
{
Capacity *sep = capacity(i);
m_cons += sep->gasConstraints();
m_cons += sep->oilConstraints();
m_cons += sep->waterConstraints();
m_cons += sep->liquidConstraints();
}
// getting the material balance constraints
for(int i = 0; i < m_mb_cons.size(); ++i)
{
MaterialBalanceConstraint *mbc = m_mb_cons.at(i);
m_cons.push_back(mbc->oilConstraint());
m_cons.push_back(mbc->gasConstraint());
m_cons.push_back(mbc->waterConstraint());
}
// getting the user defined constraints
for(int i = 0; i < numberOfUserDefinedConstraints(); ++i)
{
m_cons.push_back(userDefinedConstraint(i)->constraint());
}
}
return m_cons;
}
//-----------------------------------------------------------------------------------------------
// Returns a vector of the real vars for a component
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<RealVariable> > DecoupledModel::realVariables(Component *c)
{
QVector<shared_ptr<RealVariable> > comp_vars;
// looping through all the real variables
for(int i = 0; i < realVariables().size(); ++i)
{
if(realVariables().at(i)->parent()->id() == c->id())
{
comp_vars.push_back(realVariables().at(i));
}
}
return comp_vars;
}
} // namespace ResOpt