-
Notifications
You must be signed in to change notification settings - Fork 5
/
coupledmodel.cpp
615 lines (458 loc) · 21.2 KB
/
coupledmodel.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
/*
* This file is part of the ResOpt project.
*
* Copyright (C) 2011-2012 Aleksander O. Juell <[email protected]>
*
* 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 "coupledmodel.h"
#include "midpipe.h"
#include "pipeconnection.h"
#include "binaryvariable.h"
#include "realvariable.h"
#include "intvariable.h"
#include "stream.h"
#include "productionwell.h"
#include "separator.h"
#include "pressurebooster.h"
#include "capacity.h"
#include "userconstraint.h"
#include "wellconnectionvariable.h"
#include "wellpath.h"
#include <iostream>
using std::cout;
using std::endl;
namespace ResOpt
{
CoupledModel::CoupledModel()
{
}
CoupledModel::CoupledModel(const CoupledModel &m)
: Model(m)
{}
CoupledModel::~CoupledModel()
{}
//-----------------------------------------------------------------------------------------------
// Initializes the model, sets up constraints
//-----------------------------------------------------------------------------------------------
void CoupledModel::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());
// checking if this is a Booster
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(pipe(i));
if(p_boost != 0) p_boost->setupCapacityConstraints(masterSchedule()); // if so, setting up the capacity constraints
}
// setting up the constraints for the capacities
for(int i = 0; i < numberOfCapacities(); ++i)
{
capacity(i)->setupConstraints(masterSchedule());
}
// initializing the user defined constraints
for(int i = 0; i < numberOfUserDefinedConstraints(); ++i) userDefinedConstraint(i)->initialize();
}
//-----------------------------------------------------------------------------------------------
// processes the model after the reservoir simulator is run
//-----------------------------------------------------------------------------------------------
void CoupledModel::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 rates flowing through every element in the model
//-----------------------------------------------------------------------------------------------
void CoupledModel::updateStreams()
{
//cout << "Updating the streams for the pipe system..." << endl;
// first need to empty all the streams in all pipes
for(int i = 0; i < numberOfPipes(); ++i) pipe(i)->emptyStreams();
// starting with the production wells, feeding the rates to the connected pipes
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
addStreamsUpstream(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, separator or booster
MidPipe *p_mid = dynamic_cast<MidPipe*>(prod_well->pipeConnection(j)->pipe());
Separator *p_sep = dynamic_cast<Separator*>(prod_well->pipeConnection(j)->pipe());
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(prod_well->pipeConnection(j)->pipe());
if(p_mid != 0) addStreamsUpstream(p_mid, prod_well, prod_well->pipeConnection(j)->variable()->value());
else if(p_sep != 0) addStreamsUpstream(p_sep, prod_well, prod_well->pipeConnection(j)->variable()->value());
else if(p_boost != 0) addStreamsUpstream(p_boost, prod_well, prod_well->pipeConnection(j)->variable()->value());
} // pipe connection
} // production well
} // well
}
//-----------------------------------------------------------------------------------------------
// updates the value of the constraints
//-----------------------------------------------------------------------------------------------
bool CoupledModel::updateConstraints()
{
// the coupled model does not have any custom constraints
bool ok = updateCommonConstraints(); // updates the constraints that are common for all model types
return ok;
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the well to the direct upstream connections
//-----------------------------------------------------------------------------------------------
void CoupledModel::addStreamsUpstream(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;
// adding the rate contribution from this well to what is allready going through the pipe
p->addToStream(j, s);
}
}
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the pipe to all upstream connections
//-----------------------------------------------------------------------------------------------
void CoupledModel::addStreamsUpstream(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;
// adding the contribution to the upstream pipe
upstream->addToStream(j, s);
}
// 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);
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(upstream);
if(p_mid != 0) addStreamsUpstream(p_mid, from_well, total_frac);
else if(p_sep != 0) addStreamsUpstream(p_sep, from_well, total_frac);
else if(p_boost != 0) addStreamsUpstream(p_boost, from_well, total_frac);
}
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the separator to all upstream connections
//-----------------------------------------------------------------------------------------------
void CoupledModel::addStreamsUpstream(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->remainingCapacity(i)) qw_remove = s->remainingCapacity(i);
// subtracting the removed water
str.setWaterRate(str.waterRate(true) - qw_remove);
// subrtacting from the remaining capacity
s->reduceRemainingCapacity(i, 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->remainingCapacity(i)) qg_remove = s->remainingCapacity(i);
// subtracting the removed gas
str.setGasRate(str.gasRate(true) - qg_remove);
// subrtacting from the remaining capacity
s->reduceRemainingCapacity(i, qg_remove);
}
}
// adding the stream to the upstream pipe
upstream->addToStream(i, str);
}
// 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);
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(upstream);
if(p_mid != 0) addStreamsUpstream(p_mid, from_well, flow_frac);
else if(p_sep != 0) addStreamsUpstream(p_sep, from_well, flow_frac);
else if(p_boost != 0) addStreamsUpstream(p_boost, from_well, flow_frac);
}
//-----------------------------------------------------------------------------------------------
// adds the rates from the booster to all upstream connections
//-----------------------------------------------------------------------------------------------
void CoupledModel::addStreamsUpstream(PressureBooster *b, Well *from_well, double flow_frac)
{
// pointer to the upstream connected pipe
Pipe *upstream = b->outletConnection()->pipe();
// looping through the streams, adding the contribution from the separator
for(int i = 0; i < b->numberOfStreams(); ++i)
{
Stream str = *from_well->stream(i) * flow_frac;
// adding the stream to the upstream pipe
upstream->addToStream(i, str);
}
// then checking if the upstream pipe is a midpipe, separator, or booster
MidPipe *p_mid = dynamic_cast<MidPipe*>(upstream);
Separator *p_sep = dynamic_cast<Separator*>(upstream);
PressureBooster *p_boost = dynamic_cast<PressureBooster*>(upstream);
if(p_mid != 0) addStreamsUpstream(p_mid, from_well, flow_frac);
else if(p_sep != 0) addStreamsUpstream(p_sep, from_well, flow_frac);
else if(p_boost != 0) addStreamsUpstream(p_boost, from_well, flow_frac);
}
//-----------------------------------------------------------------------------------------------
// Collects all the binary variables
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<BinaryVariable> >& CoupledModel::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> >& CoupledModel::realVariables(bool force_refresh)
{
if(m_vars_real.size() == 0 || force_refresh)
{
if(force_refresh) m_vars_real.resize(0);
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());
}
}
}
for(int i = 0; i < numberOfPipes(); ++i) // looping through the pipes, finding the separators and boosters
{
// checking if this is a Separator
Separator *s = dynamic_cast<Separator*>(pipe(i));
if(s != 0)
{
if(s->removeFraction()->isVariable()) m_vars_real.push_back(s->removeFraction());
if(s->removeCapacity()->isVariable()) m_vars_real.push_back(s->removeCapacity());
}
// checking if this is a Booster
PressureBooster *b = dynamic_cast<PressureBooster*>(pipe(i));
if(b != 0)
{
if(b->pressureVariable()->isVariable()) m_vars_real.push_back(b->pressureVariable());
if(b->capacityVariable()->isVariable()) m_vars_real.push_back(b->capacityVariable());
}
}
}
return m_vars_real;
}
//-----------------------------------------------------------------------------------------------
// Collects all the integer variables
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<IntVariable> >& CoupledModel::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 and boosters
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
}
// checking if this is a Booster
PressureBooster *b = dynamic_cast<PressureBooster*>(pipe(i));
if(b != 0)
{
if(b->installTime()->isVariable()) m_vars_integer.push_back(b->installTime());
}
}
// collecting the install time variables and connection 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
}
// checking if the well has connection variables
if(well(i)->hasVariableConnections())
{
for(int j = 0; j < well(i)->numberOfVariableConnections(); ++j)
{
if(well(i)->variableConnection(j)->iVariable()->isVariable()) m_vars_integer.push_back(well(i)->variableConnection(j)->iVariable());
if(well(i)->variableConnection(j)->jVariable()->isVariable()) m_vars_integer.push_back(well(i)->variableConnection(j)->jVariable());
}
}
// checking if the well has a well path
if(well(i)->hasWellPath())
{
m_vars_integer += well(i)->wellPath()->variables();
}
}
}
return m_vars_integer;
}
//-----------------------------------------------------------------------------------------------
// Collects all the constraints
//-----------------------------------------------------------------------------------------------
QVector<shared_ptr<Constraint> >& CoupledModel::constraints(bool force_refresh)
{
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 pipe constraints (midpipe connection and booster capacity)
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());
// checking if this is a booster
PressureBooster* p_boost = dynamic_cast<PressureBooster*>(pipe(i));
if(p_boost != 0)
{
m_cons += p_boost->capacityConstraints();
}
}
// getting the 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 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> > CoupledModel::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