tetmesh_read_tags.c 9.39 KB
Newer Older
1 2 3 4 5 6
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "H5hut.h"

7
// name of input file
8 9
const char* FNAME = "simple_tet.h5";

10 11 12
// H5hut verbosity/debug level
const h5_int64_t h5_verbosity = H5_VERBOSE_DEFAULT;
const h5_int64_t h5_debugmsk = H5_DEBUG_ALL;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

typedef struct timer {
	clock_t _start;
	clock_t _end;
	struct timer* (*new)(void);
	void (*delete)(struct timer*);
	void (*start)(struct timer*);
	void (*stop)(struct timer*);
	double (*elapsed)(struct timer*);
} Timer;

extern Timer Timer_;
static Timer*
new (
	void
	) {
gsell's avatar
gsell committed
29
	Timer* self = (Timer*)malloc (sizeof (Timer));
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	*self = Timer_;
	return self;
}

static void
delete (Timer* self) {
	free (self);
}

static void
start (Timer* self) {
	self->_start = clock();
}

static void
stop (Timer* self) {
	self->_end = clock();
}

static double
elapsed (Timer* self) {
	return (double)(self->_end - self->_start)/CLOCKS_PER_SEC;
}

Timer Timer_ = {
	0,
	0,
	new,
	delete,
	start,
	stop,
	elapsed
};

struct vertex {
	h5_float64_t P[3];
};

typedef struct vertex vertex_t; 

struct tet {
	h5_glb_id_t global_id;
	h5_glb_id_t parent_id;
	h5_glb_id_t vids[4];
};
typedef struct tet tet_t;

static h5_err_t
traverse_vertices (
	h5t_mesh_t* const m,
	h5t_tagset_t* tagset,
	int dumpit,
	Timer* timer
	) {
	printf ( "  %-32s", "Traversing vertices: ");

	/* get iterator for co-dim 3 entities, i.e vertices */
	h5t_iterator_t* iter = H5FedBeginTraverseEntities (m, 3);

	/* iterate  */
	h5_loc_id_t local_id;
	h5_size_t num_entities = 0;
	h5_size_t num_entities_tagged = 0;
	timer->start(timer);
	while ((local_id = H5FedTraverseEntities (iter)) >= 0) {
		num_entities++;
		if (dumpit) {
			printf ("\n");
			h5_float64_t P[3];
			H5FedGetVertexCoordsByID (m, local_id, P);
			char v[256];
			snprintf (v, sizeof(v), "=%llx=", (long long)local_id);
			printf ("| %-18s | (%f, %f, %f) |",
				v, P[0], P[1], P[2]);
		}

		h5_size_t size = 3;
		h5_int64_t tval[3];
		if ((local_id = H5FedGetTag (tagset, local_id, &size, tval)) < 0) {
			continue;	// not tagged
		}
		if (dumpit) {
			printf (" (%llx, %llx, %llx) |",
gsell's avatar
gsell committed
113
				(long long)tval[0], (long long)tval[1], (long long)tval[2]);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		}
		num_entities_tagged++;
		if (tval[0] != local_id ||
		    tval[1] != local_id+1 ||
		    tval[2] != local_id+2) {
			fprintf (stderr,
				 "!!! Wrong tag values for entity %lld\n",
				 (long long)local_id);
			exit (1);
		}
	}
	/* done */
	H5FedEndTraverseEntities (iter);
	timer->stop(timer);

	if (dumpit) {
		printf ("\n\nTime elapsed:");
	}
	printf ("  %fsec, total number of entities: %lu; tagged: %lu\n",
		timer->elapsed(timer),
		(unsigned long)num_entities, (unsigned long)num_entities_tagged);
	return H5_SUCCESS;
}

static h5_err_t
traverse_edges (
	h5t_mesh_t* const m,
	h5t_tagset_t* tagset,
	int dumpit,
	Timer* timer
	) {
	printf ( "  %-32s", "Traversing edges: ");
	timer->start(timer);

	/* get iterator for co-dim 2 entities, i.e. edges */
	h5t_iterator_t* iter = H5FedBeginTraverseEntities (m, 2);

	/* iterate */
	h5_loc_id_t local_id;
	h5_size_t num_entities = 0;
	h5_size_t num_entities_tagged = 0;
	while ((local_id = H5FedTraverseEntities (iter)) >= 0) {
		num_entities++;
		if (dumpit) {
			printf ("\n");
			char v[256];
			char k[256];
			h5_loc_id_t local_vids[4];
			snprintf ( k, sizeof(k), "=%llx=", (long long)local_id );
			H5FedGetVertexIndicesOfEntity ( m, local_id, local_vids );
			snprintf ( v, sizeof(v), "=[%lld,%lld]=",
				   (long long)local_vids[0], (long long)local_vids[1] );
			printf ( "| %-18s | %-18s |", k, v );
		}
		h5_size_t size = 3;
		h5_int64_t tval[3];
		if ((local_id = H5FedGetTag (tagset, local_id, &size, tval)) < 0) 
			continue;	// not tagged
		if (dumpit) {
			printf (" (%llx, %llx, %llx) |",
gsell's avatar
gsell committed
174
				(long long)tval[0], (long long)tval[1], (long long)tval[2]);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		}
		num_entities_tagged++;

		if (tval[0] != local_id ||
		    tval[1] != local_id+1 ||
		    tval[2] != local_id+2) {
			fprintf (stderr,
				 "!!! Wrong tag values for entity %lld\n",
				 (long long)local_id);
			exit (1);
		}
	}

	/* done */
	H5FedEndTraverseEntities (iter);
	timer->stop(timer);

	if (dumpit) {
		printf ("\n\nTime elapsed:");
	}
	printf ("  %fsec, total number of entities: %lu; tagged: %lu\n",
		timer->elapsed(timer),
		(unsigned long)num_entities, (unsigned long)num_entities_tagged);
	return H5_SUCCESS;
}

static h5_err_t
traverse_triangles (
	h5t_mesh_t* const m,
	h5t_tagset_t* tagset,
	int dumpit,
	Timer* timer
	) {
	printf ( "  %-32s", "Traversing triangles: ");
	timer->start(timer);
	/* get iterator for co-dim 1 entities, i.e. triangles */
	h5t_iterator_t* iter = H5FedBeginTraverseEntities (m, 1);

	/* iterate */
	h5_loc_id_t local_id;
	h5_size_t num_entities = 0;
	h5_size_t num_entities_tagged = 0;
	while ((local_id = H5FedTraverseEntities (iter)) >= 0) {
		num_entities++;
		if (dumpit) {
			printf ("\n");
			char v[256];
			char k[256];
			h5_loc_id_t local_vids[4];
			snprintf ( k, sizeof(k), "=%llx=", (long long)local_id );
			H5FedGetVertexIndicesOfEntity ( m, local_id, local_vids );
			snprintf ( v, sizeof(v), "=[%lld,%lld,%lld]=",
				   (long long)local_vids[0],
				   (long long)local_vids[1],
				   (long long)local_vids[2] );
			printf ( "| %-18s | %-18s |", k, v );
		}
		h5_size_t size = 3;
		h5_int64_t tval[3];
		if ((local_id = H5FedGetTag (tagset, local_id, &size, tval)) < 0) 
			continue;	// not tagged
		if (dumpit) {
			printf (" (%llx, %llx, %llx) |",
gsell's avatar
gsell committed
238
				(long long)tval[0], (long long)tval[1], (long long)tval[2]);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		}
		num_entities_tagged++;

		if (tval[0] != local_id || tval[1] != local_id+1 || tval[2] != local_id+2) {
			fprintf (stderr,
				 "!!! Wrong tag values for entity %lld\n",
				 (long long)local_id);
			exit (1);
		}
	}

	/* done */
	H5FedEndTraverseEntities (iter);
	timer->stop(timer);

	if (dumpit) {
		printf ("\n\nTime elapsed:");
	}
	printf ("  %fsec, total number of entities: %lu; tagged: %lu\n",
		timer->elapsed(timer),
		(unsigned long)num_entities, (unsigned long)num_entities_tagged);
	return H5_SUCCESS;
}

static h5_err_t
traverse_tets (
	h5t_mesh_t* const m,
	h5t_tagset_t* tagset,
	int dumpit,
	Timer* timer
	) {
	printf ( "  %-32s", "Traversing tetrahedra: ");
	timer->start(timer);
	/* get iterator for co-dim 0 entities, i.e. tetrahedra */
	h5t_iterator_t* iter = H5FedBeginTraverseEntities (m, 0);
	/* iterate */
	h5_loc_id_t local_id;
	h5_size_t num_entities = 0;
	h5_size_t num_entities_tagged = 0;
	while ((local_id = H5FedTraverseEntities (iter)) >= 0) {
		num_entities++;
		if (dumpit) {
			printf ("\n");
			char v[256];
			char k[256];
			h5_loc_id_t local_vids[4];
			snprintf ( k, sizeof(k), "=%llx=", (long long)local_id );
			H5FedGetVertexIndicesOfEntity ( m, local_id, local_vids );
			snprintf ( v, sizeof(v), "=[%lld,%lld,%lld,%lld]=",
				   (long long)local_vids[0],
				   (long long)local_vids[1],
				   (long long)local_vids[2],
				   (long long)local_vids[3] );
			printf ( "| %-18s | %-18s |", k, v );
		}
		h5_size_t size = 3;
		h5_int64_t tval[3];
		if ((local_id = H5FedGetTag (tagset, local_id, &size, tval)) < 0) 
			continue;	// not tagged
		if (dumpit) {
			printf (" (%llx, %llx, %llx) |",
gsell's avatar
gsell committed
300
				(long long)tval[0], (long long)tval[1], (long long)tval[2]);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		}
		num_entities_tagged++;

		if (tval[0] != local_id || tval[1] != local_id+1 || tval[2] != local_id+2) {
			fprintf (stderr,
				 "!!! Wrong tag values for entity %lld\n",
				 (long long)local_id);
			exit (1);
		}
	}

	/* done */
	H5FedEndTraverseEntities (iter);
	timer->stop(timer);

	if (dumpit) {
		printf ("\n\nTime elapsed:");
	}
	printf ("  %fsec, total number of entities: %lu; tagged: %lu\n",
		timer->elapsed(timer),
		(unsigned long)num_entities, (unsigned long)num_entities_tagged);
	return H5_SUCCESS;
}

static h5_err_t
traverse_level (
	h5t_mesh_t* const m,
	h5t_tagset_t* tagset,
gsell's avatar
gsell committed
329
	const h5_lvl_idx_t level_id,
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	int dumpit,
	Timer* timer
	) {
	printf ("Setting level to %d\n", level_id);
	H5FedSetLevel (m, level_id);
	traverse_vertices (m, tagset, dumpit, timer);
	traverse_edges (m, tagset, dumpit, timer);
	traverse_triangles (m, tagset, dumpit, timer);
	traverse_tets (m, tagset, dumpit, timer);
	return H5_SUCCESS;
}

static h5_err_t
traverse_mesh (
	const h5_file_t f,
	const h5_id_t mesh_id,
	int dumpit
	) {
	h5t_mesh_t* mesh;
	Timer* timer = Timer_.new();
	timer->start(timer);
	/* open mesh and get number of levels */
gsell's avatar
gsell committed
352
	printf ("    Opening mesh with id %lld\n", (long long)mesh_id);
353 354 355 356 357 358 359 360 361 362 363
	H5FedOpenTetrahedralMeshByIndex (f, mesh_id, &mesh);
	timer->stop(timer);
	printf ("  %fsec\n", timer->elapsed(timer));

	h5t_tagset_t* tagset;
	H5FedOpenMTagset (mesh, "testtag", &tagset);

	h5_size_t num_levels = H5FedGetNumLevels (mesh);
	printf ("    Number of levels in mesh: %lld\n", (long long)num_levels);

	/* loop over all levels */
gsell's avatar
gsell committed
364
	h5_lvl_idx_t level_id;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	for (level_id = 0; level_id < num_levels; level_id++) {
		traverse_level (mesh, tagset, level_id, dumpit, timer);
	}
	/* done */
	H5FedCloseMTagset (tagset);
	H5FedCloseMesh (mesh);
	timer->delete(timer);
	return H5_SUCCESS;
}

int
main (
	int argc,
	char* argv[]
	) {
380
	
381 382
	/* abort program on error, so we don't have to handle them */
	H5SetErrorHandler (H5AbortErrorhandler);
383 384 385
	H5SetVerbosityLevel (h5_verbosity);
	H5SetDebugMask (h5_debugmsk);
	
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	/* open file and get number of meshes */
	h5_file_t f = H5OpenFile (FNAME, H5_O_RDONLY, 0);
	h5_size_t num_meshes = H5FedGetNumTetrahedralMeshes (f);
	printf ("    Number of meshes: %lld\n", (long long)num_meshes);

	/* loop over all meshes */
	h5_id_t mesh_id;
	int dumpit = 1;
	for (mesh_id = 0; mesh_id < num_meshes; mesh_id++) {
		traverse_mesh (f, mesh_id, dumpit);
	}

	/* done */
	H5CloseFile (f);
	return 0;
}