commit e931a643301a4874ec132a8fe0b5bec2a46a5d72
parent 1864cae30429b2195f2bf2bf0811be98331aa371
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 24 Aug 2022 18:29:53 +0200
VTK write function can now write multiple octrees
Diffstat:
3 files changed, 99 insertions(+), 54 deletions(-)
diff --git a/src/rnatm.h b/src/rnatm.h
@@ -160,9 +160,9 @@ rnatm_get_octrees_count
(const struct rnatm* atm);
RNATM_API res_T
-rnatm_write_vtk_octree
+rnatm_write_vtk_octrees
(const struct rnatm* atm,
- const size_t iaccel_struct,
+ const size_t octrees[2], /* Range of consider. Limits are inclusive */
FILE* stream);
#endif /* RNATM_H */
diff --git a/src/rnatm_write_vtk_octree.c b/src/rnatm_write_vtk_octree.c
@@ -87,6 +87,16 @@ octree_data_release(struct octree_data* data)
}
static void
+octree_data_clear(struct octree_data* data)
+{
+ ASSERT(data);
+ htable_vertex_clear(&data->vertex2id);
+ darray_double_clear(&data->vertices);
+ darray_double_clear(&data->data);
+ darray_size_t_clear(&data->cells);
+}
+
+static void
register_leaf
(const struct svx_voxel* leaf,
const size_t ileaf,
@@ -133,56 +143,44 @@ register_leaf
CHK(RES_OK == darray_double_push_back(&ctx->data, &kext_max));
}
-
-/*******************************************************************************
- * Exported function
- ******************************************************************************/
-res_T
-rnatm_write_vtk_octree
- (const struct rnatm* atm,
+static void
+write_vtk_octree
+ (const struct rnatm* atm,
const size_t iaccel_struct,
+ struct octree_data* data,
FILE* stream)
{
- char buf[128];
- struct time t0, t1;
- struct octree_data data;
- struct svx_tree* octree = NULL;
+ const struct accel_struct* accel_struct = NULL;
const double* leaf_data = NULL;
size_t nvertices;
size_t ncells;
size_t i;
- res_T res = RES_OK;
- if(!atm
- || !stream
- || iaccel_struct >= darray_accel_struct_size_get(&atm->accel_structs)) {
- res = RES_BAD_ARG;
- goto error;
- }
-
- /* Start time recording */
- log_info(atm, "write VTK octree\n");
- time_current(&t0);
+ ASSERT(atm && stream);
+ ASSERT(iaccel_struct < darray_accel_struct_size_get(&atm->accel_structs));
- octree = darray_accel_struct_cdata_get(&atm->accel_structs)[iaccel_struct].octree;
+ accel_struct = darray_accel_struct_cdata_get(&atm->accel_structs)+iaccel_struct;
- octree_data_init(atm, &data);
+ octree_data_clear(data);
/* Register leaf data */
- SVX(tree_for_each_leaf(octree, register_leaf, &data));
- nvertices = darray_double_size_get(&data.vertices)/3/*#coords per vertex*/;
- ncells = darray_size_t_size_get(&data.cells)/8/*#ids per cell*/;
+ SVX(tree_for_each_leaf(accel_struct->octree, register_leaf, data));
+ nvertices = darray_double_size_get(&data->vertices)/3/*#coords per vertex*/;
+ ncells = darray_size_t_size_get(&data->cells)/8/*#ids per cell*/;
#ifndef NDEBUG
{
struct svx_tree_desc octree_desc = SVX_TREE_DESC_NULL;
- SVX(tree_get_desc(octree, &octree_desc));
+ SVX(tree_get_desc(accel_struct->octree, &octree_desc));
ASSERT(ncells == octree_desc.nleaves);
}
#endif
/* Write headers */
fprintf(stream, "# vtk DataFile Version 2.0\n");
- fprintf(stream, "Radiative coefficients\n");
+ fprintf(stream,
+ "Radiative coefficients for band %lu and quadrature point %lu\n",
+ (unsigned long)accel_struct->iband,
+ (unsigned long)accel_struct->iquad_pt);
fprintf(stream, "ASCII\n");
fprintf(stream, "DATASET UNSTRUCTURED_GRID\n");
@@ -190,7 +188,7 @@ rnatm_write_vtk_octree
fprintf(stream, "POINTS %lu float\n", (unsigned long)nvertices);
FOR_EACH(i, 0, nvertices) {
fprintf(stream, "%g %g %g\n",
- SPLIT3(darray_double_cdata_get(&data.vertices) + i*3));
+ SPLIT3(darray_double_cdata_get(&data->vertices) + i*3));
}
/* Write the cells */
@@ -199,14 +197,14 @@ rnatm_write_vtk_octree
(unsigned long)(ncells*(8/*#verts per cell*/ + 1/*1st field of a cell*/)));
FOR_EACH(i, 0, ncells) {
fprintf(stream, "8 %lu %lu %lu %lu %lu %lu %lu %lu\n",
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+0],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+1],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+2],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+3],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+4],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+5],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+6],
- (unsigned long)darray_size_t_cdata_get(&data.cells)[i*8+7]);
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+0],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+1],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+2],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+3],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+4],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+5],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+6],
+ (unsigned long)darray_size_t_cdata_get(&data->cells)[i*8+7]);
}
/* Write the cell type */
@@ -214,19 +212,53 @@ rnatm_write_vtk_octree
FOR_EACH(i, 0, ncells) fprintf(stream, "11\n");
/* Write the cell data */
- leaf_data = darray_double_cdata_get(&data.data);
+ leaf_data = darray_double_cdata_get(&data->data);
fprintf(stream, "CELL_DATA %lu\n", (unsigned long)ncells);
fprintf(stream, "SCALARS Kext double 2\n");
fprintf(stream, "LOOKUP_TABLE default\n");
FOR_EACH(i, 0, ncells) {
fprintf(stream, "%g %g\n", leaf_data[i*2+0], leaf_data[i*2+1]);
}
+}
+
+/*******************************************************************************
+ * Exported function
+ ******************************************************************************/
+res_T
+rnatm_write_vtk_octrees
+ (const struct rnatm* atm,
+ const size_t structs[2], /* Range of acceleration structures to write */
+ FILE* stream)
+{
+ char buf[128];
+ struct time t0, t1;
+ struct octree_data data;
+ size_t i;
+ res_T res = RES_OK;
+
+ if(!atm
+ || !stream
+ || structs[0] >= darray_accel_struct_size_get(&atm->accel_structs)
+ || structs[1] >= darray_accel_struct_size_get(&atm->accel_structs)
+ || structs[0] > structs[1]) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ /* Start time recording */
+ log_info(atm, "write VTK octrees\n");
+ time_current(&t0);
+
+ octree_data_init(atm, &data);
+ FOR_EACH(i, structs[0], structs[1]+1) {
+ write_vtk_octree(atm, structs[0]+i, &data, stream);
+ }
octree_data_release(&data);
/* Log elapsed time */
time_sub(&t0, time_current(&t1), &t0);
time_dump(&t0, TIME_ALL, NULL, buf, sizeof(buf));
- log_info(atm, "octree written in %s\n", buf);
+ log_info(atm, "octrees written in %s\n", buf);
exit:
return res;
diff --git a/src/test_rnatm.c b/src/test_rnatm.c
@@ -50,8 +50,14 @@ print_help(const char* cmd)
"Test the Rad-Net ATMosphere library\n\n",
cmd);
printf(
-" -d file write the builded octree to file according to the\n"
-" VTK file format\n");
+" -d file write the builded octrees to file according to the VTK\n"
+" file format. The octrees are written to standard ouput\n"
+" if the file is a dash (-). To split the resulting file\n"
+" into n VTK files, each storing an octree, one can use\n"
+" the csplit command. For example with n = 4:\n\n");
+ printf(
+" csplit -f octree -k file %%^#\\ vtk%% /^#\\ vtk/ {2}\n\n");
+
printf(
" -g gas Atmospheric gas mixture. This mixture is defined by\n"
" the following list of parameters, each parameter\n"
@@ -271,25 +277,32 @@ error:
}
static res_T
-write_vtk_octree(struct rnatm* atm, const char* filename)
+write_vtk_octrees(struct rnatm* atm, const char* filename)
{
+ size_t octrees_range[2];
FILE* fp = NULL;
res_T res = RES_OK;
ASSERT(atm && filename);
- fp = fopen(filename, "w");
- if(!fp) {
- fprintf(stderr, "Could not open `%s' -- %s\n", filename, strerror(errno));
- res = RES_IO_ERR;
- goto error;
+ if(!strcmp(filename, "-")) {
+ fp = stdout;
+ } else {
+ fp = fopen(filename, "w");
+ if(!fp) {
+ fprintf(stderr, "Could not open `%s' -- %s\n", filename, strerror(errno));
+ res = RES_IO_ERR;
+ goto error;
+ }
}
- res = rnatm_write_vtk_octree
- (atm, 0/*FIXME handle a user defined octree id*/, fp);
+ octrees_range[0] = 0;
+ octrees_range[1] = rnatm_get_octrees_count(atm) - 1;
+
+ res = rnatm_write_vtk_octrees(atm, octrees_range, fp);
if(res != RES_OK) goto error;
exit:
- if(fp) CHK(fclose(fp) == 0);
+ if(fp != stdout) CHK(fclose(fp) == 0);
return res;
error:
goto exit;
@@ -313,7 +326,7 @@ main(int argc, char** argv)
if(res != RES_OK) goto error;
if(args.vtk_filename) {
- res = write_vtk_octree(atm, args.vtk_filename);
+ res = write_vtk_octrees(atm, args.vtk_filename);
if(res != RES_OK) goto error;
}