From bde8b53a7dbb256e49a0324761f9085dc93ad5b1 Mon Sep 17 00:00:00 2001 From: "Halvor M. Nilsen" Date: Fri, 8 Jun 2012 15:34:00 +0200 Subject: [PATCH 1/7] Add support for reading vag/(unstructured format) --- Makefile.am | 7 +- opm/core/vag_format/vag.cpp | 135 ++++++++++++++++++++++++++++++++++++ opm/core/vag_format/vag.hpp | 55 +++++++++++++++ tests/Makefile.am | 5 +- tests/test_read_vag.cpp | 24 +++++++ 5 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 opm/core/vag_format/vag.cpp create mode 100644 opm/core/vag_format/vag.hpp create mode 100644 tests/test_read_vag.cpp diff --git a/Makefile.am b/Makefile.am index 3d79e38f..420986a6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -118,7 +118,8 @@ opm/core/transport/reorder/TransportModelCompressibleTwophase.cpp \ opm/core/transport/reorder/TransportModelTwophase.cpp \ opm/core/transport/reorder/reordersequence.cpp \ opm/core/transport/reorder/nlsolvers.c \ -opm/core/transport/reorder/tarjan.c +opm/core/transport/reorder/tarjan.c \ +opm/core/vag_format/vag.cpp nobase_include_HEADERS = \ opm/core/eclipse/CornerpointChopper.hpp \ @@ -250,8 +251,8 @@ opm/core/transport/reorder/TransportModelCompressibleTwophase.hpp \ opm/core/transport/reorder/TransportModelTwophase.hpp \ opm/core/transport/reorder/nlsolvers.h \ opm/core/transport/reorder/reordersequence.h \ -opm/core/transport/reorder/tarjan.h - +opm/core/transport/reorder/tarjan.h \ +opm/core/vag_format/vag.hpp # ---------------------------------------------------------------------- # Optional library constituents. diff --git a/opm/core/vag_format/vag.cpp b/opm/core/vag_format/vag.cpp new file mode 100644 index 00000000..e4cce4fd --- /dev/null +++ b/opm/core/vag_format/vag.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +namespace OPM +{ + void readPosStruct(std::istream& is,int n,PosStruct& pos_struct){ + using namespace std; + //PosStruct pos_struct; + pos_struct.pos.resize(n+1); + pos_struct.pos[0]=0; + for(int i=0;i< n;++i){ + int number; + is >> number ; + //cout <> value; + // cout << value << " "; + pos_struct.value.push_back(value); + } + //cout << endl; + } + if(!(pos_struct.value.size()==pos_struct.pos[n])){ + cerr << "Failed to read pos structure" << endl; + cerr << "pos_struct.value.size()" << pos_struct.value.size() << endl; + cerr << "pos_struct.pos[n+1]" << pos_struct.pos[n] << endl; + } + }; + void readVagGrid(std::istream& is,OPM::VAG& vag_grid){ + using namespace std; + using namespace OPM; + while (!is.eof()) { + string keyword; + is >> keyword; + //cout << keyword<< endl; + if(keyword == "Number"){ + string stmp; + is >> stmp; + if(stmp == "of"){ + string entity; + is >> entity; + getline(is,stmp); + int number; + is >> number; + if(entity=="vertices"){ + vag_grid.number_of_vertices=number; + }else if((entity=="volumes") || (entity=="control")){ + vag_grid.number_of_volumes=number; + }else if(entity=="faces"){ + vag_grid.number_of_faces=number; + }else if(entity=="edges"){ + vag_grid.number_of_edges=number; + } + cout << "Found Number of: " << entity <<" " << number << endl; + } else { + cerr << "Wrong format: Not of after Number" << endl; + return; + } + }else{ + // read geometry defined by vertices + if(keyword=="Vertices"){ + int number; + is >> number; + vag_grid.vertices.resize(3*number);// assume 3d data + readVector(is,vag_grid.vertices); + } + // here starts the reding of all pos structures + else if(keyword=="Volumes->Faces" || keyword=="Volumes->faces"){ + //vag_grid.volumes_to_faces= + int number; + is >> number; + readPosStruct(is,number,vag_grid.volumes_to_faces); + cout << "Volumes->Faces: Number of " << number << endl; + }else if(keyword=="Faces->edges" || keyword=="Faces->Edges" || keyword=="Faces->Edgess"){ + int number; + is >> number; + //vag_grid.volumes_to_faces= + readPosStruct(is,number,vag_grid.faces_to_edges); + cout << "Faces->edges: Number of " << number << endl; + }else if(keyword=="Faces->Vertices" || keyword=="Faces->vertices"){ + int number; + is >> number; + //vag_grid.volumes_to_faces= + readPosStruct(is,number,vag_grid.faces_to_vertices); + cout << "Faces->Vertices: Number of " << number << endl; + }else if(keyword=="Volumes->Vertices" || keyword=="Volumes->Verticess"){ + int number; + is >> number; + //vag_grid.volumes_to_faces= + readPosStruct(is,number,vag_grid.volumes_to_vertices); + cout << "Volumes->Vertices: Number of " << number << endl; + } + + // read simple mappings + else if(keyword=="Edge" || keyword=="Edges"){ + int number; + is >> number; + vag_grid.edges.resize(2*number); + readVector(is,vag_grid.edges); + cout << "Edges: Number of " << number << endl; + }else if(keyword=="Faces->Volumes" || keyword=="Faces->Control"){ + int number; + if(keyword=="Faces->Control"){ + string vol; + is >> vol; + } + is >> number; + vag_grid.faces_to_volumes.resize(2*number); + readVector(is,vag_grid.faces_to_volumes); + cout << "Faces->Volumes: Number of " << number << endl; + } + // read material + else if(keyword=="Material"){ + string snum; + is >> snum; + int number; + is >> number; + cout << "Material number " << number << endl; + // we read all the rest into doubles + while(!is.eof()){ + double value; + is >> value; + //cout << value << endl; + vag_grid.material.push_back(value); + } + }else{ + //cout << "keyword; + } + //cout << "Found" << keyword << "Number of " << number << endl; + } + } + }; +} diff --git a/opm/core/vag_format/vag.hpp b/opm/core/vag_format/vag.hpp new file mode 100644 index 00000000..772a10d3 --- /dev/null +++ b/opm/core/vag_format/vag.hpp @@ -0,0 +1,55 @@ +#ifndef OPM_VAG_HEADER_INCLUDED +#define OPM_VAG_HEADER_INCLUDED + +#include +#include +#include +#include +namespace OPM +{ + /* Struct to hold maping from the natural number less than pos.size()-1 to + a set of integers. value(pos(i):pos(i+1)-1) hold the integers corresponding to i. + pos(end)-1==value.size(); + */ + struct PosStruct{ + std::vector pos; + std::vector value; + }; + /* Structure to represent the unstructured vag grid format + */ + struct VAG{ + int number_of_vertices; + int number_of_volumes; + int number_of_faces; + int number_of_edges; + std::vector vertices; + PosStruct volumes_to_faces; + PosStruct volumes_to_vertices; + PosStruct faces_to_edges; + PosStruct faces_to_vertices; + std::vector edges; + std::vector faces_to_volumes; + std::vector material; + }; + /* Function the vag grid format and make a vag_grid struct. This structure + is intended to be converted to a grid*/ + void readVagGrid(std::istream& is,OPM::VAG& vag_grid); + /* + void writeVagFormat(std::ostream& os){ + using namespace std; + os << "File in the Vag grid format" << endl; + }; + */ + template + void readVector(std::istream& is,std::vector& vec){ + using namespace std; + for(int i=0;i< vec.size();++i){ + is >> vec[i]; + } + } + + //PosStruct readPosStruct(std::istream& is,int n){ + void readPosStruct(std::istream& is,int n,PosStruct& pos_struct); +} + +#endif // OPE diff --git a/tests/Makefile.am b/tests/Makefile.am index 064d385c..a37cf4ae 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -21,7 +21,8 @@ test_readpolymer \ test_readvector \ test_sf2p \ test_writeVtkData \ -unit_test +unit_test \ +test_read_vag bo_resprop_test_SOURCES = bo_resprop_test.cpp monotcubicinterpolator_test_SOURCES = monotcubicinterpolator_test.cpp @@ -41,6 +42,8 @@ test_readvector_SOURCES = test_readvector.cpp test_sf2p_SOURCES = test_sf2p.cpp test_writeVtkData_SOURCES = test_writeVtkData.cpp unit_test_SOURCES = unit_test.cpp +test_read_vag_SOURCES = test_read_vag.cpp +test_read_vag_LDADD = $(LDADD) if UMFPACK noinst_PROGRAMS += test_cfs_tpfa diff --git a/tests/test_read_vag.cpp b/tests/test_read_vag.cpp new file mode 100644 index 00000000..5ced4816 --- /dev/null +++ b/tests/test_read_vag.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +//#include "../config.h" +/* test reading of vag grid format */ +int main(int argc, char** argv) +{ + using namespace std; + using namespace OPM; + std::string filename; + if (argc == 2) { + filename = argv[1]; + } else { + std::cout << "\nUsage: test_read_vag filename\n"; + exit( 1 ); + } + ifstream is(filename.c_str());//"/home/hnil/heim/SVN/simmatlab/projects/clastic/utils/unstructuredgrids/data/3x3_w_layered-vag.dat"); + //ifstream is("/home/hnil/heim/SVN/simmatlab/projects/clastic/utils/unstructuredgrids/data/test.txt"); + //std::ofstream is(""); + VAG vag_grid; + readVagGrid(is,vag_grid); +} From 839920fab210c458481593a15679902b2140e388 Mon Sep 17 00:00:00 2001 From: "Halvor M. Nilsen" Date: Fri, 8 Jun 2012 15:47:18 +0200 Subject: [PATCH 2/7] Added copyright on newly added files --- opm/core/vag_format/vag.cpp | 35 ++++++++++++++++++++++++++++ opm/core/vag_format/vag.hpp | 46 +++++++++++++++++++++++++++++++++---- tests/test_read_vag.cpp | 34 +++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) diff --git a/opm/core/vag_format/vag.cpp b/opm/core/vag_format/vag.cpp index e4cce4fd..8f920fdf 100644 --- a/opm/core/vag_format/vag.cpp +++ b/opm/core/vag_format/vag.cpp @@ -1,3 +1,38 @@ +/*=========================================================================== +// +// File: vag.cpp +// +// Created: 2012-06-08 15:45:53+0200 +// +// Authors: Knut-Andreas Lie +// 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 #include #include diff --git a/opm/core/vag_format/vag.hpp b/opm/core/vag_format/vag.hpp index 772a10d3..2f4f096f 100644 --- a/opm/core/vag_format/vag.hpp +++ b/opm/core/vag_format/vag.hpp @@ -1,5 +1,43 @@ -#ifndef OPM_VAG_HEADER_INCLUDED -#define OPM_VAG_HEADER_INCLUDED +/*=========================================================================== +// +// File: vag.hpp +// +// Created: 2012-06-08 15:46:23+0200 +// +// Authors: Knut-Andreas Lie +// 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 . +*/ + +#ifndef OPM_VAG_HPP_HEADER +#define OPM_VAG_HPP_HEADER + + + #include #include @@ -51,5 +89,5 @@ namespace OPM //PosStruct readPosStruct(std::istream& is,int n){ void readPosStruct(std::istream& is,int n,PosStruct& pos_struct); } - -#endif // OPE +#endif /* OPM_VAG_HPP_HEADER */ + diff --git a/tests/test_read_vag.cpp b/tests/test_read_vag.cpp index 5ced4816..eeb9c88a 100644 --- a/tests/test_read_vag.cpp +++ b/tests/test_read_vag.cpp @@ -1,3 +1,37 @@ +/*=========================================================================== +// +// File: test_read_vag.cpp +// +// Created: 2012-06-08 15:44:43+0200 +// +// Authors: Knut-Andreas Lie +// 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 #include #include From efc3cc3324a6a8997d2ab807e21376e3e76f7bb2 Mon Sep 17 00:00:00 2001 From: "Halvor M. Nilsen" Date: Fri, 8 Jun 2012 20:50:41 +0200 Subject: [PATCH 3/7] Added convert function from vag grid to opm::unstructed. In the process it was added a convenience function to allocate unstructured grid in grid.h. Compute geometry is used to calculate the new geometry. --- opm/core/grid.c | 76 ++++++++++++++++++++++++++++++++++++- opm/core/grid.h | 10 ++++- opm/core/vag_format/vag.cpp | 48 +++++++++++++++++++++-- opm/core/vag_format/vag.hpp | 7 ++-- tests/test_read_vag.cpp | 11 ++++++ 5 files changed, 144 insertions(+), 8 deletions(-) diff --git a/opm/core/grid.c b/opm/core/grid.c index 3f8f6620..905a9afc 100644 --- a/opm/core/grid.c +++ b/opm/core/grid.c @@ -19,7 +19,7 @@ #include #include - +#include void destroy_grid(struct UnstructuredGrid *g) { if (g!=NULL) @@ -58,3 +58,77 @@ create_grid_empty(void) return G; } + +struct UnstructuredGrid * +allocate_grid(size_t ndims , + size_t ncells, + size_t nfaces, + size_t nfacenodes, + size_t ncellfaces, + size_t nnodes) +{ + size_t nel; + struct UnstructuredGrid *G; + + G = create_grid_empty(); + + if (G != NULL) { + /* Node fields ---------------------------------------- */ + nel = nnodes * ndims; + G->node_coordinates = malloc(nel * sizeof *G->node_coordinates); + + /* Face fields ---------------------------------------- */ + nel = nfacenodes; + G->face_nodes = malloc(nel * sizeof *G->face_nodes); + + nel = nfaces + 1; + G->face_nodepos = malloc(nel * sizeof *G->face_nodepos); + + nel = 2 * nfaces; + G->face_cells = malloc(nel * sizeof *G->face_cells); + + nel = nfaces * ndims; + G->face_centroids = malloc(nel * sizeof *G->face_centroids); + + nel = nfaces * ndims; + G->face_normals = malloc(nel * sizeof *G->face_normals); + + nel = nfaces * 1; + G->face_areas = malloc(nel * sizeof *G->face_areas); + + + /* Cell fields ---------------------------------------- */ + nel = ncellfaces; + G->cell_faces = malloc(nel * sizeof *G->cell_faces); + + nel = ncells + 1; + G->cell_facepos = malloc(nel * sizeof *G->cell_facepos); + + nel = ncells * ndims; + G->cell_centroids = malloc(nel * sizeof *G->cell_centroids); + + nel = ncells * 1; + G->cell_volumes = malloc(nel * sizeof *G->cell_volumes); + + if ((G->node_coordinates == NULL) || + (G->face_nodes == NULL) || + (G->face_nodepos == NULL) || + (G->face_cells == NULL) || + (G->face_centroids == NULL) || + (G->face_normals == NULL) || + (G->face_areas == NULL) || + (G->cell_faces == NULL) || + (G->cell_facepos == NULL) || + (G->cell_centroids == NULL) || + (G->cell_volumes == NULL) ) + { + destroy_grid(G); + G = NULL; + } + } + + return G; +} + + + diff --git a/opm/core/grid.h b/opm/core/grid.h index 8a8d63d1..e09adfed 100644 --- a/opm/core/grid.h +++ b/opm/core/grid.h @@ -26,7 +26,7 @@ * Main OPM-Core grid data structure along with destructor and default * constructor. */ - +#include #ifdef __cplusplus extern "C" { #endif @@ -238,6 +238,14 @@ void destroy_grid(struct UnstructuredGrid *g); */ struct UnstructuredGrid * create_grid_empty(void); + +struct UnstructuredGrid * + allocate_grid(size_t ndims , + size_t ncells, + size_t nfaces, + size_t nfacenodes, + size_t ncellfaces, + size_t nnodes); #ifdef __cplusplus } diff --git a/opm/core/vag_format/vag.cpp b/opm/core/vag_format/vag.cpp index 8f920fdf..4996091d 100644 --- a/opm/core/vag_format/vag.cpp +++ b/opm/core/vag_format/vag.cpp @@ -34,9 +34,11 @@ */ #include +#include #include #include #include +#include namespace OPM { void readPosStruct(std::istream& is,int n,PosStruct& pos_struct){ @@ -57,12 +59,12 @@ namespace OPM } //cout << endl; } - if(!(pos_struct.value.size()==pos_struct.pos[n])){ + if(!(int(pos_struct.value.size())==pos_struct.pos[n])){ cerr << "Failed to read pos structure" << endl; cerr << "pos_struct.value.size()" << pos_struct.value.size() << endl; cerr << "pos_struct.pos[n+1]" << pos_struct.pos[n] << endl; } - }; + } void readVagGrid(std::istream& is,OPM::VAG& vag_grid){ using namespace std; using namespace OPM; @@ -166,5 +168,45 @@ namespace OPM //cout << "Found" << keyword << "Number of " << number << endl; } } - }; + } + void vagToUnstructuredGrid(OPM::VAG& vag_grid,UnstructuredGrid& grid){ + using namespace std; + using namespace OPM; + cout << "Converting grid" << endl; + grid.dimensions=3; + grid.number_of_cells=vag_grid.number_of_volumes; + grid.number_of_faces=vag_grid.number_of_faces; + grid.number_of_faces=vag_grid.number_of_faces; + // fill face_nodes + for(int i=0;i< int(vag_grid.faces_to_vertices.pos.size());++i){ + grid.face_nodepos[i] = vag_grid.faces_to_vertices.pos[i]; + } + for(int i=0;i< int(vag_grid.faces_to_vertices.value.size());++i){ + grid.face_nodes[i] = vag_grid.faces_to_vertices.value[i]-1; + } + // fill cell_face + for(int i=0;i< int(vag_grid.volumes_to_faces.pos.size());++i){ + grid.cell_facepos[i] = vag_grid.volumes_to_faces.pos[i]; + } + for(int i=0;i< int(vag_grid.volumes_to_faces.value.size());++i){ + grid.cell_faces[i] = vag_grid.volumes_to_faces.value[i]-1; + } + // fill face_cells + for(int i=0;i< int(vag_grid.faces_to_volumes.size());++i){ + grid.face_cells[i] = vag_grid.faces_to_volumes[i]-1; + } + + // fill node_cordinates. This is the only geometry given in the vag + for(int i=0;i< int(vag_grid.vertices.size());++i){ + grid.node_coordinates[i] = vag_grid.vertices[i]; + } + // informations in edges, faces_to_eges, faces_to_vertices, volume_to_vertices and materials + // is not used + cout << "Computing geometry" << endl; + compute_geometry(&grid); + + + + } + } diff --git a/opm/core/vag_format/vag.hpp b/opm/core/vag_format/vag.hpp index 2f4f096f..2f9d8c74 100644 --- a/opm/core/vag_format/vag.hpp +++ b/opm/core/vag_format/vag.hpp @@ -38,7 +38,7 @@ - +#include #include #include #include @@ -81,13 +81,14 @@ namespace OPM template void readVector(std::istream& is,std::vector& vec){ using namespace std; - for(int i=0;i< vec.size();++i){ + for(int i=0;i< int(vec.size());++i){ is >> vec[i]; } } //PosStruct readPosStruct(std::istream& is,int n){ - void readPosStruct(std::istream& is,int n,PosStruct& pos_struct); + void readPosStruct(std::istream& is,int n,PosStruct& pos_struct); + void vagToUnstructuredGrid(OPM::VAG& vag_grid,UnstructuredGrid& grid); } #endif /* OPM_VAG_HPP_HEADER */ diff --git a/tests/test_read_vag.cpp b/tests/test_read_vag.cpp index eeb9c88a..c86b753e 100644 --- a/tests/test_read_vag.cpp +++ b/tests/test_read_vag.cpp @@ -37,6 +37,7 @@ #include #include #include +#include //#include "../config.h" /* test reading of vag grid format */ int main(int argc, char** argv) @@ -55,4 +56,14 @@ int main(int argc, char** argv) //std::ofstream is(""); VAG vag_grid; readVagGrid(is,vag_grid); + UnstructuredGrid *grid;// make a pointer, can it be avoided?? + grid = allocate_grid(3, + vag_grid.number_of_volumes, + vag_grid.number_of_faces, + vag_grid.faces_to_vertices.value.size(), + vag_grid.volumes_to_faces.value.size(), + vag_grid.number_of_vertices); + vagToUnstructuredGrid(vag_grid,*grid); + destroy_grid(grid); + } From 6287f53b2fc775eb67bfb5602a2dc523cab00855 Mon Sep 17 00:00:00 2001 From: "Halvor M. Nilsen" Date: Mon, 11 Jun 2012 10:34:40 +0200 Subject: [PATCH 4/7] Added then allocate_grid in grid and updated documentation in vag.hpp and grid.h --- opm/core/grid.c | 82 ++++++++++++++++++------------------- opm/core/grid.h | 38 +++++++++++++---- opm/core/vag_format/vag.hpp | 52 +++++++++++++++++++---- 3 files changed, 115 insertions(+), 57 deletions(-) diff --git a/opm/core/grid.c b/opm/core/grid.c index 905a9afc..8f8746af 100644 --- a/opm/core/grid.c +++ b/opm/core/grid.c @@ -19,26 +19,28 @@ #include #include -#include -void destroy_grid(struct UnstructuredGrid *g) + + +void +destroy_grid(struct UnstructuredGrid *g) { if (g!=NULL) { - free(g->face_nodes); - free(g->face_nodepos); - free(g->face_cells); - free(g->cell_facepos); - free(g->cell_faces); + free(g->face_nodes); + free(g->face_nodepos); + free(g->face_cells); + free(g->cell_facepos); + free(g->cell_faces); - free(g->node_coordinates); - free(g->face_centroids); - free(g->face_areas); - free(g->face_normals); - free(g->cell_centroids); - free(g->cell_volumes); + free(g->node_coordinates); + free(g->face_centroids); + free(g->face_areas); + free(g->face_normals); + free(g->cell_centroids); + free(g->cell_volumes); - free(g->global_cell); - free(g->cell_facetag); + free(g->global_cell); + free(g->cell_facetag); } free(g); @@ -53,82 +55,80 @@ create_grid_empty(void) G = malloc(1 * sizeof *G); if (G != NULL) { - *G = g; + *G = g; } return G; } + struct UnstructuredGrid * -allocate_grid(size_t ndims , - size_t ncells, - size_t nfaces, +allocate_grid(size_t ndims , + size_t ncells , + size_t nfaces , size_t nfacenodes, size_t ncellfaces, - size_t nnodes) + size_t nnodes ) { size_t nel; struct UnstructuredGrid *G; - + G = create_grid_empty(); - + if (G != NULL) { /* Node fields ---------------------------------------- */ nel = nnodes * ndims; - G->node_coordinates = malloc(nel * sizeof *G->node_coordinates); - + G->node_coordinates = malloc(nel * sizeof *G->node_coordinates); + /* Face fields ---------------------------------------- */ nel = nfacenodes; G->face_nodes = malloc(nel * sizeof *G->face_nodes); - + nel = nfaces + 1; G->face_nodepos = malloc(nel * sizeof *G->face_nodepos); - + nel = 2 * nfaces; G->face_cells = malloc(nel * sizeof *G->face_cells); - + nel = nfaces * ndims; G->face_centroids = malloc(nel * sizeof *G->face_centroids); - + nel = nfaces * ndims; G->face_normals = malloc(nel * sizeof *G->face_normals); - + nel = nfaces * 1; G->face_areas = malloc(nel * sizeof *G->face_areas); - - + + /* Cell fields ---------------------------------------- */ nel = ncellfaces; G->cell_faces = malloc(nel * sizeof *G->cell_faces); - + nel = ncells + 1; G->cell_facepos = malloc(nel * sizeof *G->cell_facepos); - + nel = ncells * ndims; G->cell_centroids = malloc(nel * sizeof *G->cell_centroids); - + nel = ncells * 1; G->cell_volumes = malloc(nel * sizeof *G->cell_volumes); - + if ((G->node_coordinates == NULL) || (G->face_nodes == NULL) || (G->face_nodepos == NULL) || - (G->face_cells == NULL) || + (G->face_cells == NULL) || (G->face_centroids == NULL) || (G->face_normals == NULL) || (G->face_areas == NULL) || (G->cell_faces == NULL) || (G->cell_facepos == NULL) || - (G->cell_centroids == NULL) || + (G->cell_centroids == NULL) || (G->cell_volumes == NULL) ) { destroy_grid(G); G = NULL; } } - + return G; } - - - diff --git a/opm/core/grid.h b/opm/core/grid.h index e09adfed..cefd9d4a 100644 --- a/opm/core/grid.h +++ b/opm/core/grid.h @@ -26,7 +26,7 @@ * Main OPM-Core grid data structure along with destructor and default * constructor. */ -#include + #ifdef __cplusplus extern "C" { #endif @@ -58,6 +58,14 @@ struct UnstructuredGrid void destroy_grid(struct UnstructuredGrid *g); +struct UnstructuredGrid * +allocate_grid(size_t ndims , + size_t ncells , + size_t nfaces , + size_t nfacenodes, + size_t ncellfaces, + size_t nnodes ); + ---- end of synopsis of grid.h ---- */ @@ -238,14 +246,28 @@ void destroy_grid(struct UnstructuredGrid *g); */ struct UnstructuredGrid * create_grid_empty(void); - + +/** + Allocate and initialise an UnstructuredGrid where pointers are set to + location with correct size. + + \param[in] ndims Number of physical dimensions. + \param[in] ncells Number of cells. + \param[in] nfaces Number of faces. + \param[in] nfacenodes Size of mapping from faces to nodes. + \param[in] ncellfaces Size of mapping from cells to faces.(i.e Number of halffaces) + \param[in] nnodes Number of Nodes + \return Fully formed UnstructuredGrid with all fields allocated, but not filled with + usefull numbers. + NULL in case of allocation failure. + */ struct UnstructuredGrid * - allocate_grid(size_t ndims , - size_t ncells, - size_t nfaces, - size_t nfacenodes, - size_t ncellfaces, - size_t nnodes); +allocate_grid(size_t ndims , + size_t ncells , + size_t nfaces , + size_t nfacenodes, + size_t ncellfaces, + size_t nnodes ); #ifdef __cplusplus } diff --git a/opm/core/vag_format/vag.hpp b/opm/core/vag_format/vag.hpp index 2f9d8c74..4cd67a9d 100644 --- a/opm/core/vag_format/vag.hpp +++ b/opm/core/vag_format/vag.hpp @@ -45,7 +45,8 @@ #include namespace OPM { - /* Struct to hold maping from the natural number less than pos.size()-1 to + /** + Struct to hold maping from the natural number less than pos.size()-1 to a set of integers. value(pos(i):pos(i+1)-1) hold the integers corresponding to i. pos(end)-1==value.size(); */ @@ -53,31 +54,56 @@ namespace OPM std::vector pos; std::vector value; }; - /* Structure to represent the unstructured vag grid format + /** + Structure to represent the unstructured vag grid format. The format is only for + 3D grids. */ struct VAG{ - int number_of_vertices; + int number_of_vertices; int number_of_volumes; int number_of_faces; int number_of_edges; + /** Vertices. The coordinates of vertice i is [vetices[3*i:3*i+2]*/ std::vector vertices; + /** Mapping from volumes to faces */ PosStruct volumes_to_faces; + /** Mapping from volumes to vertices */ PosStruct volumes_to_vertices; + /** Mapping from faces to edges */ PosStruct faces_to_edges; + /** Mapping from faces to vertices */ PosStruct faces_to_vertices; + /** The edge i is given by the nodes edges[2*i:2*i+1] */ std::vector edges; + /** The two neigbours of the face i is faces_to_volumes[2*i:2*i+1] */ std::vector faces_to_volumes; + /** A vector containing information of each volume. The size is n*number_of_volumes. + For each i this is the information: + material[n*i] is the volume number and should be transformed to integer + material[n*i+1] is a tag and should be transformed to integer + material[n*i+2:n*(i+1)-1] represent propertices. + */ std::vector material; }; - /* Function the vag grid format and make a vag_grid struct. This structure - is intended to be converted to a grid*/ + /** + Function the vag grid format and make a vag_grid struct. This structure + is intended to be converted to a grid. + \param[in] is is stream of the file. + \param[out] is a reference to a vag_grid struct. + */ void readVagGrid(std::istream& is,OPM::VAG& vag_grid); - /* + /** Function to write vag format. + + */ void writeVagFormat(std::ostream& os){ using namespace std; os << "File in the Vag grid format" << endl; }; - */ + /** + Function to read of some type from a stream. + \param[in] is is stream of the file. + \param[out] is a resized and filled vector containing the quantiy read. + */ template void readVector(std::istream& is,std::vector& vec){ using namespace std; @@ -86,8 +112,18 @@ namespace OPM } } - //PosStruct readPosStruct(std::istream& is,int n){ + /** + Read pos struct type mapping from a stream + \param[in] stream + \param[in] number of lines to read + \param[out] reference to PosStruct + */ void readPosStruct(std::istream& is,int n,PosStruct& pos_struct); + /** + Fill a UnstructuredGrid from a vag_grid. + \param[in] a valid vag_grid struct. + \param[out] a grid with have allocated correct size to each pointer. + */ void vagToUnstructuredGrid(OPM::VAG& vag_grid,UnstructuredGrid& grid); } #endif /* OPM_VAG_HPP_HEADER */ From 7e4350e7aaee88b3e30e4d00e3c6695f9bf47fa8 Mon Sep 17 00:00:00 2001 From: "Halvor M. Nilsen" Date: Mon, 11 Jun 2012 10:42:34 +0200 Subject: [PATCH 5/7] Corrected include and errors in vag.hpp --- opm/core/grid.h | 2 ++ opm/core/vag_format/vag.hpp | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/opm/core/grid.h b/opm/core/grid.h index cefd9d4a..b6dfb3f0 100644 --- a/opm/core/grid.h +++ b/opm/core/grid.h @@ -20,6 +20,8 @@ #ifndef OPM_GRID_HEADER_INCLUDED #define OPM_GRID_HEADER_INCLUDED +#include + /** * \file * diff --git a/opm/core/vag_format/vag.hpp b/opm/core/vag_format/vag.hpp index 4cd67a9d..278cdba0 100644 --- a/opm/core/vag_format/vag.hpp +++ b/opm/core/vag_format/vag.hpp @@ -92,13 +92,12 @@ namespace OPM \param[out] is a reference to a vag_grid struct. */ void readVagGrid(std::istream& is,OPM::VAG& vag_grid); - /** Function to write vag format. - - */ + /* Function to write vag format. void writeVagFormat(std::ostream& os){ using namespace std; os << "File in the Vag grid format" << endl; - }; + } + */ /** Function to read of some type from a stream. \param[in] is is stream of the file. From 4d1a26ff30c94601c22f43fc5ef5a22961627445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Mon, 11 Jun 2012 11:02:56 +0200 Subject: [PATCH 6/7] Re-implement allocate_cart_grid() in term of allocate_grid(). --- opm/core/grid/cart_grid.c | 67 ++++----------------------------------- 1 file changed, 6 insertions(+), 61 deletions(-) diff --git a/opm/core/grid/cart_grid.c b/opm/core/grid/cart_grid.c index b318de74..50e75063 100644 --- a/opm/core/grid/cart_grid.c +++ b/opm/core/grid/cart_grid.c @@ -170,68 +170,13 @@ allocate_cart_grid(size_t ndims , size_t nfaces, size_t nnodes) { - size_t nel; - struct UnstructuredGrid *G; + size_t nfacenodes, ncellfaces; - G = create_grid_empty(); - - if (G != NULL) { - /* Node fields ---------------------------------------- */ - nel = nnodes * ndims; - G->node_coordinates = malloc(nel * sizeof *G->node_coordinates); - - - /* Face fields ---------------------------------------- */ - nel = nfaces * (2 * (ndims - 1)); - G->face_nodes = malloc(nel * sizeof *G->face_nodes); - - nel = nfaces + 1; - G->face_nodepos = malloc(nel * sizeof *G->face_nodepos); - - nel = 2 * nfaces; - G->face_cells = malloc(nel * sizeof *G->face_cells); - - nel = nfaces * ndims; - G->face_centroids = malloc(nel * sizeof *G->face_centroids); - - nel = nfaces * ndims; - G->face_normals = malloc(nel * sizeof *G->face_normals); - - nel = nfaces * 1; - G->face_areas = malloc(nel * sizeof *G->face_areas); - - - /* Cell fields ---------------------------------------- */ - nel = ncells * (2 * ndims); - G->cell_faces = malloc(nel * sizeof *G->cell_faces); - - nel = ncells + 1; - G->cell_facepos = malloc(nel * sizeof *G->cell_facepos); - - nel = ncells * ndims; - G->cell_centroids = malloc(nel * sizeof *G->cell_centroids); - - nel = ncells * 1; - G->cell_volumes = malloc(nel * sizeof *G->cell_volumes); - - if ((G->node_coordinates == NULL) || - (G->face_nodes == NULL) || - (G->face_nodepos == NULL) || - (G->face_cells == NULL) || - (G->face_centroids == NULL) || - (G->face_normals == NULL) || - (G->face_areas == NULL) || - (G->cell_faces == NULL) || - (G->cell_facepos == NULL) || - (G->cell_centroids == NULL) || - (G->cell_volumes == NULL) ) - { - destroy_grid(G); - G = NULL; - } - } - - return G; + nfacenodes = nfaces * (2 * (ndims - 1)); + ncellfaces = ncells * (2 * ndims); + + return allocate_grid(ndims, ncells, nfaces, + nfacenodes, ncellfaces, nnodes); } From 239dffd5fbd403bfa22db8d0210b30f5db99c39a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Mon, 11 Jun 2012 12:55:33 +0200 Subject: [PATCH 7/7] Remove an instance of EOL whitespace introduced in cset 7e6d7352d8ea. --- opm/core/grid/cart_grid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opm/core/grid/cart_grid.c b/opm/core/grid/cart_grid.c index 50e75063..2847e1fd 100644 --- a/opm/core/grid/cart_grid.c +++ b/opm/core/grid/cart_grid.c @@ -174,7 +174,7 @@ allocate_cart_grid(size_t ndims , nfacenodes = nfaces * (2 * (ndims - 1)); ncellfaces = ncells * (2 * ndims); - + return allocate_grid(ndims, ncells, nfaces, nfacenodes, ncellfaces, nnodes); }