[cig-commits] r6826 - in cs/cigma/trunk: include/cigma src tests
luis at geodynamics.org
luis at geodynamics.org
Wed May 9 17:21:34 PDT 2007
Author: luis
Date: 2007-05-09 17:21:34 -0700 (Wed, 09 May 2007)
New Revision: 6826
Added:
cs/cigma/trunk/include/cigma/mesh.h
cs/cigma/trunk/src/mesh.c
cs/cigma/trunk/tests/test_mesh.c
Log:
Added mesh object and related functionality
Added: cs/cigma/trunk/include/cigma/mesh.h
===================================================================
--- cs/cigma/trunk/include/cigma/mesh.h 2007-05-10 00:18:13 UTC (rev 6825)
+++ cs/cigma/trunk/include/cigma/mesh.h 2007-05-10 00:21:34 UTC (rev 6826)
@@ -0,0 +1,25 @@
+#ifndef __CIGMA_MESH_H__
+#define __CIGMA_MESH_H__
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ int nno; // number of nodes
+ int nsd; // number of spatial dimensions
+ int nel; // number of elements
+ int ndof; // number of degrees of freedom per element
+ double *coords; // node coordinates
+ int *connect; // element connectivity
+} mesh_t;
+
+int mesh_init(mesh_t *m, char *filename, char *path_coords, char *path_connect);
+int mesh_free(mesh_t *m);
+
+void mesh_coords(mesh_t *m, int e, double *dof_coords);
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif
Added: cs/cigma/trunk/src/mesh.c
===================================================================
--- cs/cigma/trunk/src/mesh.c 2007-05-10 00:18:13 UTC (rev 6825)
+++ cs/cigma/trunk/src/mesh.c 2007-05-10 00:21:34 UTC (rev 6826)
@@ -0,0 +1,83 @@
+#include <stdlib.h>
+#include <assert.h>
+#include <hdf5.h>
+#include <cigma/dataset.h>
+#include <cigma/mesh.h>
+
+
+/*
+ * Input : mesh object, element number e
+ * Output: dof coordinates -- [ndof x nsd] matrix
+ */
+void mesh_coords(mesh_t *m, int e, double *dof_coords)
+{
+
+ double *coord = m->coords;
+ int *conn = m->connect;
+ int ndof = m->ndof;
+
+ int i,j;
+ for (i = 0; i < ndof; i++)
+ {
+ j = conn[ndof*e + i];
+ dof_coords[3*i + 0] = coord[3*j + 0];
+ dof_coords[3*i + 1] = coord[3*j + 1];
+ dof_coords[3*i + 2] = coord[3*j + 2];
+ }
+}
+
+
+/*
+ * Mesh object initializer
+ */
+int mesh_init(mesh_t *mesh,
+ char *filename,
+ char *path_coords,
+ char *path_connect)
+{
+ hid_t file_id;
+ herr_t status;
+ int ierr;
+
+ /* open the file in read-only mode */
+ file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
+ if (file_id < 0)
+ {
+ return -1;
+ }
+
+ /* read all coordinates */
+ ierr = dataset_read2(file_id, path_coords, H5T_NATIVE_DOUBLE,
+ (void **)&(mesh->coords), &(mesh->nno), &(mesh->nsd));
+ if (ierr < 0)
+ {
+ H5Fclose(file_id);
+ return -2;
+ }
+ assert(mesh->nsd == 3);
+
+ /* read all elements */
+ ierr = dataset_read2(file_id, path_connect, H5T_NATIVE_INT,
+ (void **)&(mesh->connect), &(mesh->nel), &(mesh->ndof));
+ if (ierr < 0)
+ {
+ H5Fclose(file_id);
+ return -3;
+ }
+
+ /* close the file */
+ status = H5Fclose(file_id);
+
+ return 0;
+}
+
+int mesh_free(mesh_t *mesh)
+{
+ if (mesh->connect != NULL)
+ free(mesh->connect);
+
+ if (mesh->coords != NULL)
+ free(mesh->coords);
+
+ return 0;
+}
Added: cs/cigma/trunk/tests/test_mesh.c
===================================================================
--- cs/cigma/trunk/tests/test_mesh.c 2007-05-10 00:18:13 UTC (rev 6825)
+++ cs/cigma/trunk/tests/test_mesh.c 2007-05-10 00:21:34 UTC (rev 6826)
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <cigma/mesh.h>
+
+int main(int argc, char *argv[])
+{
+ mesh_t mesh;
+
+ int e;
+ int i,j;
+ double *coords;
+
+ int ierr;
+
+ if (argc < 2)
+ {
+ printf("Usage: %s mesh.h5\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ ierr = mesh_init(&mesh, argv[1], "/model/geometry/coordinates", "/model/topology/simplices");
+ if (ierr < 0)
+ {
+ printf("Could not read mesh from '%s'!\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ printf("Reading mesh %s\n", argv[1]);
+ printf("Found %d nodes and %d elements\n", mesh.nno, mesh.nel);
+
+ coords = (double *)malloc((mesh.ndof * 3) * sizeof(double));
+
+ for (e = 100; e < 105; e++)
+ {
+ mesh_coords(&mesh, e, coords);
+
+ printf("coords(%d) = {", e);
+ for (i = 0; i < mesh.ndof; i++)
+ {
+ printf("{");
+ for (j = 0; j < 3; j++)
+ {
+ printf("%g", coords[3*i + j]);
+ if (j != 2)
+ printf(",");
+ }
+ printf("}");
+ if (i != (mesh.ndof - 1))
+ printf(",");
+ }
+ printf("};\n");
+ }
+
+ free(coords);
+ mesh_free(&mesh);
+ return EXIT_SUCCESS;
+}
More information about the cig-commits
mailing list