[cig-commits] r6626 - in cs/cigma/trunk/sandbox/c: . tests

luis at geodynamics.org luis at geodynamics.org
Fri Apr 20 14:24:18 PDT 2007


Author: luis
Date: 2007-04-20 14:24:17 -0700 (Fri, 20 Apr 2007)
New Revision: 6626

Added:
   cs/cigma/trunk/sandbox/c/Makefile
   cs/cigma/trunk/sandbox/c/mesh.c
   cs/cigma/trunk/sandbox/c/mesh.h
   cs/cigma/trunk/sandbox/c/tests/
   cs/cigma/trunk/sandbox/c/tests/Makefile
   cs/cigma/trunk/sandbox/c/tests/test_mesh.c
Log:
Functions for HDF5 I/O


Added: cs/cigma/trunk/sandbox/c/Makefile
===================================================================
--- cs/cigma/trunk/sandbox/c/Makefile	2007-04-20 20:49:20 UTC (rev 6625)
+++ cs/cigma/trunk/sandbox/c/Makefile	2007-04-20 21:24:17 UTC (rev 6626)
@@ -0,0 +1,20 @@
+HDF5_HOME = /usr
+
+CC = gcc
+CFLAGS = -Wall -O3
+INCLUDES = -I$(HDF5_HOME)/include
+LIBRARIES = -L$(HDF5_HOME)/lib
+LIBS = -lhdf5
+
+OBJFILES = mesh.o
+
+libcigma.a: $(OBJFILES)
+	ar r $@ $^
+
+.c.o:
+	$(CC) $(CFLAGS) $(INCLUDES) $(LIBRARIES) -c $<
+
+clean:
+	rm -f $(OBJFILES)
+
+.PHONY: clean

Added: cs/cigma/trunk/sandbox/c/mesh.c
===================================================================
--- cs/cigma/trunk/sandbox/c/mesh.c	2007-04-20 20:49:20 UTC (rev 6625)
+++ cs/cigma/trunk/sandbox/c/mesh.c	2007-04-20 21:24:17 UTC (rev 6626)
@@ -0,0 +1,142 @@
+#include <stdlib.h>
+#include <assert.h>
+#include "mesh.h"
+#include "hdf5.h"
+
+static int mesh_coords_init(coordinates_t *nodes, hid_t dset_id);
+static int mesh_connect_init(connectivity_t *conn, hid_t dset_id);
+
+static int mesh_coords_fini(coordinates_t *nodes);
+static int mesh_connect_fini(connectivity_t *conn);
+
+
+
+/*
+ * Initialize mesh object
+ */
+int mesh_init(mesh_t *mesh,
+              char *filename,
+              char *path_coords,
+              char *path_connect)
+{
+    hid_t file_id;
+    hid_t coords_dset;
+    hid_t connect_dset;
+    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 */
+    coords_dset = H5Dopen(file_id, path_coords);
+    mesh->coords = (coordinates_t *)malloc(sizeof(coordinates_t));
+    ierr = mesh_coords_init(mesh->coords, coords_dset);
+    status = H5Dclose(coords_dset);
+    if (ierr < 0)
+    {
+        return -2;
+    }
+
+    /* read all elements */
+    connect_dset = H5Dopen(file_id, path_connect);
+    mesh->connect = (connectivity_t *)malloc(sizeof(connectivity_t));
+    ierr = mesh_connect_init(mesh->connect, connect_dset);
+    status = H5Dclose(connect_dset);
+    if (ierr < 0)
+    {
+        return -3;
+    }
+
+    /* close the file */
+    status = H5Fclose(file_id);
+
+    return 0;
+}
+
+int mesh_fini(mesh_t *mesh)
+{
+    mesh_connect_fini(mesh->connect);
+    free(mesh->connect);
+
+    mesh_coords_fini(mesh->coords);
+    free(mesh->coords);
+
+    return 0;
+}
+
+
+/*
+ * Internal functions
+ */
+
+
+static int mesh_coords_init(coordinates_t *nodes, hid_t dset_id)
+{
+    int rank;
+    hsize_t dims[2];
+    hid_t dspace_id;
+    herr_t status;
+
+    dspace_id = H5Dget_space(dset_id);
+    rank = H5Sget_simple_extent_ndims(dspace_id); assert(rank == 2);
+    status = H5Sget_simple_extent_dims(dspace_id, dims, NULL);
+    status = H5Sclose(dspace_id);
+
+    nodes->nno = dims[0];
+    nodes->n   = dims[1]; assert(nodes->n == 3);
+    nodes->coords = (double *)malloc((nodes->nno)*(nodes->n)*sizeof(double));
+
+    status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
+                     H5P_DEFAULT, nodes->coords);
+    if (status < 0)
+    {
+        return -1;
+    }
+
+    return 0;
+}
+
+static int mesh_coords_fini(coordinates_t *nodes)
+{
+    free(nodes->coords);
+    return 0;
+}
+
+
+static int mesh_connect_init(connectivity_t *conn, hid_t dset_id)
+{
+    int rank;
+    hsize_t dims[2];
+    hid_t dspace_id;
+    herr_t status;
+    
+    dspace_id = H5Dget_space(dset_id);
+    rank = H5Sget_simple_extent_ndims(dspace_id); assert(rank == 2);
+    status = H5Sget_simple_extent_dims(dspace_id, dims, NULL);
+    status = H5Sclose(dspace_id);
+
+    conn->nel = dims[0];
+    conn->n   = dims[1]; assert((conn->n == 4) || (conn->n == 8));
+    conn->elements = (int *)malloc((conn->nel)*(conn->n)*sizeof(int));
+
+    status = H5Dread(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
+                     H5P_DEFAULT, conn->elements);
+    
+    if (status < 0)
+    {
+        return -1;
+    }
+
+    return 0;
+}
+
+static int mesh_connect_fini(connectivity_t *conn)
+{
+    free(conn->elements);
+    return 0;
+}

Added: cs/cigma/trunk/sandbox/c/mesh.h
===================================================================
--- cs/cigma/trunk/sandbox/c/mesh.h	2007-04-20 20:49:20 UTC (rev 6625)
+++ cs/cigma/trunk/sandbox/c/mesh.h	2007-04-20 21:24:17 UTC (rev 6626)
@@ -0,0 +1,31 @@
+#ifndef __MESH_H__
+#define __MESH_H__
+
+typedef struct
+{
+    int n;          // number of components per node (always 3)
+    int nno;        // number of nodes
+    double *coords;
+} coordinates_t;
+
+typedef struct {
+    int n;          // number of components per element (4 or 8)
+    int nel;        // number of elements
+    int *elements;
+} connectivity_t;
+
+typedef struct
+{
+    coordinates_t  *coords;
+    connectivity_t *connect;
+} mesh_t;
+
+
+/*
+ * Function prototypes
+ */
+
+int mesh_init(mesh_t *m, char *filename, char *path_coords, char *path_connect);
+int mesh_fini(mesh_t *m);
+
+#endif

Added: cs/cigma/trunk/sandbox/c/tests/Makefile
===================================================================
--- cs/cigma/trunk/sandbox/c/tests/Makefile	2007-04-20 20:49:20 UTC (rev 6625)
+++ cs/cigma/trunk/sandbox/c/tests/Makefile	2007-04-20 21:24:17 UTC (rev 6626)
@@ -0,0 +1,17 @@
+CC = gcc
+CFLAGS = -Wall -g 
+INCLUDES = -I..
+LIBRARIES = -L..
+LIBS = -lcigma -lhdf5
+
+TARGETS = test_mesh
+
+all: $(TARGETS)
+
+test_mesh: test_mesh.c
+	$(CC) $(CFLAGS) $(INCLUDES) $(LIBRARIES) $< -o $@ $(LIBS)
+
+clean:
+	rm -f $(TARGETS)
+
+.PHONY: clean

Added: cs/cigma/trunk/sandbox/c/tests/test_mesh.c
===================================================================
--- cs/cigma/trunk/sandbox/c/tests/test_mesh.c	2007-04-20 20:49:20 UTC (rev 6625)
+++ cs/cigma/trunk/sandbox/c/tests/test_mesh.c	2007-04-20 21:24:17 UTC (rev 6626)
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "mesh.h"
+
+int main(int argc, char *argv[])
+{
+    mesh_t mesh;
+    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 %s\n", argv[1]);
+    printf("Found %d nodes and %d elements\n", mesh.coords->nno, mesh.connect->nel);
+
+    mesh_fini(&mesh);
+    return EXIT_SUCCESS;
+}



More information about the cig-commits mailing list