diff --git a/opm/core/wells/WellsGroup.cpp b/opm/core/wells/WellsGroup.cpp index 1e626a3d0..757460807 100644 --- a/opm/core/wells/WellsGroup.cpp +++ b/opm/core/wells/WellsGroup.cpp @@ -429,10 +429,15 @@ namespace Opm if (production_violated) { switch (prodSpec().procedure_) { case ProductionSpecification::WELL: - getWorstOffending(well_reservoirrates_phase, - well_surfacerates_phase, - production_mode_violated).first->shutWell(); - return false; + { + const auto& offender = getWorstOffending(well_reservoirrates_phase, + well_surfacerates_phase, + production_mode_violated); + if (offender.first) + offender.first->shutWell(); + + return false; + } case ProductionSpecification::RATE: applyProdGroupControl(production_mode_violated, getTarget(production_mode_violated), diff --git a/opm/core/wells/well_controls.c b/opm/core/wells/well_controls.c index 8a64c5254..ae3a2ceea 100644 --- a/opm/core/wells/well_controls.c +++ b/opm/core/wells/well_controls.c @@ -167,7 +167,6 @@ static int well_controls_reserve(int nctrl, struct WellControls *ctrl) /* ---------------------------------------------------------------------- */ { - int c, p, ok; void *type, *target, *alq, *vfp, *distr; type = realloc(ctrl->type , nctrl * 1 * sizeof *ctrl->type ); @@ -176,7 +175,7 @@ well_controls_reserve(int nctrl, struct WellControls *ctrl) vfp = realloc(ctrl->vfp , nctrl * 1 * sizeof *ctrl->vfp ); distr = realloc(ctrl->distr , nctrl * ctrl->number_of_phases * sizeof *ctrl->distr ); - ok = 0; + int ok = 0; if (type != NULL) { ctrl->type = type ; ok++; } if (target != NULL) { ctrl->target = target; ok++; } if (alq != NULL) { ctrl->alq = alq; ok++; } @@ -184,12 +183,12 @@ well_controls_reserve(int nctrl, struct WellControls *ctrl) if (distr != NULL) { ctrl->distr = distr ; ok++; } if (ok == 5) { - for (c = ctrl->cpty; c < nctrl; c++) { + for (int c = ctrl->cpty; c < nctrl; c++) { ctrl->type [c] = BHP; ctrl->target[c] = -1.0; } - for (p = ctrl->cpty * ctrl->number_of_phases; p < nctrl * ctrl->number_of_phases; ++p) { + for (int p = ctrl->cpty * ctrl->number_of_phases; p < nctrl * ctrl->number_of_phases; ++p) { ctrl->distr[ p ] = 0.0; } @@ -205,34 +204,27 @@ struct WellControls * well_controls_clone(const struct WellControls *ctrl) /* ---------------------------------------------------------------------- */ { - int ok, i, n; - double target; - double alq; - int vfp; - const double *distr; - struct WellControls *new; - enum WellControlType type; - - new = well_controls_create(); + struct WellControls* new = well_controls_create(); if (new != NULL) { /* Assign appropriate number of phases */ well_controls_assert_number_of_phases(new, ctrl->number_of_phases); - n = well_controls_get_num(ctrl); - ok = well_controls_reserve(n, new); + int n = well_controls_get_num(ctrl); + int ok = well_controls_reserve(n, new); if (! ok) { well_controls_destroy(new); new = NULL; } else { + int i; for (i = 0; ok && (i < n); i++) { - type = well_controls_iget_type (ctrl, i); - distr = well_controls_iget_distr (ctrl, i); - target = well_controls_iget_target(ctrl, i); - alq = well_controls_iget_alq (ctrl, i); - vfp = well_controls_iget_vfp (ctrl, i); + enum WellControlType type = well_controls_iget_type (ctrl, i); + const double* distr = well_controls_iget_distr (ctrl, i); + double target = well_controls_iget_target(ctrl, i); + double alq = well_controls_iget_alq (ctrl, i); + int vfp = well_controls_iget_vfp (ctrl, i); ok = well_controls_add_new(type, target, alq, vfp, distr, new); } @@ -418,6 +410,12 @@ well_controls_equal(const struct WellControls *ctrls1, const struct WellControls { bool are_equal = true; + if (!ctrls1 || !ctrls2) { + if (verbose) + printf("ctrls1 %p or cntrls2 %p is NULL\n", ctrls2, ctrls2); + return false; + } + if (ctrls1->num != ctrls2->num) { are_equal = false; if (verbose) diff --git a/opm/core/wells/wells.c b/opm/core/wells/wells.c index 3689ba701..cdb72eefc 100644 --- a/opm/core/wells/wells.c +++ b/opm/core/wells/wells.c @@ -262,10 +262,7 @@ struct Wells * create_wells(int nphases, int nwells, int nperf) /* ---------------------------------------------------------------------- */ { - int ok; - struct Wells *W; - - W = malloc(1 * sizeof *W); + struct Wells* W = malloc(1 * sizeof *W); if (W != NULL) { W->number_of_wells = 0; @@ -286,7 +283,7 @@ create_wells(int nphases, int nwells, int nperf) W->data = create_well_mgmt(); - ok = (W->well_connpos != NULL) && (W->data != NULL); + int ok = (W->well_connpos != NULL) && (W->data != NULL); if (ok) { W->well_connpos[0] = 0; @@ -310,18 +307,14 @@ void destroy_wells(struct Wells *W) /* ---------------------------------------------------------------------- */ { - int w; - - struct WellMgmt *m; - if (W != NULL) { - m = W->data; + struct WellMgmt* m = W->data; - for (w = 0; w < m->well_cpty; w++) { + for (int w = 0; w < m->well_cpty; w++) { well_controls_destroy(W->ctrls[w]); } - for (w = 0; w < m->well_cpty; w++) { + for (int w = 0; w < m->well_cpty; w++) { free(W->name[w]); } @@ -374,39 +367,34 @@ add_well(enum WellType type , struct Wells *W ) /* ---------------------------------------------------------------------- */ { - int ok, nw, np, nperf_tot, off; - int nwalloc, nperfalloc; - - struct WellMgmt *m; - assert (W != NULL); - nw = W->number_of_wells; - nperf_tot = W->well_connpos[nw]; + int nw = W->number_of_wells; + int nperf_tot = W->well_connpos[nw]; - m = W->data; + struct WellMgmt* m = W->data; - ok = (nw < m->well_cpty) && (nperf_tot + nperf <= m->perf_cpty); + int ok = (nw < m->well_cpty) && (nperf_tot + nperf <= m->perf_cpty); if (! ok) { - nwalloc = alloc_size(nw , 1 , m->well_cpty); - nperfalloc = alloc_size(nperf_tot, nperf, m->perf_cpty); + int nwalloc = alloc_size(nw , 1 , m->well_cpty); + int nperfalloc = alloc_size(nperf_tot, nperf, m->perf_cpty); ok = wells_reserve(nwalloc, nperfalloc, W); } - off = W->well_connpos[nw]; + int off = W->well_connpos[nw]; if (ok && (nperf > 0)) { assert (cells != NULL); + if (cells != NULL && W->well_cells != NULL) + memcpy(W->well_cells + off, + cells, nperf * sizeof *W->well_cells); - memcpy(W->well_cells + off, - cells, nperf * sizeof *W->well_cells); - - if (WI != NULL) { + if (W->WI != NULL && WI != NULL) { memcpy(W->WI + off, WI, nperf * sizeof *W->WI); } - if (sat_table_id != NULL) { + if (W->sat_table_id != NULL && sat_table_id != NULL) { memcpy(W->sat_table_id + off, sat_table_id, nperf * sizeof *W->sat_table_id); } } @@ -422,7 +410,7 @@ add_well(enum WellType type , W->name [nw] = dup_string(name); } - np = W->number_of_phases; + int np = W->number_of_phases; if (comp_frac != NULL) { memcpy(W->comp_frac + np*nw, comp_frac, np * sizeof *W->comp_frac); } @@ -493,10 +481,6 @@ struct Wells * clone_wells(const struct Wells *W) /* ---------------------------------------------------------------------- */ { - int np, nperf, ok, pos, w; - const int *cells, *sat_table_id; - const double *WI, *comp_frac; - struct WellControls *ctrl; struct Wells *newWells; @@ -504,21 +488,21 @@ clone_wells(const struct Wells *W) newWells = NULL; } else { - np = W->number_of_phases; + int np = W->number_of_phases; newWells = create_wells(W->number_of_phases, W->number_of_wells, W->well_connpos[ W->number_of_wells ]); if (newWells != NULL) { - pos = W->well_connpos[ 0 ]; - ok = 1; + int pos = W->well_connpos[ 0 ]; + int ok = 1; - for (w = 0; ok && (w < W->number_of_wells); w++) { - nperf = W->well_connpos[w + 1] - pos; - cells = W->well_cells + pos; + for (int w = 0; ok && (w < W->number_of_wells); w++) { + int nperf = W->well_connpos[w + 1] - pos; + const int* cells = W->well_cells + pos; - WI = W->WI != NULL ? W->WI + pos : NULL; - sat_table_id = W->sat_table_id != NULL ? W->sat_table_id + pos : NULL; - comp_frac = W->comp_frac != NULL ? W->comp_frac + w*np : NULL; + const double* WI = W->WI != NULL ? W->WI + pos : NULL; + const int* sat_table_id = W->sat_table_id != NULL ? W->sat_table_id + pos : NULL; + const double* comp_frac = W->comp_frac != NULL ? W->comp_frac + w*np : NULL; ok = add_well(W->type[ w ], W->depth_ref[ w ], nperf, comp_frac, cells, WI, sat_table_id, W->name[ w ], W->allow_cf[ w ], newWells); @@ -565,9 +549,8 @@ wells_equal(const struct Wells *W1, const struct Wells *W2 , bool verbose) return false; } - bool are_equal = true; - are_equal = (W1->number_of_wells == W2->number_of_wells); - are_equal = are_equal && (W1->number_of_phases == W2->number_of_phases); + bool are_equal = (W1->number_of_wells == W2->number_of_wells) && + (W1->number_of_phases == W2->number_of_phases); if (!are_equal) { return are_equal; }