From 7d57530df5f700a26a83c5f32627351a59af3971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Mon, 21 May 2012 15:23:36 +0200 Subject: [PATCH] Implement compute_sequence*() in terms of compute_reorder_sequence_graph(). While here, insert various white-space adjustments. --- .../transport/reorder/reordersequence.cpp | 151 ++++++++++-------- 1 file changed, 81 insertions(+), 70 deletions(-) diff --git a/opm/core/transport/reorder/reordersequence.cpp b/opm/core/transport/reorder/reordersequence.cpp index 8f5be7b31..5458ef0f7 100644 --- a/opm/core/transport/reorder/reordersequence.cpp +++ b/opm/core/transport/reorder/reordersequence.cpp @@ -27,11 +27,20 @@ struct SortByAbsFlux const double* flux_; }; + /* Construct adjacency matrix of upwind graph wrt flux. Column indices are not sorted. */ +// --------------------------------------------------------------------- static void -make_upwind_graph(int nc, int *cellfaces, int *faceptr, int *face2cell, - const double *flux, int *ia, int *ja, int *work) +make_upwind_graph(int nc , + const int *cellfaces, + const int *faceptr , + const int *face2cell, + const double *flux , + int *ia , + int *ja , + int *work ) +// --------------------------------------------------------------------- { /* Using topology (conn, cptr), and direction, construct adjacency matrix of graph. */ @@ -90,27 +99,55 @@ make_upwind_graph(int nc, int *cellfaces, int *faceptr, int *face2cell, } } + +// --------------------------------------------------------------------- static void -compute_reorder_sequence(int nc, int nf, int *cellfaces, int *faceptr, int *face2cell, - const double *flux, int *sequence, int *components, int *ncomponents) +compute_reorder_sequence_graph(int nc, + const int *cellfaces, + const int *facepos, + const int *face2cell, + const double *flux, + int *sequence, + int *components, + int *ncomponents, + int *ia, int *ja, int* work) +// --------------------------------------------------------------------- { - int *ia, *ja; - int *work; - int sz = nf; - if (nf < 3*nc) - { - sz = 3*nc; - } - - work = (int*) std::malloc( sz * sizeof *work); - ia = (int*) std::malloc((nc+1) * sizeof *ia); - ja = (int*) std::malloc( nf * sizeof *ja); /* A bit too much... */ - - - make_upwind_graph(nc, cellfaces, faceptr, face2cell, + make_upwind_graph(nc, cellfaces, facepos, face2cell, flux, ia, ja, work); tarjan (nc, ia, ja, sequence, components, ncomponents, work); +} + + +// --------------------------------------------------------------------- +void +compute_sequence(const struct UnstructuredGrid* grid , + const double* flux , + int* sequence , + int* components , + int* ncomponents) +// --------------------------------------------------------------------- +{ + const std::size_t nc = grid->number_of_cells; + const std::size_t nf = grid->number_of_faces; + const std::size_t sz = std::max(nf, 3 * nc); + + int* work = (int*) std::malloc( sz * sizeof *work); + int* ia = (int*) std::malloc((nc + 1) * sizeof *ia ); + int* ja = (int*) std::malloc( nf * sizeof *ja ); /* A bit too much... */ + + if (work && ia && ja) { + compute_reorder_sequence_graph(grid->number_of_cells, + grid->cell_faces, + grid->cell_facepos, + grid->face_cells, + flux, + sequence, + components, + ncomponents, + ia, ja, work); + } std::free(ja); std::free(ia); @@ -118,65 +155,39 @@ compute_reorder_sequence(int nc, int nf, int *cellfaces, int *faceptr, int *face } -static void -compute_reorder_sequence_graph(int nc, int nf, int *cellfaces, int *faceptr, int *face2cell, - const double *flux, int *sequence, int *components, int *ncomponents, - int *ia, int *ja) +// --------------------------------------------------------------------- +void +compute_sequence_graph(const struct UnstructuredGrid* grid , + const double* flux , + int* sequence , + int* components , + int* ncomponents, + int* ia , + int* ja ) +// --------------------------------------------------------------------- { - int *work; - int sz = nf; - if (nf < 3*nc) - { - sz = 3*nc; + const std::size_t nc = grid->number_of_cells; + const std::size_t nf = grid->number_of_faces; + const std::size_t sz = std::max(nf, 3 * nc); + + int* work = (int*) std::malloc(sz * sizeof *work); + + if (work) { + compute_reorder_sequence_graph(grid->number_of_cells, + grid->cell_faces, + grid->cell_facepos, + grid->face_cells, + flux, + sequence, + components, + ncomponents, + ia, ja, work); } - work = (int*) std::malloc( sz * sizeof *work); - - - make_upwind_graph(nc, cellfaces, faceptr, face2cell, - flux, ia, ja, work); - - tarjan (nc, ia, ja, sequence, components, ncomponents, work); - std::free(work); } -void compute_sequence(const struct UnstructuredGrid *grid, const double *flux, - int *sequence, - int *components, int *ncomponents) -{ - - compute_reorder_sequence(grid->number_of_cells, - grid->number_of_faces, - grid->cell_faces, - grid->cell_facepos, - grid->face_cells, - flux, - sequence, - components, - ncomponents); -} - -void compute_sequence_graph(const struct UnstructuredGrid *grid, const double *flux, - int *sequence, - int *components, int *ncomponents, - int *ia, int *ja) -{ - - compute_reorder_sequence_graph(grid->number_of_cells, - grid->number_of_faces, - grid->cell_faces, - grid->cell_facepos, - grid->face_cells, - flux, - sequence, - components, - ncomponents, - ia, ja); -} - - /* Local Variables: */ /* c-basic-offset:4 */ /* End: */