From 76deba58bdef5f6d121b2e9d7ae1b91416f23f42 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Sun, 5 Jan 2014 14:47:15 +0100 Subject: [PATCH] Moved well_controls implementation to separate file well_controls.c. --- CMakeLists_files.cmake | 1 + opm/core/well_controls.h | 19 +++- opm/core/wells/well_controls.c | 175 +++++++++++++++++++++++++++++++++ opm/core/wells/wells.c | 130 +----------------------- 4 files changed, 194 insertions(+), 131 deletions(-) create mode 100644 opm/core/wells/well_controls.c diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index bfe0f255..54329c24 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -140,6 +140,7 @@ list (APPEND MAIN_SOURCE_FILES opm/core/wells/WellsGroup.cpp opm/core/wells/WellsManager.cpp opm/core/wells/wells.c + opm/core/wells/well_controls.c ) # originally generated with the command: diff --git a/opm/core/well_controls.h b/opm/core/well_controls.h index 80d97c7e..0d5fd388 100644 --- a/opm/core/well_controls.h +++ b/opm/core/well_controls.h @@ -55,6 +55,7 @@ extern "C" { * BHP constraint defines a minimum acceptable bottom-hole pressure * value for the well. */ + struct WellControls { /** @@ -91,10 +92,24 @@ struct WellControls void *data; }; - -bool +bool well_controls_equal(const struct WellControls *ctrls1, const struct WellControls *ctrls2); +int +well_controls_reserve(int nctrl, int nphases, struct WellControls *ctrl); + +struct WellControls * +well_controls_create(void); + +void +well_controls_destroy(struct WellControls *ctrl); + + +struct WellControlMgmt { + int cpty; +}; + + #ifdef __cplusplus } diff --git a/opm/core/wells/well_controls.c b/opm/core/wells/well_controls.c new file mode 100644 index 00000000..6ebdbcaf --- /dev/null +++ b/opm/core/wells/well_controls.c @@ -0,0 +1,175 @@ +/*=========================================================================== +// +// File: newwells.c +// +// Created: 2012-02-03 11:28:40+0100 +// +// Authors: Knut-Andreas Lie +// Jostein R. Natvig +// Halvor M. Nilsen +// Atgeirr F. Rasmussen +// Xavier Raynaud +// Bård Skaflestad +// +//==========================================================================*/ + + +/* + Copyright 2012 SINTEF ICT, Applied Mathematics. + Copyright 2012 Statoil ASA. + + This file is part of the Open Porous Media Project (OPM). + + OPM 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 3 of the License, or + (at your option) any later version. + + OPM 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 OPM. If not, see . +*/ + +#include "config.h" + +#include + +#include +#include + + +static void +destroy_ctrl_mgmt(struct WellControlMgmt *m) +{ + free(m); +} + + +static struct WellControlMgmt * +create_ctrl_mgmt(void) +{ + struct WellControlMgmt *m; + + m = malloc(1 * sizeof *m); + + if (m != NULL) { + m->cpty = 0; + } + + return m; +} + + + +/* ---------------------------------------------------------------------- */ +void +well_controls_destroy(struct WellControls *ctrl) +/* ---------------------------------------------------------------------- */ +{ + if (ctrl != NULL) { + destroy_ctrl_mgmt(ctrl->data); + free (ctrl->distr); + free (ctrl->target); + free (ctrl->type); + } + + free(ctrl); +} + + +/* ---------------------------------------------------------------------- */ +struct WellControls * +well_controls_create(void) +/* ---------------------------------------------------------------------- */ +{ + struct WellControls *ctrl; + + ctrl = malloc(1 * sizeof *ctrl); + + if (ctrl != NULL) { + /* Initialise empty control set */ + ctrl->num = 0; + ctrl->number_of_phases = 0; + ctrl->type = NULL; + ctrl->target = NULL; + ctrl->distr = NULL; + ctrl->current = -1; + + ctrl->data = create_ctrl_mgmt(); + + if (ctrl->data == NULL) { + well_controls_destroy(ctrl); + ctrl = NULL; + } + } + + return ctrl; +} + + +/* ---------------------------------------------------------------------- */ +int +well_controls_reserve(int nctrl, int nphases, struct WellControls *ctrl) +/* ---------------------------------------------------------------------- */ +{ + int c, p, ok; + void *type, *target, *distr; + + struct WellControlMgmt *m; + + type = realloc(ctrl->type , nctrl * 1 * sizeof *ctrl->type ); + target = realloc(ctrl->target, nctrl * 1 * sizeof *ctrl->target); + distr = realloc(ctrl->distr , nctrl * nphases * sizeof *ctrl->distr ); + + ok = 0; + if (type != NULL) { ctrl->type = type ; ok++; } + if (target != NULL) { ctrl->target = target; ok++; } + if (distr != NULL) { ctrl->distr = distr ; ok++; } + + ctrl->number_of_phases = nphases; + + if (ok == 3) { + m = ctrl->data; + for (c = m->cpty; c < nctrl; c++) { + ctrl->type [c] = BHP; + ctrl->target[c] = -1.0; + } + + for (p = m->cpty * ctrl->number_of_phases; p < nctrl * ctrl->number_of_phases; ++p) { + ctrl->distr[ p ] = 0.0; + } + + m->cpty = nctrl; + } + + return ok == 3; +} + + + + +bool +well_controls_equal(const struct WellControls *ctrls1, const struct WellControls *ctrls2) +/* ---------------------------------------------------------------------- */ +{ + bool are_equal = true; + are_equal = (ctrls1->num == ctrls2->num); + are_equal &= (ctrls1->number_of_phases == ctrls2->number_of_phases); + if (!are_equal) { + return are_equal; + } + + are_equal &= (memcmp(ctrls1->type, ctrls2->type, ctrls1->num * sizeof *ctrls1->type ) == 0); + are_equal &= (memcmp(ctrls1->target, ctrls2->target, ctrls1->num * sizeof *ctrls1->target ) == 0); + are_equal &= (memcmp(ctrls1->distr, ctrls2->distr, ctrls1->num * ctrls1->number_of_phases * sizeof *ctrls1->distr ) == 0); + + struct WellControlMgmt* mgmt1 = (struct WellControlMgmt*)(ctrls1->data); + struct WellControlMgmt* mgmt2 = (struct WellControlMgmt*)(ctrls2->data); + are_equal &= (mgmt1->cpty == mgmt2->cpty); + + return are_equal; +} diff --git a/opm/core/wells/wells.c b/opm/core/wells/wells.c index d4208ad5..312c5432 100644 --- a/opm/core/wells/wells.c +++ b/opm/core/wells/wells.c @@ -35,6 +35,7 @@ */ #include "config.h" #include +#define HAVE_WELLCONTROLS #include #include @@ -44,9 +45,6 @@ #include -struct WellControlMgmt { - int cpty; -}; struct WellMgmt { int well_cpty; @@ -54,28 +52,6 @@ struct WellMgmt { }; -static void -destroy_ctrl_mgmt(struct WellControlMgmt *m) -{ - free(m); -} - - -static struct WellControlMgmt * -create_ctrl_mgmt(void) -{ - struct WellControlMgmt *m; - - m = malloc(1 * sizeof *m); - - if (m != NULL) { - m->cpty = 0; - } - - return m; -} - - static void destroy_well_mgmt(struct WellMgmt *m) { @@ -99,89 +75,6 @@ create_well_mgmt(void) } -/* ---------------------------------------------------------------------- */ -static void -well_controls_destroy(struct WellControls *ctrl) -/* ---------------------------------------------------------------------- */ -{ - if (ctrl != NULL) { - destroy_ctrl_mgmt(ctrl->data); - free (ctrl->distr); - free (ctrl->target); - free (ctrl->type); - } - - free(ctrl); -} - - -/* ---------------------------------------------------------------------- */ -static struct WellControls * -well_controls_create(void) -/* ---------------------------------------------------------------------- */ -{ - struct WellControls *ctrl; - - ctrl = malloc(1 * sizeof *ctrl); - - if (ctrl != NULL) { - /* Initialise empty control set */ - ctrl->num = 0; - ctrl->number_of_phases = 0; - ctrl->type = NULL; - ctrl->target = NULL; - ctrl->distr = NULL; - ctrl->current = -1; - - ctrl->data = create_ctrl_mgmt(); - - if (ctrl->data == NULL) { - well_controls_destroy(ctrl); - ctrl = NULL; - } - } - - return ctrl; -} - - -/* ---------------------------------------------------------------------- */ -static int -well_controls_reserve(int nctrl, int nphases, struct WellControls *ctrl) -/* ---------------------------------------------------------------------- */ -{ - int c, p, ok; - void *type, *target, *distr; - - struct WellControlMgmt *m; - - type = realloc(ctrl->type , nctrl * 1 * sizeof *ctrl->type ); - target = realloc(ctrl->target, nctrl * 1 * sizeof *ctrl->target); - distr = realloc(ctrl->distr , nctrl * nphases * sizeof *ctrl->distr ); - - ok = 0; - if (type != NULL) { ctrl->type = type ; ok++; } - if (target != NULL) { ctrl->target = target; ok++; } - if (distr != NULL) { ctrl->distr = distr ; ok++; } - - ctrl->number_of_phases = nphases; - - if (ok == 3) { - m = ctrl->data; - for (c = m->cpty; c < nctrl; c++) { - ctrl->type [c] = BHP; - ctrl->target[c] = -1.0; - } - - for (p = m->cpty * ctrl->number_of_phases; p < nctrl * ctrl->number_of_phases; ++p) { - ctrl->distr[ p ] = 0.0; - } - - m->cpty = nctrl; - } - - return ok == 3; -} /* ---------------------------------------------------------------------- */ @@ -718,24 +611,3 @@ wells_equal(const struct Wells *W1, const struct Wells *W2) } /* ---------------------------------------------------------------------- */ -bool -well_controls_equal(const struct WellControls *ctrls1, const struct WellControls *ctrls2) -/* ---------------------------------------------------------------------- */ -{ - bool are_equal = true; - are_equal = (ctrls1->num == ctrls2->num); - are_equal &= (ctrls1->number_of_phases == ctrls2->number_of_phases); - if (!are_equal) { - return are_equal; - } - - are_equal &= (memcmp(ctrls1->type, ctrls2->type, ctrls1->num * sizeof *ctrls1->type ) == 0); - are_equal &= (memcmp(ctrls1->target, ctrls2->target, ctrls1->num * sizeof *ctrls1->target ) == 0); - are_equal &= (memcmp(ctrls1->distr, ctrls2->distr, ctrls1->num * ctrls1->number_of_phases * sizeof *ctrls1->distr ) == 0); - - struct WellControlMgmt* mgmt1 = (struct WellControlMgmt*)(ctrls1->data); - struct WellControlMgmt* mgmt2 = (struct WellControlMgmt*)(ctrls2->data); - are_equal &= (mgmt1->cpty == mgmt2->cpty); - - return are_equal; -}