/*=========================================================================== // // File: cart_grid.c // // Author: Jostein R. Natvig // //==========================================================================*/ /* Copyright 2011 SINTEF ICT, Applied Mathematics. Copyright 2011 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 #include #include "cart_grid.h" static struct UnstructuredGrid *allocate_cart_grid_3d(int nx, int ny, int nz); static void fill_cart_topology_3d(struct UnstructuredGrid *G); static void fill_cart_geometry_3d(struct UnstructuredGrid *G, double dx, double dy, double dz); struct UnstructuredGrid* create_cart_grid_3d(int nx, int ny, int nz) { return create_hexa_grid_3d(nx, ny, nz, 1.0, 1.0, 1.0); } struct UnstructuredGrid* create_hexa_grid_3d(int nx, int ny, int nz, double dx, double dy, double dz) { struct UnstructuredGrid *G; G = allocate_cart_grid_3d(nx, ny, nz); if (G == NULL) { fprintf(stderr, "Cannot allocate space for grid.\n"); exit(EXIT_FAILURE); } fill_cart_topology_3d(G); fill_cart_geometry_3d(G, dx, dy, dz); return G; } /* --------------------------------------------------------------------- */ static struct UnstructuredGrid *allocate_cart_grid_2d(int nx, int ny); static void fill_cart_topology_2d(struct UnstructuredGrid *G); static void fill_cart_geometry_2d(struct UnstructuredGrid *G); struct UnstructuredGrid* create_cart_grid_2d(int nx, int ny) { struct UnstructuredGrid *G; G = allocate_cart_grid_2d(nx, ny); if (G == NULL) { fprintf(stderr, "Cannot allocate space for grid.\n"); exit(EXIT_FAILURE); } fill_cart_topology_2d(G); fill_cart_geometry_2d(G); return G; } /* --------------------------------------------------------------------- */ struct UnstructuredGrid * create_tensor_grid_2d(int nx, int ny, double x[], double y[]) { int i,j; double *coord; struct UnstructuredGrid *G; G = allocate_cart_grid_2d(nx, ny); if (G == NULL) { fprintf(stderr, "Cannot allocate space for grid.\n"); exit(EXIT_FAILURE); } fill_cart_topology_2d(G); coord = G->node_coordinates; for (j=0; jnode_coordinates; for (k=0; kdimensions = 3; G->cartdims[0] = nx; G->cartdims[1] = ny; G->cartdims[2] = nz; Nx = nx+1; Ny = ny+1; Nz = nz+1; nxf = Nx*ny*nz; nyf = nx*Ny*nz; nzf = nx*ny*Nz; G->number_of_cells = nx*ny*nz; G->number_of_faces = nxf+nyf+nzf; G->number_of_nodes = Nx*Ny*Nz; G->node_coordinates = malloc(G->number_of_nodes * 3 * sizeof *(G->node_coordinates)); G->face_nodes = malloc(G->number_of_faces * 4 * sizeof *(G->face_nodes)); G->face_nodepos = malloc((G->number_of_faces+1) * sizeof *(G->face_nodepos)); G->face_cells = malloc(G->number_of_faces * 2 * sizeof *(G->face_cells)); G->face_centroids = malloc(G->number_of_faces * 3 * sizeof *(G->face_centroids)); G->face_normals = malloc(G->number_of_faces * 3 * sizeof *(G->face_normals)); G->face_areas = malloc(G->number_of_faces * 1 * sizeof *(G->face_areas)); G->cell_faces = malloc(G->number_of_cells * 6 * sizeof *(G->cell_faces)); G->cell_facepos = malloc((G->number_of_cells+1) * sizeof *(G->cell_facepos)); G->cell_centroids = malloc(G->number_of_cells * 3 * sizeof *(G->cell_centroids)); G->cell_volumes = malloc(G->number_of_cells * 1 * sizeof *(G->cell_volumes)); G->global_cell = NULL; G->cell_facetag = NULL; if ((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 ) ) { free_grid(G); G = NULL; } } return G; } /* --------------------------------------------------------------------- */ static void fill_cart_topology_3d(struct UnstructuredGrid *G) { int nx, ny, nz; int Nx, Ny, Nz; int nxf, nyf, nzf; int i,j,k; int *cfaces, *cfacepos, *fnodes, *fnodepos, *fcells; nx = G->cartdims[0]; ny = G->cartdims[1]; nz = G->cartdims[2]; Nx = nx+1; Ny = ny+1; Nz = nz+1; nxf = Nx*ny*nz; nyf = nx*Ny*nz; nzf = nx*ny*Nz; cfaces = G->cell_faces; cfacepos = G->cell_facepos; cfacepos[0] = 0; for (k=0; kface_nodes; fnodepos = G->face_nodepos; fcells = G->face_cells; fnodepos[0] = 0; /* Faces with x-normal */ for (k=0; kcartdims[0]; ny = G->cartdims[1]; nz = G->cartdims[2]; Nx = nx+1; Ny = ny+1; Nz = nz+1; nxf = Nx*ny*nz; nyf = nx*Ny*nz; nzf = nx*ny*Nz; areax = dy*dz; areay = dx*dz; areaz = dx*dy; cvol = dx*dy*dz; ccentroids = G->cell_centroids; cvolumes = G->cell_volumes; for (k=0; kface_normals; fcentroids = G->face_centroids; fareas = G->face_areas; /* Faces with x-normal */ for (k=0; knode_coordinates; for (k=0; kdimensions = 2; G->cartdims[0] = nx; G->cartdims[1] = ny; G->cartdims[2] = 1; Nx = nx+1; Ny = ny+1; nxf = Nx*ny; nyf = nx*Ny; G->number_of_cells = nx*ny; G->number_of_faces = nxf+nyf; G->number_of_nodes = Nx*Ny; G->node_coordinates = malloc(G->number_of_nodes * 2 * sizeof *(G->node_coordinates)); G->face_nodes = malloc(G->number_of_faces * 2 * sizeof *(G->face_nodes)); G->face_nodepos = malloc((G->number_of_faces+1) * sizeof *(G->face_nodepos)); G->face_cells = malloc(G->number_of_faces * 2 * sizeof *(G->face_cells)); G->face_centroids = malloc(G->number_of_faces * 2 * sizeof *(G->face_centroids)); G->face_normals = malloc(G->number_of_faces * 2 * sizeof *(G->face_normals)); G->face_areas = malloc(G->number_of_faces * 1 * sizeof *(G->face_areas)); G->cell_faces = malloc(G->number_of_cells * 4 * sizeof *(G->cell_faces)); G->cell_facepos = malloc((G->number_of_cells+1) * sizeof *(G->cell_facepos)); G->cell_centroids = malloc(G->number_of_cells * 2 * sizeof *(G->cell_centroids)); G->cell_volumes = malloc(G->number_of_cells * 1 * sizeof *(G->cell_volumes)); G->global_cell = NULL; G->cell_facetag = NULL; if ((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 ) ) { free_grid(G); G = NULL; } } return G; } /* --------------------------------------------------------------------- */ static void fill_cart_topology_2d(struct UnstructuredGrid *G) { int i,j; int nx, ny; int nxf; int Nx; int *fnodes, *fnodepos, *fcells, *cfaces, *cfacepos; cfaces = G->cell_faces; cfacepos = G->cell_facepos; nx = G->cartdims[0]; ny = G->cartdims[1]; Nx = nx + 1; nxf = Nx * ny; cfacepos[0] = 0; for (j=0; jface_nodes; fnodepos = G->face_nodepos; fcells = G->face_cells; fnodepos[0] = 0; /* Faces with x-normal */ for (j=0; jcartdims[0]; ny = G->cartdims[1]; Nx = nx+1; Ny = ny+1; nxf = Nx*ny; nyf = nx*Ny; ccentroids = G->cell_centroids; cvolumes = G->cell_volumes; for (j=0; jface_normals; fcentroids = G->face_centroids; fareas = G->face_areas; /* Faces with x-normal */ for (j=0; jnode_coordinates; for (j=0; jnumber_of_cells; ++i) { fprintf(stderr, "%d: ", i); for (k=g->cell_facepos[i]; kcell_facepos[i+1]; ++k) { fprintf(stderr, "%d ", g->cell_faces[k]); } fprintf(stderr, "\n"); } free_grid(g); return 0; } #endif