[cig-commits] r6895 - cs/cigma/trunk/tools
luis at geodynamics.org
luis at geodynamics.org
Wed May 16 10:18:54 PDT 2007
Author: luis
Date: 2007-05-16 10:18:54 -0700 (Wed, 16 May 2007)
New Revision: 6895
Added:
cs/cigma/trunk/tools/cube-cmd.c
Log:
Added cube command
Added: cs/cigma/trunk/tools/cube-cmd.c
===================================================================
--- cs/cigma/trunk/tools/cube-cmd.c 2007-05-16 17:18:17 UTC (rev 6894)
+++ cs/cigma/trunk/tools/cube-cmd.c 2007-05-16 17:18:54 UTC (rev 6895)
@@ -0,0 +1,193 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <hdf5.h>
+#include <argtable2.h>
+
+#include <cigma/cube.h>
+#include <cigma/dataset.h>
+
+
+int cube_command(const char *filename,
+ const char *coords_path,
+ const char *hexconn_path,
+ const char *tetconn_path,
+ int L, int M, int N)
+{
+ cube_t *cube;
+
+ hid_t file_id;
+ herr_t status;
+ int ierr;
+
+ int exitcode = EXIT_SUCCESS;
+
+ /* Quick sanity check to avoid HDF5 error */
+ if (filename == NULL)
+ {
+ fprintf(stderr, "Error: filename not specified\n");
+ return EXIT_FAILURE;
+ }
+
+
+ /* Create and initialize the cube object */
+ cube = (cube_t *)malloc(sizeof(cube_t));
+ cube_init(cube);
+
+
+ /*
+ * Open a new HDF5 file
+ */
+ file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
+ if (file_id < 0)
+ {
+ fprintf(stderr, "Error: Could not create file '%s'\n", filename);
+ cube_free(cube);
+ free(cube);
+ return EXIT_FAILURE;
+ }
+
+
+ /*
+ * Partition the unit cube
+ */
+
+ cube_partition(cube, L, M, N);
+
+ #ifdef DEBUG
+ printf("file = \"%s\"\n", filename);
+ printf("L, M, N = (%d, %d, %d)\n", L, M, N);
+ printf("cube->nno = %d\n", cube->nno);
+ printf("cube->hexnel = %d\n", cube->hexnel);
+ printf("cube->tetnel = %d\n", cube->tetnel);
+ #endif
+
+
+ /*
+ * Create the /coords dataset
+ */
+ if (coords_path != NULL)
+ {
+ ierr = dataset_write2(file_id, coords_path, H5T_NATIVE_DOUBLE,
+ (void *)cube->coords, cube->nno, 3);
+ if (ierr < 0)
+ {
+ fprintf(stderr, "Error: Could not create dataset %s in '%s'\n", coords_path, filename);
+ exitcode = -1;
+ goto exit;
+ }
+ }
+
+ /*
+ * Save the hexahedral mesh connectivity
+ */
+ if (hexconn_path != NULL)
+ {
+ ierr = dataset_write2(file_id, hexconn_path, H5T_NATIVE_INT,
+ (void *)cube->hexconn, cube->hexnel, 8);
+ if (ierr < 0)
+ {
+ fprintf(stderr, "Error: Could not create dataset %s in '%s'\n", hexconn_path, filename);
+ exitcode = -2;
+ goto exit;
+ }
+ }
+
+ /*
+ * Save the tetrahedral mesh connectivity
+ */
+ if (tetconn_path != NULL)
+ {
+ ierr = dataset_write2(file_id, tetconn_path, H5T_NATIVE_INT,
+ (void *)cube->tetconn, cube->tetnel, 4);
+ if (ierr < 0)
+ {
+ fprintf(stderr, "Could not create dataset %s in '%s'\n", tetconn_path, filename);
+ exitcode = -3;
+ goto exit;
+ }
+ }
+
+exit:
+ status = H5Fclose(file_id);
+ cube_free(cube);
+ free(cube);
+ return exitcode;
+}
+
+
+
+
+int cube_main(int argc, char *argv[])
+{
+ const char *progname = "cube";
+
+ struct arg_lit *help = arg_lit0 ("h", "help", "display this help and exit");
+ struct arg_str *coords = arg_str0 (NULL, "coords", "<path>", "coords dataset path in output");
+ struct arg_str *hexconn = arg_str0 (NULL, "hexconn", "<path>", "hexconn dataset path in output");
+ struct arg_str *tetconn = arg_str0 (NULL, "tetconn", "<path>", "tetconn dataset path in output");
+ struct arg_int *L = arg_int0 ("L", NULL, "<int>", "elements along x-dimension");
+ struct arg_int *M = arg_int0 ("M", NULL, "<int>", "elements along y-dimension");
+ struct arg_int *N = arg_int0 ("N", NULL, "<int>", "elements along z-dimension");
+ struct arg_file *outfile = arg_file1(NULL, NULL, "<output.h5>", "output file");
+ struct arg_end *end = arg_end(20);
+
+ void *argtable[] = {outfile, L, M, N, coords, hexconn, tetconn, help, end};
+
+ int nerrors;
+ int exitcode = 0;
+
+ /* verify the argtable[] entries were allocated successfully */
+ if (arg_nullcheck(argtable) != 0)
+ {
+ fprintf(stderr, "%s: failed to allocate argtable\n", progname);
+ exitcode = 1;
+ goto exit;
+ }
+
+ /* set any command line default values prior to parsing */
+
+ coords->sval[0] = "/coords";
+ hexconn->sval[0] = "/hexconn";
+ tetconn->sval[0] = "/tetconn";
+
+ L->ival[0] = 9;
+ M->ival[0] = 9;
+ N->ival[0] = 9;
+
+ nerrors = arg_parse(argc, argv, argtable);
+
+ /* special case: '--help' takes precedence over error reporting */
+ if (help->count > 0)
+ {
+ printf("Usage: %s [options] <output.h5>\n\n", progname);
+ //printf("Usage: %s", progname);
+ //arg_print_syntax(stdout, argtable, "\n");
+ arg_print_glossary(stdout, argtable, " %-25s %s\n");
+ exitcode = 0;
+ goto exit;
+ }
+
+ /* If the parser returned any errors then display them and exit */
+ if (nerrors > 0)
+ {
+ /* Display the error details contained in the arg_end struct. */
+ arg_print_errors(stdout, end, progname);
+ exitcode = 1;
+ goto exit;
+ }
+
+ /* normal case: take the command line options */
+ exitcode = cube_command(outfile->filename[0],
+ coords->sval[0],
+ hexconn->sval[0],
+ tetconn->sval[0],
+ L->ival[0],
+ M->ival[0],
+ N->ival[0]);
+
+exit:
+ arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0]));
+
+ return exitcode;
+}
More information about the cig-commits
mailing list