h5t_octree.c 88.9 KB
Newer Older
gsell's avatar
gsell committed
1 2
#include "private/h5t_octree.h"
#include "private/h5t_core.h"
3

gsell's avatar
gsell committed
4
#ifdef WITH_PARALLEL_H5GRID
5 6 7 8 9

#include <stdlib.h>
#include <string.h>
#include <assert.h>

gsell's avatar
gsell committed
10
#include "private/h5_mpi.h"
11

gsell's avatar
gsell committed
12 13 14
h5_oct_dta_types_t h5_oct_dta_types;


15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 113 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
static h5_err_t
get_bounding_box_of_octant (
		h5t_octree_t* const octree,
		h5_oct_idx_t oct_idx,
		h5_float64_t* bounding_box
		);

#if 0

/*** code copied from http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
 * author http://stackoverflow.com/users/429544/techplexengineer
 */
static int printf_arginfo_M(const struct printf_info *info, size_t n, int *argtypes, int *avoidwarning) {
	/* "%M" always takes one argument, a pointer to uint8_t[6]. */
	if (n > 0) {
		argtypes[0] = PA_POINTER;
	}
	return 1;
}     /* printf_arginfo_M */

static int printf_output_M(FILE *stream, const struct printf_info *info, const void *const *args) {
	int value = 0;
	int len;
	value = *(int *) (args[0]);
//	value = *(int **) (args[0]);
	//Begin My Code ------------------------------------------------------------
	char buffer [2] = ""; //Is this bad?
	char buffer2 [40] = ""; //Is this bad?
	int bits = 32; //info->width;
//        if (bits <= 0)
//            bits = 8; //Default to 8 bits
	int counter = 1;
	uint mask = 1 << (bits-1);
	while (mask > 0) {
		sprintf(buffer, "%s", (((value & mask) > 0) ? "1" : "0"));
		strcat(buffer2, buffer);
		if (counter %8 == 0) {
			sprintf(buffer, " ");
			strcat(buffer2, buffer);
		}
		mask >>= 1;
		counter++;
	}
	strcat(buffer2, "\n");
	//End my code --------------------------------------------------------------
	len = fprintf(stream, "%s", buffer2);
	return len;
}     /* printf_output_M */
static int
printf_output_B(FILE* stream,
		const struct printf_info* info,
		const void* const* args
		) {
	int value = 0;
	int length = 0;
	value = *(int *) (args[0]);
	char buffer [2] = "";
	char buffer2 [40] = "";
	int bits = 32;
	int counter = 1;
	uint mask = 1 << (bits-1);
	while (mask > 0) {
		sprintf(buffer, "%s", (((value & mask) > 0) ? "1" : "0"));
		strcat(buffer2, buffer);
		if (counter %8 == 0) {
			sprintf(buffer, " ");
			strcat(buffer2, buffer);
		}
		mask >>= 1;
		counter++;
	}
	strcat(buffer2, "\n");
	length = fprintf(stream, "%s", buffer2);
	return length;
}

static int
printf_arginfo_B (const struct printf_info* info,
		size_t n,
		int* argtypes,
		int* args
		) {
	if (n > 0) {
		argtypes[0] = PA_POINTER;
	}
	return 1;
}
#endif

static inline h5_err_t
create_mpi_type_octant (
        void
        ) {
	h5t_octant_t octant;
	H5_PRIV_FUNC_ENTER (h5_err_t, "%s", "void");
	int i = 0;
	const int count = 6;
	int blocklens[count];
	MPI_Aint indices[count];
	MPI_Datatype old_types[count];
	MPI_Aint base;
	MPI_Aint addr;
	TRY (mpi_get_address (&octant, &base));

	// idx
	blocklens[i] = 1;
	TRY (mpi_get_address (&octant.idx, &addr));
	indices[i] = addr - base;
	old_types[i++] = MPI_INT;

	// parent_idx
	blocklens[i] = 1;
	TRY (mpi_get_address (&octant.parent_idx, &addr));
	indices[i] = addr - base;
	old_types[i++] = MPI_INT;

	// child_idx
	blocklens[i] = 1;
	TRY (mpi_get_address (&octant.child_idx, &addr));
	indices[i] = addr - base;
	old_types[i++] = MPI_INT;

	// level_idx
	blocklens[i] = 1;
	TRY (mpi_get_address (&octant.level_idx, &addr));
	indices[i] = addr - base;
	old_types[i++] = MPI_SHORT;

//	// bounding_box
//	blocklens[i] = 6;
//	TRY (mpi_get_address (&octant.bounding_box, &addr));
//	indices[i] = addr - base;
//	old_types[i++] = MPI_DOUBLE;

	// processor
	blocklens[i] = 1;
	TRY (mpi_get_address (&octant.processor, &addr));
	indices[i] = addr - base;
	old_types[i++] = MPI_INT;

	// userlevels
	blocklens[i] = 1;
	TRY (mpi_get_address (&octant.userlevels, &addr));
	indices[i] = addr - base;
	old_types[i++] = MPI_INT;


	// create new type
	assert (i == count);
	TRY (mpi_create_type_struct (count, blocklens, indices, old_types,
	                             &h5_oct_dta_types.mpi_octant));
	// commit new type
	TRY (h5priv_mpi_type_commit (&h5_oct_dta_types.mpi_octant));

gsell's avatar
gsell committed
169
	H5_RETURN (H5_SUCCESS);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}
h5_err_t H5t_create_mpi_type_octant (void) {
	return create_mpi_type_octant ();
}
/*
 * Get parent index of octant
 */
static h5_oct_idx_t
get_parent (
        h5t_octree_t* const octree,
        const h5_oct_idx_t oct_idx
        ) {
	if (oct_idx <= 0) {
		return -1;
	}
	return (octree->octants[oct_idx].parent_idx);
};
h5_oct_idx_t
H5t_get_parent (h5t_octree_t* const octree, const h5_oct_idx_t oct_idx) {
	return get_parent (octree, oct_idx);
};

static int
get_maxpoints (
		h5t_octree_t* const octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p,", octree);
gsell's avatar
gsell committed
197
	H5_RETURN (octree->maxpoints);
198 199 200 201 202 203 204 205 206 207 208 209 210
}
int  H5t_get_maxpoints (h5t_octree_t* const octree) {
	return get_maxpoints (octree);
}

static h5_err_t
set_maxpoints (
		h5t_octree_t* const octree,
        int maxpoints
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, maxpoints=%d", octree, maxpoints);
	assert (maxpoints > 0);
	octree->maxpoints = maxpoints;
gsell's avatar
gsell committed
211
	H5_RETURN (H5_SUCCESS);
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 238 239 240 241 242 243 244
}

h5_err_t H5t_set_maxpoints (h5t_octree_t* const octree, int maxpoints) {
	return set_maxpoints (octree, maxpoints);
}

/*
 * Check if octant is on level
 * return value is 0 if it is NOT on level
 */
h5_oct_level_t
oct_has_level (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_level_t level
        ) {
	return octree->octants[oct_idx].userlevels &  1 << level;
}
h5_oct_level_t H5t_oct_has_level (h5t_octree_t* octree, h5_oct_idx_t oct_idx, h5_oct_level_t level) {
	return oct_has_level (octree, oct_idx, level);
}
/*
 * set internal data changed
 */
static h5_err_t
set_intdata_chg (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d", octree, oct_idx);

	octree->octants[oct_idx].level_idx |= ( 1 << OCT_CHG_INTERNAL);

gsell's avatar
gsell committed
245
	H5_RETURN (H5_SUCCESS);
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
}
/*
 * get new proc
 */
static h5_int32_t
get_proc (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	return octree->octants[oct_idx].processor;

}
h5_int32_t
H5t_get_proc (h5t_octree_t* octree, h5_oct_idx_t oct_idx) {
	return get_proc (octree, oct_idx);
}
/*
 * set new proc
 */
static h5_err_t
set_proc (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_int32_t proc
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d proc=%d", octree, oct_idx, proc);

	if (octree->octants[oct_idx].processor != proc) {   // to avoid unnecessary internal updates
		octree->octants[oct_idx].processor = proc;
		TRY (set_intdata_chg (octree, oct_idx));
	}
gsell's avatar
gsell committed
277
	H5_RETURN (H5_SUCCESS);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}
h5_err_t
H5t_set_proc (h5t_octree_t* octree, h5_oct_idx_t oct_idx, h5_int32_t proc) {
	return set_proc (octree, oct_idx, proc);
}
/*
 * set new proc without triggering update
 * WARNING if not all proc do the same the state gets inconsistent!
 */
static h5_err_t
set_proc_int (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_int32_t proc
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d proc=%d", octree, oct_idx, proc);

	octree->octants[oct_idx].processor = proc;

gsell's avatar
gsell committed
297
	H5_RETURN (H5_SUCCESS);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
}
h5_err_t
H5t_set_proc_int (h5t_octree_t* octree, h5_oct_idx_t oct_idx, h5_int32_t proc) {
	return set_proc_int (octree, oct_idx, proc);
}

/*
 * set userlevel
 */
static h5_err_t
set_userlevel (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_level_t level
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d level=%d", octree, oct_idx, level);

	octree->octants[oct_idx].userlevels |=  ( 1 << level);

	TRY (set_intdata_chg (octree, oct_idx));

gsell's avatar
gsell committed
319
	H5_RETURN (H5_SUCCESS);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
}
h5_err_t H5t_set_userlevel (h5t_octree_t* octree, h5_oct_idx_t oct_idx, h5_oct_level_t level) {
	return set_userlevel (octree, oct_idx, level);
}
/*
 * get userlevel
 */
static h5_oct_level_t
get_userlevel (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d", octree, oct_idx);
	h5_oct_level_t level;
	level = octree->octants[oct_idx].userlevels ;
	level = ((level | 1 << (OCT_USERLEV_LENGTH - 1)) ^ (1 << (OCT_USERLEV_LENGTH - 1))); // remove leave level bit

gsell's avatar
gsell committed
337
	H5_RETURN (level);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
}
h5_oct_level_t H5t_get_userlevel (h5t_octree_t* octree, h5_oct_idx_t oct_idx) {
	return get_userlevel (octree, oct_idx);
}

/*
 * set userlevel
 */
static h5_err_t
set_userlevel_int (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_level_t level
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d level=%d", octree, oct_idx, level);

	octree->octants[oct_idx].userlevels |=  ( 1 << level);

gsell's avatar
gsell committed
356
	H5_RETURN (H5_SUCCESS);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
}
// is an internal function - maybe if there is a usecase one could make it API but is dangerous
// to use since it could create a missmatch in level definitions an different procs
//h5_err_t H5t_set_userlevel_int (h5t_octree_t* octree, h5_oct_idx_t oct_idx, h5_oct_level_t level) {
//	return set_userlevel_int (octree, oct_idx, level);
//}

#if 0
/*
 * remove userlevel
 */
static h5_err_t
remove_userlevel (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_level_t level
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d level=%d", octree, oct_idx, level);
	if (oct_has_level (octree,oct_idx,level)){
	octree->octants[oct_idx].userlevels ^=  ( 1 << level);
	TRY (set_intdata_chg (octree, oct_idx));
	}
gsell's avatar
gsell committed
379
	H5_RETURN (H5_SUCCESS);
380 381 382 383 384 385 386 387 388 389 390 391 392 393
}
#endif

static h5_err_t
remove_userlevel_int (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_level_t level
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d level=%d", octree, oct_idx, level);
	if (oct_has_level (octree,oct_idx,level)){
	octree->octants[oct_idx].userlevels ^=  ( 1 << level);

	}
gsell's avatar
gsell committed
394
	H5_RETURN (H5_SUCCESS);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
}
/*
 * set leave level
 */
static h5_err_t
set_leave_level (
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);

	for (int i = 0; i <= octree->current_oct_idx; i++) {
		// check if leave level bit needs to be changed
		if (((octree->octants[i].child_idx == -1) &&
			((octree->octants[i].userlevels & 1 << (OCT_USERLEV_LENGTH - 1)) != 1 << (OCT_USERLEV_LENGTH - 1) )) ||
		    ((octree->octants[i].child_idx != -1) &&
		    ((octree->octants[i].userlevels & 1 << (OCT_USERLEV_LENGTH - 1)) == 1 << (OCT_USERLEV_LENGTH - 1) ))) {
			octree->octants[i].userlevels ^= 1 << (OCT_USERLEV_LENGTH - 1);
			//TRY (set_intdata_chg (octree, i));
		}
	}

gsell's avatar
gsell committed
416
	H5_RETURN (H5_SUCCESS);
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}

#if 0
/*
 * Clear all levels
 */
static h5_err_t
clear_all_levels (
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);

	for (int i = 0; i <= octree->current_oct_idx; i++) {
		octree->octants[i].userlevels = 0;
		//TRY (set_intdata_chg (octree, i));
	}
	set_leave_level (octree);

gsell's avatar
gsell committed
435
	H5_RETURN (H5_SUCCESS);
436 437 438 439 440 441 442 443 444 445 446 447 448
}
#endif

static h5_err_t
clear_level_internal (
		h5t_octree_t* octree
		) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
	for (int i = 0; i <= octree->current_oct_idx; i++) {
		if ((octree->octants[i].level_idx & (1 << OCT_CHG_INTERNAL)) == (1 << OCT_CHG_INTERNAL) ) {
			octree->octants[i].level_idx ^= (1 << OCT_CHG_INTERNAL);
		}
	}
gsell's avatar
gsell committed
449
	H5_RETURN (H5_SUCCESS);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
}


/*
 * get orientation of octant
 */
static h5_oct_orient_t
get_orient (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	if (oct_idx != 0) {
		// WARNING Right shift may not work as expected (c standart is not clear)
		// implementation now without right shift
		h5_oct_orient_t orient = 0;
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_X)) != 0) {
			orient += 1;
		}
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_Y)) != 0) {
			orient += 2;
		}
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_Z)) != 0) {
			orient += 4;
		}
		return orient ;
	} else {
		return -1;
	}

}
/*
 * get orientation of octant
 */
static h5_lvl_idx_t
get_oct_level (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	assert (oct_idx > -1);
	if (oct_idx != 0) {
		// WARNING Right shift may not work as expected (c standart is not clear)
		// implementation now without right shift
		h5_oct_orient_t orient = 0;
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_CHG_INTERNAL)) != 0) {
					orient |= 1 << OCT_CHG_INTERNAL;
				}
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_CHG_USERDATA)) != 0) {
					orient |= 1 << OCT_CHG_USERDATA;
				}
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_X)) != 0) {
			orient |= 1 << OCT_X;
		}
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_Y)) != 0) {
			orient |= 1 << OCT_Y;
		}
		if ((octree->octants[oct_idx].level_idx & (1 << OCT_Z)) != 0) {
			orient |= 1 << OCT_Z;
		}
		return octree->octants[oct_idx].level_idx ^ orient ;
	} else {
		return 0;
	}

}


#if 0
/*
 * Get total number of octants
 */
static h5_oct_idx_t
get_nbr_octants (
        h5t_octree_t* const octree
        ) {
	return (octree->current_oct_idx) +1;
};

/*
 * Make global update because user data changed
 */
#endif

static h5_err_t
update_userdata (
        h5t_octree_t* const octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
	h5_oct_idx_t* oct_idx;
	TRY (oct_idx = h5_calloc (octree->current_oct_idx + 1, sizeof (*oct_idx)));
	h5_oct_idx_t nbr_loc_oct_changed = 0;
	for (int i = 0; i <= octree->current_oct_idx; i++) {
		if ((octree->octants[i].level_idx & (1 << OCT_CHG_USERDATA)) == (1 << OCT_CHG_USERDATA)) {
			oct_idx[nbr_loc_oct_changed] = i;
			nbr_loc_oct_changed++;
		}
	}

	// exchange the number of changed octants
	int size;
	TRY (h5priv_mpi_comm_size (octree->comm, &size));
	h5_int32_t* nbr_oct_changed;
	TRY (nbr_oct_changed = h5_calloc (size, sizeof (*nbr_oct_changed)));

	TRY (h5priv_mpi_allgather (&nbr_loc_oct_changed,
	                          1,
	                          MPI_INT,
	                          nbr_oct_changed,
	                          1,
	                          MPI_INT,
	                          octree->comm));


	// exchange the changed octant_id's
	h5_oct_idx_t* changed_oct_idx;
	h5_oct_idx_t* recv_displs;
	h5_oct_idx_t* recv_counts = nbr_oct_changed;    // just for simpler naming
	h5_oct_idx_t nbr_glb_oct_changed = nbr_oct_changed[0];

	TRY (recv_displs = h5_calloc(size,sizeof(*recv_displs)));

	recv_displs[0] = 0;

	for (int i = 1; i < size; i++) {
		recv_displs[i] = recv_displs[i-1] + nbr_oct_changed[i-1];
		nbr_glb_oct_changed += nbr_oct_changed[i];
	};
	TRY (changed_oct_idx = h5_calloc (nbr_glb_oct_changed, sizeof (*changed_oct_idx)));
	TRY (mpi_allgatherv (oct_idx,
	                     nbr_loc_oct_changed,
	                     MPI_INT,
	                     changed_oct_idx,
	                     recv_counts,
	                     recv_displs,
	                     MPI_INT,
	                     octree->comm
	                     ));

	// check if an octant has been changed on multiple procs
	h5_oct_idx_t oct_idx_to_check;
	for (int i = 0; i < nbr_glb_oct_changed - 1; i++) {
		oct_idx_to_check = changed_oct_idx[i];
		for (int j = i + 1; j < nbr_glb_oct_changed; j++) {
			if (oct_idx_to_check == changed_oct_idx[j]) {
				/*** an octant was changed twice! ***/
594 595 596 597
				H5_RETURN_ERROR (
					H5_ERR_INVAL,
					"Multiple cores tried to update the same userdata with idx: %d",
					oct_idx_to_check);
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
			};
		};
	};
	/*** exchange changed userdata ***/
	// new mpi datatype for userdata
	MPI_Datatype userdata_type;

	TRY (h5priv_mpi_type_contiguous (octree->size_userdata, MPI_BYTE, &userdata_type));
	TRY (h5priv_mpi_type_commit (&userdata_type));

	void* sendbuf;
	void* recvbuf;
	TRY (sendbuf = h5_calloc (nbr_loc_oct_changed, octree->size_userdata));
	TRY (recvbuf = h5_calloc (nbr_glb_oct_changed, octree->size_userdata));

//	recv_counts[0] *=  octree->size_userdata;
//	for (int i = 1; i < size; i++) {
//		recv_displs[i] *=  octree->size_userdata;
//		recv_counts[i] *=  octree->size_userdata;
//	};
	char* charp_to = (char*) sendbuf;
	char* charp_from = (char*) octree->userdata;
	// memcpy to send buffer
	for (int i = 0; i < nbr_loc_oct_changed; i++) {
		memcpy( &(charp_to[i*octree->size_userdata]),
		        &(charp_from[oct_idx[i]*octree->size_userdata]),
		        octree->size_userdata);
	};

	TRY (mpi_allgatherv (
	            sendbuf,
	            nbr_loc_oct_changed,
	            userdata_type,
	            recvbuf,
	            recv_counts,
	            recv_displs,
	            userdata_type,
	            octree->comm
	            ));
	// copy user data to local memory location
	charp_to = octree->userdata;
	charp_from = recvbuf;
	//TODO could be improved by not copying the already local updated octants
	for (int i = 0; i < nbr_glb_oct_changed; i++) {
		memcpy( &(charp_to[changed_oct_idx[i] * octree->size_userdata]),
		        &(charp_from[i * octree->size_userdata]),
		        octree->size_userdata);
	}
	// set changed user data to 0
	for (int i = 0; i <= octree->current_oct_idx; i++) {
		if (i == octree->nbr_alloc_oct) { h5_debug ("THIS shoul not happen");}
		octree->octants[i].level_idx &= ~(1 << OCT_CHG_USERDATA);
	}
	// free memory

	TRY (h5_free (oct_idx));
	TRY (h5_free (nbr_oct_changed));
	TRY (h5_free (sendbuf));
	TRY (h5_free (recvbuf));
	TRY (h5_free (recv_displs));
	TRY (h5_free (changed_oct_idx));
	//MPI_Type_free(&userdata_type);

gsell's avatar
gsell committed
661
	H5_RETURN (H5_SUCCESS);
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
};
h5_err_t
H5t_update_userdata (h5t_octree_t* const octree) {
	return update_userdata (octree);
}

/*
 * Make global update because internal data changed
 */

static h5_err_t
update_internal (
        h5t_octree_t* const octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
	int nprocs =-1;
	h5priv_mpi_comm_size (octree->comm, &nprocs);
	if (nprocs == 1 ) {
		// set changed internal data to 0
		clear_level_internal (octree);
		set_leave_level (octree);
	}

	h5_oct_idx_t* oct_idx;
	TRY (oct_idx = h5_calloc (octree->current_oct_idx + 1, sizeof (*oct_idx)));
	h5_oct_idx_t nbr_loc_oct_changed = 0;
	for (int i = 0; i <= octree->current_oct_idx; i++) {
		if ((octree->octants[i].level_idx & (1 << OCT_CHG_INTERNAL)) == (1 << OCT_CHG_INTERNAL)) {
			oct_idx[nbr_loc_oct_changed] = i;
			nbr_loc_oct_changed++;
		}
	}

	// exchange the number of changed octants
	int size;
	TRY (h5priv_mpi_comm_size (octree->comm, &size));
	h5_int32_t* nbr_oct_changed;
	TRY (nbr_oct_changed = h5_calloc (size, sizeof (*nbr_oct_changed)));

	TRY (h5priv_mpi_allgather (&nbr_loc_oct_changed,
	                          1,
	                          MPI_INT,
	                          nbr_oct_changed,
	                          1,
	                          MPI_INT,
	                          octree->comm));

	// exchange the changed octant_id's
	h5_oct_idx_t* changed_oct_idx;
	h5_oct_idx_t* recv_displs;
	h5_oct_idx_t* recv_counts = nbr_oct_changed;    // just for simpler naming
	h5_oct_idx_t nbr_glb_oct_changed = nbr_oct_changed[0];

	TRY (recv_displs = h5_calloc(size,sizeof(*recv_displs)));

	recv_displs[0] = 0;

	for (int i = 1; i < size; i++) {
		recv_displs[i] = recv_displs[i-1] + nbr_oct_changed[i-1];
		nbr_glb_oct_changed += nbr_oct_changed[i];
	};
	if (nbr_glb_oct_changed == 0) {
		// set changed internal data to 0
		clear_level_internal (octree);
		// free memory

		TRY (h5_free (oct_idx));
		TRY (h5_free (nbr_oct_changed));
		TRY (h5_free (recv_displs));
gsell's avatar
gsell committed
731
		H5_LEAVE (H5_SUCCESS);
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	}
	TRY (changed_oct_idx = h5_calloc (nbr_glb_oct_changed, sizeof (*changed_oct_idx)));
	TRY (mpi_allgatherv (oct_idx,
	                     nbr_loc_oct_changed,
	                     MPI_INT,
	                     changed_oct_idx,
	                     recv_counts,
	                     recv_displs,
	                     MPI_INT,
	                     octree->comm
	                     ));

	// check if an octant has been changed on multiple procs
	h5_oct_idx_t oct_idx_to_check;
	for (int i = 0; i < nbr_glb_oct_changed - 1; i++) {
		oct_idx_to_check = changed_oct_idx[i];
		for (int j = i + 1; j < nbr_glb_oct_changed; j++) {
			if (oct_idx_to_check == changed_oct_idx[j]) {
				/*** an octant was changed twice! ***/
gsell's avatar
gsell committed
751
				H5_LEAVE(H5_ERR_INVAL)
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
			};
		};
	};
	/*** exchange changed octants ***/
	// new mpi datatype for userdata


	void* sendbuf;
	void* recvbuf;
	TRY (sendbuf = h5_calloc (nbr_loc_oct_changed, sizeof (*(octree->octants))));
	TRY (recvbuf = h5_calloc (nbr_glb_oct_changed, sizeof (*(octree->octants))));

	h5t_octant_t* charp_to = (h5t_octant_t*) sendbuf;
	h5t_octant_t* charp_from =  octree->octants;
	// memcpy to send buffer
	for (int i = 0; i < nbr_loc_oct_changed; i++) {
		memcpy( &(charp_to[i]),
		        &(charp_from[oct_idx[i]]),
		        sizeof (*octree->octants));
	};

	TRY (mpi_allgatherv (
	            sendbuf,
	            nbr_loc_oct_changed,
	            h5_oct_dta_types.mpi_octant,
	            recvbuf,
	            recv_counts,
	            recv_displs,
	            h5_oct_dta_types.mpi_octant,
	            octree->comm
	            ));
	// copy user data to local memory location
	charp_to = octree->octants;
	charp_from = recvbuf;
	//TODO could be improved by not copying the already local updated octants
	for (int i = 0; i < nbr_glb_oct_changed; i++) {
		memcpy( &(charp_to[changed_oct_idx[i]]),
		        &(charp_from[i]),
		        sizeof (*octree->octants));
	}
	// set changed internal data to 0
	clear_level_internal (octree);
	set_leave_level (octree);
	// free memory

	TRY (h5_free (oct_idx));
	TRY (h5_free (nbr_oct_changed));
	TRY (h5_free (sendbuf));
	TRY (h5_free (recvbuf));
	TRY (h5_free (recv_displs));
	TRY (h5_free (changed_oct_idx));


gsell's avatar
gsell committed
805
	H5_RETURN (H5_SUCCESS);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
};
h5_err_t
H5t_update_internal (h5t_octree_t* const octree) {
	return update_internal (octree);
}

/*
 * Get first children index of octant
 */
static h5_oct_idx_t
get_children (
        h5t_octree_t* const octree,
        const h5_oct_idx_t oct_idx
        ) {
	return octree->octants[oct_idx].child_idx;
};
h5_oct_idx_t
H5t_get_children (h5t_octree_t* const octree, const h5_oct_idx_t oct_idx) {
	return get_children (octree, oct_idx);
};
/*
 * Get pointer to userdata of octant for reading
 * This Pointer is only valid till the next call of the Library!
 */
static h5_err_t
get_userdata_r (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        void** userdata
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, userdata=%p", octree, oct_idx, userdata);

	char* userdata_int = (char*) octree->userdata;
	*userdata = &userdata_int[oct_idx * octree->size_userdata];

gsell's avatar
gsell committed
841
	H5_RETURN (H5_SUCCESS);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
}
h5_err_t
H5t_get_userdata_r (h5t_octree_t* octree, h5_oct_idx_t oct_idx, void** userdata) {
	return get_userdata_r (octree, oct_idx, userdata);
}

/*
 * Get pointer to userdata of octant for reading/writing
 * This Pointer is only valid till the next call of the Library!
 * To make the write visible to all procs call update_userdata(h5t_octree_t* octree);
 */
static h5_err_t
get_userdata_rw (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        void** userdata
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, userdata=%p", octree, oct_idx, userdata);

	octree->octants[oct_idx].level_idx |= (1 << OCT_CHG_USERDATA);
	char* userdata_int;
	userdata_int = octree->userdata;
	*userdata = &userdata_int[oct_idx * octree->size_userdata];

gsell's avatar
gsell committed
866
	H5_RETURN (H5_SUCCESS);
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
}
h5_err_t
H5t_get_userdata_rw (h5t_octree_t* octree, h5_oct_idx_t oct_idx, void** userdata) {
	return get_userdata_rw (octree, oct_idx, userdata);
}


/*
 * grow data structure of octree
 */
static h5_err_t
grow_octree (
        h5t_octree_t* octree,
        h5_int32_t additional_size
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, additional_size=%d", octree, additional_size);

	if (additional_size == -1) {
		additional_size = octree->nbr_alloc_oct;
	}
	if (additional_size < 8 ) {
		additional_size = 8;
	}
	// check if data structures already exist
	if (octree->octants == NULL) {
		// allocate data structures
		TRY (octree->octants = h5_calloc (additional_size, sizeof (*(octree->octants))));
		if (octree->size_userdata > 0) {
			TRY (octree->userdata = h5_calloc (additional_size, octree->size_userdata));
			memset (octree->userdata, -1, additional_size * octree->size_userdata);
		}
		octree->nbr_alloc_oct = additional_size;
	} else {
		// grow data structure
		TRY (octree->octants = h5_alloc (octree->octants, (octree->nbr_alloc_oct + additional_size) * sizeof (*(octree->octants))));
		if (octree->size_userdata > 0) {
			TRY (octree->userdata = h5_alloc (octree->userdata, (octree->nbr_alloc_oct+additional_size) * octree->size_userdata));
			memset (((char*)octree->userdata) + octree->nbr_alloc_oct * octree->size_userdata , -1, additional_size * octree->size_userdata);
		}
		octree->nbr_alloc_oct += additional_size;
	}
gsell's avatar
gsell committed
908
	H5_RETURN (H5_SUCCESS);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

}

#if 0
/*
 * Reset user data
 */
static h5_err_t
reset_userdata (
        h5t_octree_t* octree,
        h5_int32_t size_userdata
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, size_userdata=%d", octree, size_userdata);

	// make sure all procs call the function
	TRY (h5priv_mpi_barrier (octree->comm));

	// delete userdata
	TRY (h5_free(octree->userdata));

	// allocate new userdata
	if (size_userdata > 0) {
		octree->size_userdata = size_userdata;
		TRY (octree->userdata = h5_calloc (octree->nbr_alloc_oct, octree->size_userdata));
	} else {
		octree->size_userdata = -1;
	}
	// set changed user data to 0
	for (int i = 0; i <= octree->current_oct_idx; i++) {
		octree->octants[i].level_idx &= ~(1 << OCT_CHG_USERDATA);
	}

gsell's avatar
gsell committed
941
	H5_RETURN (H5_SUCCESS);
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
}
#endif
/*
 * Create octant
 */
static h5_err_t
create_octant (
        h5t_octree_t* octree,
        h5_oct_idx_t parent_idx
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t,
			"octree=%p, parent_idx=%d",
			octree,
			parent_idx);

	octree->current_oct_idx +=1;

	// check if there is enough space to insert one octant more.
	if (octree->current_oct_idx >= octree->nbr_alloc_oct) {
		TRY (grow_octree (octree,-1));
	}
	// set parameters
	h5t_octant_t* current_octant = &(octree->octants[octree->current_oct_idx]);
	current_octant->idx = octree->current_oct_idx;
	current_octant->parent_idx = parent_idx;
	current_octant->child_idx = -1;
	if (parent_idx >= 0) {

		 current_octant->level_idx = ((octree->current_oct_idx - octree->octants[parent_idx].child_idx) << OCT_X)
				 + get_oct_level (octree, parent_idx) + 1;


	}

	if (parent_idx != -1) {
		current_octant->processor = octree->octants[parent_idx].processor;
	} else {
		current_octant->processor = 0;
	}
	current_octant->userlevels = 0;
gsell's avatar
gsell committed
982
	H5_RETURN (H5_SUCCESS);
983 984 985 986 987 988 989 990 991 992 993
};

/*
 * Create root octant
 */
static h5_err_t
create_root_octant (
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
	if (octree->octants == NULL || octree->nbr_alloc_oct < 1) {
gsell's avatar
gsell committed
994
		H5_LEAVE (H5_ERR_INTERNAL);
995 996 997 998
	}
	TRY (create_octant (octree,-1));
	octree->octants[0].level_idx = 0;
	TRY (set_leave_level(octree));
gsell's avatar
gsell committed
999
	H5_RETURN (H5_SUCCESS);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
};
/*
 * Initialize an octree
 */
static h5_err_t
init_octree (
        h5t_octree_t** octree,
        h5_int32_t size_userdata,
        h5_float64_t* const bounding_box,
        h5_int32_t maxpoints,
        const MPI_Comm comm
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t,
	                    "octree=%p, size_userdata=%d, "
gsell's avatar
gsell committed
1014 1015
			    "bounding_box=%p, "
			    "maxpoints=%d, comm=?",
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
	                    octree,
	                    size_userdata,
	                    bounding_box,
	                    maxpoints);

	TRY (h5priv_mpi_barrier (comm));

	/*** assert of input data ***/
	//TODO maybe there is a more thorough check needed that input data is valid (i.e. the same on all proc)
	if (bounding_box != NULL && (bounding_box[0] >= bounding_box[3] ||
		bounding_box[1] >= bounding_box[4] ||
		bounding_box[2] >= bounding_box[5])) {
gsell's avatar
gsell committed
1028
		H5_LEAVE (H5_ERR_INVAL);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	};

	/*** allocate octree ***/
	TRY (*octree = h5_calloc (1, sizeof (**octree)));

	(*octree)->comm = comm;
	(*octree)->size_userdata = size_userdata;
	TRY (create_mpi_type_octant ());
	(*octree)->octants = NULL;
	(*octree)->userdata = NULL;
	(*octree)->maxpoints = maxpoints;

	/*** allocate datastructures***/
	TRY (grow_octree (*octree,9));

	(*octree)->nbr_alloc_oct = 9;
	(*octree)->ref_oct_idx = -1;
	(*octree)->current_oct_idx = -1;
	TRY (create_root_octant (*octree));

	if (bounding_box != NULL) {
		TRY (H5t_set_bounding_box (*octree, bounding_box));
	}


gsell's avatar
gsell committed
1054
	H5_RETURN (H5_SUCCESS);
1055 1056 1057 1058 1059 1060 1061 1062
};

h5_err_t H5t_init_octree (h5t_octree_t** octree, h5_int32_t size_userdata, h5_float64_t* const bounding_box, h5_int32_t maxpoints, const MPI_Comm comm) {
	return init_octree (octree, size_userdata, bounding_box, maxpoints, comm);
}

static h5_err_t
read_octree (
gsell's avatar
gsell committed
1063 1064 1065 1066 1067 1068 1069 1070
	h5t_octree_t** octree,
	h5_oct_idx_t current_oct_idx,
	h5_int32_t size_userdata,
	h5_int32_t maxpoints,
	h5t_octant_t** octants,
	void** userdata,
	const MPI_Comm comm
	) {
1071
	H5_PRIV_FUNC_ENTER (h5_err_t,
gsell's avatar
gsell committed
1072 1073 1074 1075 1076 1077 1078 1079
			    "octree=%p, current_oct_idx=%d, size_userdata=%d, "
			    "maxpoints=%d, octants=%p, userdata=%p, comm=?",
			    octree,
			    current_oct_idx,
			    size_userdata,
			    maxpoints,
			    octants,
			    userdata);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

	TRY (h5priv_mpi_barrier (comm)); // delete for speed up?

	/*** allocate octree ***/
	TRY (*octree = h5_calloc (1, sizeof (**octree)));
	(*octree)->comm = comm;
	(*octree)->size_userdata = size_userdata;
	TRY (create_mpi_type_octant ());
	(*octree)->octants = NULL;
	(*octree)->userdata = NULL;
	(*octree)->maxpoints = maxpoints;

	/*** allocate datastructures***/
	TRY (grow_octree (*octree,current_oct_idx + 1));

	(*octree)->nbr_alloc_oct = current_oct_idx + 1;
	(*octree)->ref_oct_idx = -1;
	(*octree)->current_oct_idx = current_oct_idx;
	(*octants) = (*octree)->octants;
	(*userdata) = (*octree)->userdata;
gsell's avatar
gsell committed
1100
	H5_RETURN (H5_SUCCESS);
1101 1102 1103 1104 1105 1106 1107 1108 1109
}
h5_err_t H5t_read_octree (h5t_octree_t** octree, h5_oct_idx_t current_oct_idx, h5_int32_t size_userdata, h5_int32_t maxpoints, h5t_octant_t** octants, void** userdata, const MPI_Comm comm) {
	return read_octree (octree, current_oct_idx, size_userdata, maxpoints, octants, userdata, comm);
}
/*
 * free octree
 */
static h5_err_t
free_oct (
gsell's avatar
gsell committed
1110
	h5t_octree_t* octree) {
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
	if (octree != NULL) {
		if (octree->userdata != NULL) {
			TRY (h5_free (octree->userdata));
		}
		MPI_Type_free(&h5_oct_dta_types.mpi_octant);
		TRY (h5_free (octree->octants));
		TRY (h5_free (octree));
		octree = NULL;
	}
gsell's avatar
gsell committed
1121
	H5_RETURN (H5_SUCCESS);
1122 1123 1124 1125 1126 1127
}
h5_err_t H5t_free_octree (h5t_octree_t* octree) {
	return free_oct (octree);
}
static h5_err_t
write_octree (
gsell's avatar
gsell committed
1128 1129
	h5t_mesh_t* const m
	) {
1130 1131 1132
	H5_PRIV_FUNC_ENTER (h5_err_t, "m=%p", m);


gsell's avatar
gsell committed
1133
	H5_RETURN (h5_error_not_implemented ());
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
}
h5_err_t H5t_write_octree ( h5t_mesh_t* const m) {
	return write_octree (m);
}

#if 0
/*
 * Print octree in human readable form
 */
static h5_err_t
print_octree_dbg (
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t,"octree=%p", octree);
	int rank;
	TRY (h5priv_mpi_comm_rank (octree->comm, &rank));
	h5_debug ("\nproc %d: Octree\n"
			"proc %d:    current_oct_idx:    %d \n"
			"proc %d:    nbr_alloc_oct:      %d \n\n",
			rank,
			rank,
			octree->current_oct_idx,
			rank,
			octree->nbr_alloc_oct);
gsell's avatar
gsell committed
1158
	H5_RETURN (H5_SUCCESS);
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
}
/*
 * Print octree in human readable form
 */
static h5_err_t
print_octree (
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t,"octree=%p", octree);
	int rank;
	TRY (h5priv_mpi_comm_rank (octree->comm, &rank));
	printf ("\nproc %d: Octree\n"
			"proc %d:    current_oct_idx:    %d \n"
			"proc %d:    nbr_alloc_oct:      %d \n\n",
			rank,
			rank,
			octree->current_oct_idx,
			rank,
			octree->nbr_alloc_oct);
gsell's avatar
gsell committed
1178
	H5_RETURN (H5_SUCCESS);
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
}
/*
 * Traverse through octants and print them in human readable form
 */
static h5_err_t
print_octants (
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
#if 0
	register_printf_specifier ('B', printf_output_M, printf_arginfo_M);
	int rank;
	TRY (h5priv_mpi_comm_rank (octree->comm, &rank));
	for (int i = 0; i <= octree->current_oct_idx; i++) {
		printf ("\n"
//		h5_debug ("\n"
				"    Octant\n    idx:          %d \n"
				"    parent_idx:   %d \n"
				"    child_idx:    %d \n"
//				"    bounding_box: %4.4f, %4.4f, %4.4f \n"
//				"                  %4.4f, %4.4f, %4.4f \n"
				"    processor:    %d \n"
				"    level_idx:    %B \n"
				"    userlevels:    %B \n\n",
				octree->octants[i].idx,
				octree->octants[i].parent_idx,
				octree->octants[i].child_idx,
//				octree->octants[i].bounding_box[0],
//				octree->octants[i].bounding_box[1],
//				octree->octants[i].bounding_box[2],
//				octree->octants[i].bounding_box[3],
//				octree->octants[i].bounding_box[4],
//				octree->octants[i].bounding_box[5],
				octree->octants[i].processor,
				octree->octants[i].level_idx,
				octree->octants[i].userlevels);

	}
#endif
gsell's avatar
gsell committed
1218
	H5_RETURN (H5_SUCCESS);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
}
#endif

#if 0
static h5_err_t
print_octants_arr (
        h5t_octant_t* octants,
        h5_int32_t nbr_oct,
        h5_int32_t rank
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octants);
#if 0
	register_printf_specifier ('B', printf_output_M, printf_arginfo_M);

	for (int i = 0; i < nbr_oct; i++) {
		h5_debug ("\n"
				"    Octant\n    idx:          %d \n"
				"    parent_idx:   %d \n"
				"    child_idx:    %d \n"
//				"    bounding_box: %4.4f, %4.4f, %4.4f \n"
//				"                  %4.4f, %4.4f, %4.4f \n"
				"    processor:    %d \n"
				"    level_idx:    %B \n"
				"    userlevels:    %B \n\n",
				octants[i].idx,
				octants[i].parent_idx,
				octants[i].child_idx,
//				octants[i].bounding_box[0],
//				octants[i].bounding_box[1],
//				octants[i].bounding_box[2],
//				octants[i].bounding_box[3],
//				octants[i].bounding_box[4],
//				octants[i].bounding_box[5],
				octants[i].processor,
				octants[i].level_idx,
				octants[i].userlevels);

	}
#endif
gsell's avatar
gsell committed
1258
	H5_RETURN (H5_SUCCESS);
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
}
#endif
///*
// * Traverse through octants and plot the for gnuplot
// */

int print_octant_for_gnuplot (
		h5_oct_idx_t oct_idx,
		h5_float64_t* bounding_box
	) {
#if 0
	printf (		"#    Octant    idx:          %d \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n\n",
					oct_idx,
					bounding_box[0], bounding_box[1], bounding_box[2],
					bounding_box[3], bounding_box[1], bounding_box[2],
					bounding_box[3], bounding_box[4], bounding_box[2],
					bounding_box[0], bounding_box[4], bounding_box[2],
					bounding_box[0], bounding_box[1], bounding_box[2]);
			printf (
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n\n",
					bounding_box[0], bounding_box[1], bounding_box[5],
					bounding_box[3], bounding_box[1], bounding_box[5],
					bounding_box[3], bounding_box[4], bounding_box[5],
					bounding_box[0], bounding_box[4], bounding_box[5],
					bounding_box[0], bounding_box[1], bounding_box[5]);
			printf (
					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n\n"

					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n\n"

					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n\n"

					" %4.4f, %4.4f, %4.4f \n"
					" %4.4f, %4.4f, %4.4f \n\n",
					bounding_box[0], bounding_box[1], bounding_box[2],
					bounding_box[0], bounding_box[1], bounding_box[5],

					bounding_box[3], bounding_box[1], bounding_box[2],
					bounding_box[3], bounding_box[1], bounding_box[5],

					bounding_box[3], bounding_box[4], bounding_box[2],
					bounding_box[3], bounding_box[4], bounding_box[5],

					bounding_box[0], bounding_box[4], bounding_box[2],
					bounding_box[0], bounding_box[4], bounding_box[5]);
#endif
	return 0;
}

h5_err_t
gsell's avatar
gsell committed
1321
h5priv_plot_octants (
1322 1323
        h5t_octree_t* octree
        ) {
gsell's avatar
gsell committed
1324
	H5_PRIV_API_ENTER (h5_err_t, "octree=%p", octree);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
//	h5_debug ("# start plot octants","");
	h5_float64_t bounding_box[6];
	int i;
	for (i = 0; i <= octree->current_oct_idx; i++) {
//		h5_debug (
		TRY (get_bounding_box_of_octant (octree, i, bounding_box));
		print_octant_for_gnuplot (i, bounding_box);
	}
	for (i = 0; i <= octree->current_oct_idx; i++) {
		TRY (get_bounding_box_of_octant (octree, i, bounding_box));
		printf (
				"set label \" %d \"  at first  %4.4f, first %4.4f  font \"Helvetica,7\"\n",octree->octants[i].idx, bounding_box[0] + 0.02 + 0.6 * bounding_box[1], bounding_box[2] + 0.02 + 0.4 * bounding_box[1]);
	}
//	h5_debug ("# end plot octants","");
gsell's avatar
gsell committed
1339
	H5_RETURN (H5_SUCCESS);
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
}

static h5_err_t
get_siblings(
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_idx_t* siblings_idx
        );

h5_err_t
gsell's avatar
gsell committed
1350
h5priv_plot_octant_anc (
1351 1352 1353
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
gsell's avatar
gsell committed
1354
	H5_PRIV_API_ENTER (h5_err_t, "octree=%p", octree);
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
//	h5_debug ("# start plot octants","");
	h5_float64_t bounding_box[6];
	do {
		TRY (get_siblings(octree, oct_idx, &oct_idx));
		for (int i = 0; i < NUM_OCTANTS; i++) {
			TRY (get_bounding_box_of_octant (octree, oct_idx + i, bounding_box));
			print_octant_for_gnuplot (oct_idx + i, bounding_box);

			printf (
				"set label \" %d \"  at first  %4.4f, first %4.4f  font \"Helvetica,7\"\n",octree->octants[oct_idx+i].idx, bounding_box[0] + 0.02 + 0.6 * bounding_box[1], bounding_box[2] + 0.02 + 0.4 * bounding_box[1]);
		}
	} while ((oct_idx = get_parent(octree, oct_idx)) != -1);

gsell's avatar
gsell committed
1368
	H5_RETURN (H5_SUCCESS);
1369
}
gsell's avatar
gsell committed
1370

1371
h5_err_t
gsell's avatar
gsell committed
1372
h5priv_plot_leaf_octants (
1373 1374
        h5t_octree_t* octree
        ) {
gsell's avatar
gsell committed
1375
	H5_PRIV_API_ENTER (h5_err_t, "octree=%p", octree);
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403

	h5_float64_t bounding_box[6];
	h5t_oct_iterator_t* iter;
	TRY (H5t_init_leafoct_iterator (octree, &iter));
	h5_oct_idx_t oct_idx = -1;
	while ((oct_idx = H5t_iterate_oct(iter)) != -1) {

		TRY (get_bounding_box_of_octant (octree, oct_idx, bounding_box));
		print_octant_for_gnuplot (oct_idx, bounding_box);
	}
	printf ("\n #real oct labels \n");
	TRY (H5t_init_leafoct_iterator (octree, &iter));
	while ((oct_idx = H5t_iterate_oct(iter)) != -1) {
		TRY (get_bounding_box_of_octant (octree, oct_idx, bounding_box));
		printf (
				"set label \" %d \"  at first  %4.4f, first %4.4f  font \"Helvetica,7\"\n",
				octree->octants[oct_idx].idx, bounding_box[0] + 0.02 + 0.6 * bounding_box[1], bounding_box[2] + 0.02 + 0.4 * bounding_box[1]);
	}
	printf ("\n #leaf oct labels \n");
	TRY (H5t_init_leafoct_iterator (octree, &iter));
	int counter = 0;
	while ((oct_idx = H5t_iterate_oct(iter)) != -1) {
		TRY (get_bounding_box_of_octant (octree, oct_idx, bounding_box));
		printf (
				"set label \" %d \"  at first  %4.4f, first %4.4f  font \"Helvetica,7\"\n",
				counter++, bounding_box[0] + 0.02 + 0.6 * bounding_box[1], bounding_box[2] + 0.02 + 0.4 * bounding_box[1]);
	}
	TRY (H5t_end_iterate_oct(iter));
gsell's avatar
gsell committed
1404
	H5_RETURN (H5_SUCCESS);
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
}

void print_array (
				h5_int32_t* neigh,
				h5_oct_idx_t nbr_neigh,
				int rank
					) {
			printf ("proc %d: array \n",rank);
			for (int i = 0; i < nbr_neigh; i++) {
				printf (" %d, ", neigh[i]);
			}

			printf ("\n");
		}


/*
 * begin refine octants
 */
static h5_err_t
begin_refine_octants(
        h5t_octree_t* octree
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);

	TRY (h5priv_mpi_barrier (octree->comm));
	update_internal (octree);
	octree->ref_oct_idx = octree->current_oct_idx;
gsell's avatar
gsell committed
1433
	H5_RETURN (H5_SUCCESS);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
}
/*
 * Refine an octant
 */
static h5_err_t
refine_octant(
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d", octree, oct_idx);
	int rank;
	MPI_Comm_rank (octree->comm, &rank);

	if (octree->octants[oct_idx].processor != rank ) {
		h5_debug ("Trying to refine an octant that doesn't belong to proc");
gsell's avatar
gsell committed
1449
		H5_LEAVE (H5_ERR_INVAL);
1450 1451 1452 1453
	}

	if (octree->octants[oct_idx].child_idx != -1 || octree->ref_oct_idx == -1) {
		h5_debug ("Either octant is already refined or begin_refine_octants() was not invoked");
gsell's avatar
gsell committed
1454
		H5_LEAVE (H5_ERR_INVAL);
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
	}
	if( octree->current_oct_idx + 1 + 8 >= octree->nbr_alloc_oct) {
		/*** need to allocate more memory for octants ***/
		TRY (grow_octree (octree, -1));
	}


	octree->octants[oct_idx].child_idx = octree->current_oct_idx + 1;
	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	TRY (create_octant (octree, oct_idx));

	set_leave_level (octree);

gsell's avatar
gsell committed
1481
	H5_RETURN (H5_SUCCESS);
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
}
/*
 * end refine octants
 */
static h5_err_t
end_refine_octants(
        h5t_octree_t* octree,
        h5_oct_point_t* midpoints,
        int num_midpoints
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p", octree);
	if (octree->ref_oct_idx == -1) {
gsell's avatar
gsell committed
1494
		H5_LEAVE (H5_ERR_INVAL);
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
	}

	// exchange how many new octants per proc where created
	int size,rank;
	TRY (h5priv_mpi_comm_size (octree->comm, &size));
	TRY (h5priv_mpi_comm_rank (octree->comm, &rank));
	h5_int32_t* nbr_oct_new;
	TRY (nbr_oct_new = h5_calloc (size,sizeof (*nbr_oct_new)));
	h5_int32_t total_oct_new = 0;
	h5_oct_idx_t loc_oct_new = octree->current_oct_idx - octree->ref_oct_idx;
	TRY (h5priv_mpi_allgather (&loc_oct_new,
		                          1,
		                          MPI_INT,
		                          nbr_oct_new,
		                          1,
		                          MPI_INT,
		                          octree->comm));

	// calculate own range (i.e. offset)
	h5_oct_idx_t* offset;
	TRY (offset = h5_calloc (size,sizeof(*offset)));
	total_oct_new = nbr_oct_new[0];
	for (int i = 1; i < size; i++ ) {
		total_oct_new += nbr_oct_new[i];
		offset[i] = nbr_oct_new[i-1] + offset[i-1];
	}
	// allocate enough memory for all new octants
	if (octree->ref_oct_idx + 1 + total_oct_new >= octree->nbr_alloc_oct) {
		TRY (grow_octree (octree, total_oct_new ));
	}

	// update octants with offset
	if (offset[rank] > 0) {
		for (int i =  octree->ref_oct_idx+1; i <= octree->current_oct_idx; i++) {
			if (octree->octants[i].idx > octree->ref_oct_idx) {
				octree->octants[i].idx += offset[rank];
			}
			if (octree->octants[i].parent_idx > octree->ref_oct_idx) {
				octree->octants[i].parent_idx += offset[rank];
			}
			if (octree->octants[i].child_idx > octree->ref_oct_idx) {
				octree->octants[i].child_idx += offset[rank];
			}
		}
		// update midpoints list with offset
		if (num_midpoints > 0) {
			for (int i = 0; i < num_midpoints; i++) {
				if (midpoints[i].oct > octree->ref_oct_idx) {
					midpoints[i].oct += offset[rank];
				}
			}
		}

	}
	void* sendbuf;
	void* recvbuf;
	if ((loc_oct_new) > 0) {
		TRY (sendbuf = h5_calloc (loc_oct_new ,sizeof (h5t_octant_t)));
	} else {
		sendbuf = NULL;
	}
	TRY (recvbuf = h5_calloc (total_oct_new ,sizeof (h5t_octant_t)));
	// copy new octants to send buffer
	memmove (sendbuf,
			&(octree->octants[octree->ref_oct_idx + 1]),
			loc_oct_new * sizeof (h5t_octant_t));
	octree->current_oct_idx = octree->ref_oct_idx + total_oct_new;
	// commit new type

//	TRY (h5priv_mpi_type_commit (&h5_oct_dta_types.mpi_octant));

	TRY (mpi_allgatherv (
			sendbuf,
	        nbr_oct_new[rank],
	        h5_oct_dta_types.mpi_octant,
	        recvbuf,
	        nbr_oct_new,
	        offset,
	        h5_oct_dta_types.mpi_octant,
	        octree->comm
	        ));

	memcpy (&(octree->octants[octree->ref_oct_idx + 1]), recvbuf, total_oct_new * sizeof (h5t_octant_t));

	// update parents child_idx
	//TODO could be more efficient
	for (int i = octree->current_oct_idx; i > octree->ref_oct_idx ; i--) {
		h5_oct_idx_t parent_idx = get_parent (octree, i);
		if (parent_idx < 0 || parent_idx > octree->current_oct_idx) {
gsell's avatar
gsell committed
1584
			H5_LEAVE (H5_ERR_INTERNAL);
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
		}
		octree->octants[parent_idx].child_idx = i;

	}

	octree->ref_oct_idx = -1;

	TRY (h5_free (nbr_oct_new));
	if (sendbuf != NULL) {
		TRY (h5_free (sendbuf));
	}
	TRY (h5_free (recvbuf));
	TRY (h5_free (offset));
	update_userdata (octree);
	clear_level_internal (octree);
	update_internal (octree); // maybe update leave level would be enough
gsell's avatar
gsell committed
1601
	H5_RETURN (H5_SUCCESS);
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
}

/*
 * compare func for qsort bsearch
 */
int compare_points_x(const void *p_a, const void *p_b)
{
return ((*(h5_oct_point_t*)p_a).x - (*(h5_oct_point_t*)p_b).x) >= 0 ? 1 : 0;
}
int compare_points_y(const void *p_a, const void *p_b)
{
return ((*(h5_oct_point_t*)p_a).y - (*(h5_oct_point_t*)p_b).y) >= 0 ? 1 : 0;
}
int compare_points_z(const void *p_a, const void *p_b)
{
return ((*(h5_oct_point_t*)p_a).z - (*(h5_oct_point_t*)p_b).z) >= 0 ? 1 : 0;
}
int sort_points_x(const void *p_a, const void *p_b)
{

	if (((*(h5_oct_point_t*)p_a).x - (*(h5_oct_point_t*)p_b).x) < 0 )
		return -1 ;
	if (((*(h5_oct_point_t*)p_a).x - (*(h5_oct_point_t*)p_b).x) == 0 )
		return 0;
	if (((*(h5_oct_point_t*)p_a).x - (*(h5_oct_point_t*)p_b).x) > 0 )
		return 1 ;
	 return -1; // will never be executed
}
int sort_points_y(const void *p_a, const void *p_b)
{
	if (((*(h5_oct_point_t*)p_a).y - (*(h5_oct_point_t*)p_b).y) < 0 )
		return -1 ;
	if (((*(h5_oct_point_t*)p_a).y - (*(h5_oct_point_t*)p_b).y) == 0 )
		return 0;
	if (((*(h5_oct_point_t*)p_a).y - (*(h5_oct_point_t*)p_b).y) > 0 )
		return 1 ;
	 return -1; // will never be executed
}
int sort_points_z(const void *p_a, const void *p_b)
{
	if (((*(h5_oct_point_t*)p_a).z - (*(h5_oct_point_t*)p_b).z) < 0 )
		return -1 ;
	if (((*(h5_oct_point_t*)p_a).z - (*(h5_oct_point_t*)p_b).z) == 0 )
		return 0;
	if (((*(h5_oct_point_t*)p_a).z - (*(h5_oct_point_t*)p_b).z) > 0 )
		return 1 ;
 return -1; // will never be executed
}


/*
 * multidimensional sort of array of points
 */
static h5_err_t
sort_array (
gsell's avatar
gsell committed
1657 1658 1659 1660 1661 1662
	h5_oct_point_t* key,
	h5_oct_point_t* points,
	h5_int32_t nbr_points,
	h5_oct_point_t** split,
	h5_int32_t* nbr_in_split
	) {
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
	H5_PRIV_FUNC_ENTER (h5_err_t, "key=%p, points=%p, nbr_points=%d, split=%p, nbr_in_split=%p",
			key, points, nbr_points, split, nbr_in_split);

	// sort in z
	comparison_fn_t compare;
	compare.compare = compare_points_z;
	split[0] = points;
	qsort (points, nbr_points, sizeof (*points), sort_points_z);
	split[4] = linsearch (key, points, nbr_points, sizeof (*points), compare);
	if (split[4] == NULL) {
		nbr_in_split[0] = nbr_points;
	} else {
		nbr_in_split[0] = split[4] - split[0];
	}
	nbr_in_split[4] = nbr_points - nbr_in_split[0];


	// sort in y
	compare.compare = compare_points_y;
	qsort (split[0], nbr_in_split[0], sizeof (*points), sort_points_y);
	qsort (split[4], nbr_in_split[4], sizeof (*points), sort_points_y);
	split[2] = linsearch (key, split[0], nbr_in_split[0], sizeof (*points), compare);
	split[6] = linsearch (key, split[4], nbr_in_split[4], sizeof (*points), compare);
	if (split[2] == NULL) {
		nbr_in_split[2] = 0;
	} else {
		nbr_in_split[2] = nbr_in_split[0] - (split[2] - split[0]);
	}
	nbr_in_split[0] -= nbr_in_split[2];

	if (split[6] == NULL) {
		nbr_in_split[6] = 0;
	} else {
		nbr_in_split[6] = nbr_in_split[4] - (split[6] - split[4]);
	}
	nbr_in_split[4] -= nbr_in_split[6];

	// sort in x
	compare.compare = compare_points_x;
	qsort (split[0], nbr_in_split[0], sizeof (*points), sort_points_x);
	qsort (split[2], nbr_in_split[2], sizeof (*points), sort_points_x);
	qsort (split[4], nbr_in_split[4], sizeof (*points), sort_points_x);
	qsort (split[6], nbr_in_split[6], sizeof (*points), sort_points_x);
	split[1] = linsearch (key, split[0], nbr_in_split[0], sizeof (*points), compare);
	split[3] = linsearch (key, split[2], nbr_in_split[2], sizeof (*points), compare);
	split[5] = linsearch (key, split[4], nbr_in_split[4], sizeof (*points), compare);
	split[7] = linsearch (key, split[6], nbr_in_split[6], sizeof (*points), compare);

	if (split[1] == NULL) {
		nbr_in_split[1] = 0;
	} else {
		nbr_in_split[1] = nbr_in_split[0] - (split[1] - split[0]);
	}
	nbr_in_split[0] -= nbr_in_split[1];

	if (split[3] == NULL) {
		nbr_in_split[3] = 0;
	} else {
		nbr_in_split[3] = nbr_in_split[2] - (split[3] - split[2]);
	}
	nbr_in_split[2] -= nbr_in_split[3];

	if (split[5] == NULL) {
		nbr_in_split[5] = 0;
	} else {
		nbr_in_split[5] = nbr_in_split[4] - (split[5] - split[4]);
	}
	nbr_in_split[4] -= nbr_in_split[5];

	if (split[7] == NULL) {
		nbr_in_split[7] = 0;
	} else {
		nbr_in_split[7] = nbr_in_split[6] - (split[7] - split[6]);
	}
	nbr_in_split[6] -= nbr_in_split[7];

gsell's avatar
gsell committed
1739
	H5_RETURN (H5_SUCCESS);
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
}
/*
 * get_midpoint
 */
static h5_err_t
get_midpoint (
        h5_oct_point_t* midpoint,
        h5_float64_t* bounding_box
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "midpoint=%p, bounding_box=%p", midpoint, bounding_box);
	midpoint->x = (bounding_box[3] + bounding_box[0]) / 2.0;
	midpoint->y = (bounding_box[4] + bounding_box[1]) / 2.0;
	midpoint->z = (bounding_box[5] + bounding_box[2]) / 2.0;
gsell's avatar
gsell committed
1753
	H5_RETURN (H5_SUCCESS);
1754 1755 1756 1757 1758 1759 1760
}
/*
 * function to calculate bounding box of child with orientation orient
 */

static h5_err_t
get_new_bounding_box (
gsell's avatar
gsell committed
1761 1762 1763 1764
	h5_float64_t* bb,
	h5_float64_t* new_bb,
	h5_oct_orient_t orient
	) {
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
	H5_PRIV_FUNC_ENTER (h5_err_t, "midpoint=%p, bounding_box=%p, orient=%d", bb, new_bb, orient);
	h5_float64_t xmin, xmid, xmax, ymin, ymid, ymax, zmin, zmid, zmax;

	// store all values such that bb and new_bb can be the same
	xmin = bb[0];
	xmax = bb[3];
	xmid = (xmin + xmax) / 2;

	ymin = bb[1];
	ymax = bb[4];
	ymid = (ymin + ymax) / 2;

	zmin = bb[2];
	zmax = bb[5];
	zmid = (zmin + zmax) / 2;

	// x direction
	if ((orient & 1) == 1) {
		new_bb[0] = xmid;
		new_bb[3] = xmax;
	} else {
		new_bb[0] = xmin;
		new_bb[3] = xmid;
	}
	// y direction
	if ((orient & 2) == 2) {
		new_bb[1] = ymid;
		new_bb[4] = ymax;
	} else {
		new_bb[1] = ymin;
		new_bb[4] = ymid;
	}
	// z direction
	if ((orient & 4) == 4) {
		new_bb[2] = zmid;
		new_bb[5] = zmax;
	} else {
		new_bb[2] = zmin;
		new_bb[5] = zmid;
	}
gsell's avatar
gsell committed
1805
	H5_RETURN (H5_SUCCESS);
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
}
void print_array_p (
			h5_oct_point_t* neigh,
			h5_oct_idx_t nbr_neigh,
			int rank
				) {
		printf ("proc %d: array \n",rank);
		for (int i = 0; i < nbr_neigh; i++) {
			printf (" %4.4f, %4.4f, %4.4f / %d, %lld --", neigh[i].x, neigh[i].y, neigh[i].z, neigh[i].oct, (long long) neigh[i].elem);
		}

		printf ("\n");
	}
/*
 * recursive refine oct w points
 */
static h5_err_t
recursive_ref_points (
gsell's avatar
gsell committed
1824 1825 1826 1827 1828 1829 1830
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	h5_float64_t* bounding_box,
	h5_oct_point_t* points,
	h5_int32_t nbr_points,
	h5_int32_t max_points
	) {
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
	H5_PRIV_FUNC_ENTER (h5_err_t,
			"octree=%p, oct_idx=%d points=%p, bounding_box=%p, nbr_points=%d, max_points=%d",
			octree,
			oct_idx,
			bounding_box,
			points,
			nbr_points,
			max_points);

	h5_oct_point_t midpoint;
	TRY (get_midpoint (&midpoint, bounding_box));
	if (octree->octants[oct_idx].child_idx == -1) {
		// octant needs to be refined
		TRY (refine_octant (octree, oct_idx));
	}
	h5_oct_point_t* split_arr[8];
	h5_oct_point_t** split = split_arr;
	h5_int32_t nbr_in_split_arr[8];
	h5_int32_t* nbr_in_split = nbr_in_split_arr;
	TRY (sort_array (&midpoint, points, nbr_points, split, nbr_in_split));

	for (int i = 0; i < 8; i++) {
		if (nbr_in_split[i] > max_points ||
			(h5tpriv_octant_is_full (octree, octree->octants[oct_idx].child_idx + i)
			&& nbr_in_split[i] > 0)
			) {
			h5_float64_t new_bounding_box[6];
			TRY (get_new_bounding_box (bounding_box, new_bounding_box, i));
			// call itself with points belonging to childrens octants
			TRY (recursive_ref_points (octree, octree->octants[oct_idx].child_idx + i, new_bounding_box, split[i], nbr_in_split[i], max_points));

		} else {
			// assign oct_idx to points
		//	print_array_p (points,nbr_points,32);
			for (int j = 0; j < nbr_in_split[i]; j++) {
				(split[i] + j)->oct = octree->octants[oct_idx].child_idx + i;

			}
			//print_array_p (points,nbr_points,32);
		}
	}

gsell's avatar
gsell committed
1873
	H5_RETURN (H5_SUCCESS);
1874 1875 1876 1877 1878 1879 1880
}

/*
 * refine with points
 */
static h5_err_t
refine_w_points (
gsell's avatar
gsell committed
1881 1882 1883 1884 1885 1886 1887 1888 1889
	h5t_octree_t* octree,
	h5_oct_point_t* points,
	h5_int32_t nbr_points,
	h5_int32_t max_points
	) {
	H5_PRIV_FUNC_ENTER (
		h5_err_t,
		"octree=%p, points=%p, nbr_points=%d, max_points=%d",
		octree, points, nbr_points, max_points);
1890 1891 1892
	if (nbr_points < 1) {
		TRY (begin_refine_octants (octree));
		TRY (end_refine_octants (octree, points, nbr_points));
gsell's avatar
gsell committed
1893
		H5_LEAVE (H5_SUCCESS);
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
	}

	TRY (begin_refine_octants (octree));

	if (points[0].oct == -1) { // adding points for the first time
		if (nbr_points > max_points || h5tpriv_octant_is_full (octree, 0)) {
			TRY (recursive_ref_points (octree, 0, octree->bounding_box, points, nbr_points, max_points));
		} else {
			for (int j = 0; j < nbr_points; j++) {
				points[j].oct = 0;
			}
		}
	} else { // midpoints from refining elements
		int counter = 0;
		h5_float64_t bounding_box[6];
		while (counter < nbr_points) {
			// find all points that are together in a leaf oct at the moment
			h5_oct_idx_t oct_idx = points[counter].oct;
			int first_point = counter++;
			assert ( oct_has_level (octree, oct_idx, OCT_USERLEV_LENGTH -1));
			while (counter < nbr_points && oct_idx == points[counter].oct) {
				counter++;
			}
			// check if refinement of octree is necessary (either octant is full or more than maxpoints are in leaf octant)
			if (counter - first_point > max_points || h5tpriv_octant_is_full (octree, points[first_point].oct)) {

				TRY (get_bounding_box_of_octant(octree, points[first_point].oct, bounding_box));
				TRY (recursive_ref_points (
						octree,
						points[first_point].oct,
						bounding_box,
						&points[first_point],
						counter - first_point,
						max_points));
			}

		}
	}
	TRY (end_refine_octants (octree, points, nbr_points));

gsell's avatar
gsell committed
1934
	H5_RETURN (H5_SUCCESS);
1935 1936 1937 1938 1939 1940
}
h5_err_t H5t_refine_w_points (h5t_octree_t* octree, h5_oct_point_t* points, h5_int32_t nbr_points,	h5_int32_t max_points) {
	return refine_w_points (octree, points, nbr_points,	max_points);
}
static int
bounding_box_contains_point (
gsell's avatar
gsell committed
1941 1942 1943
	h5_float64_t* bounding_box,
	h5_oct_point_t* point
	) {
1944 1945 1946 1947 1948 1949 1950 1951
	H5_PRIV_FUNC_ENTER (h5_err_t, "bounding_box=%p, point=%p", bounding_box, point);
	if ( 	bounding_box[0] <= point->x &&
			bounding_box[3] > point->x &&
			bounding_box[1] <= point->y &&
			bounding_box[4] > point->y &&
			bounding_box[2] <= point->z &&
			bounding_box[5] > point->z
			) {
gsell's avatar
gsell committed
1952
		H5_LEAVE (1);
1953
	} else {
gsell's avatar
gsell committed
1954
		H5_LEAVE (0);
1955
	}
gsell's avatar
gsell committed
1956
	H5_RETURN (H5_ERR);
1957 1958 1959
}
static h5_err_t
get_bounding_box_of_octant (
gsell's avatar
gsell committed
1960 1961 1962 1963
	h5t_octree_t* const octree,
	h5_oct_idx_t oct_idx,
	h5_float64_t* bounding_box
	) {
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, bounding_box=%p", octree, oct_idx, bounding_box);

	// get level and allocate memory to store all ancestors
	h5_lvl_idx_t level = get_oct_level (octree, oct_idx);
	h5_oct_idx_t* ancestors = NULL;
	TRY (ancestors = h5_calloc (level + 1, sizeof (*ancestors)));

	// retrieve all ancestors
	int i = 0;
	ancestors[i++] = oct_idx;
	h5_oct_idx_t parent = get_parent (octree, oct_idx);

	while (parent >= 0) {
		ancestors[i++] = parent;
		parent = get_parent (octree, parent);
	}
	// get bounding box of octree
	memcpy (bounding_box, octree->bounding_box, 6 * sizeof (*bounding_box));

	// calculate recursively the bounding box of oct_idx
	h5_oct_orient_t direction = 0;
	while (--i > 0) {
		direction = ancestors[i-1] - get_children(octree, ancestors[i]);
		TRY (get_new_bounding_box(bounding_box, bounding_box, direction));
	}
	TRY (h5_free (ancestors));
gsell's avatar
gsell committed
1990
	H5_RETURN (H5_SUCCESS);
1991 1992 1993 1994 1995 1996 1997 1998
}
h5_err_t
H5t_get_bounding_box_of_octant (h5t_octree_t* const octree, h5_oct_idx_t oct_idx, h5_float64_t* bounding_box) {
	return get_bounding_box_of_octant (octree, oct_idx, bounding_box);
}

static h5_oct_idx_t
find_leafoctant_of_point (
gsell's avatar
gsell committed
1999 2000 2001 2002 2003
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	h5_float64_t* bounding_box,
	h5_oct_point_t* point
	) {
2004 2005 2006 2007
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, point=%p", octree, oct_idx, point);

	// if point is not contained in octree return error
	if (!bounding_box_contains_point (bounding_box, point)) {
gsell's avatar
gsell committed
2008
		H5_LEAVE (H5_ERR_INVAL);
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
	}
	h5_oct_idx_t ret_oct_idx = oct_idx;
	// if octant has children find the child that contains it and search -> recursively
	if (octree->octants[oct_idx].child_idx != -1) {
		oct_idx = octree->octants[oct_idx].child_idx;
		h5_float64_t new_bb[6];
		for (int i = 0; i < 8; i++) {
			TRY (get_new_bounding_box(bounding_box, new_bb, i));
		if (bounding_box_contains_point (new_bb, point)) {
				TRY (ret_oct_idx = find_leafoctant_of_point (octree, oct_idx + i, new_bb, point));
				break;
			}
		}
	}

gsell's avatar
gsell committed
2024
	H5_RETURN (ret_oct_idx);
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
}
h5_oct_idx_t
H5t_find_leafoctant_of_point (h5t_octree_t* octree, h5_oct_idx_t oct_idx, h5_float64_t* bounding_box, h5_oct_point_t* point) {
	return find_leafoctant_of_point (octree, oct_idx, bounding_box, point);
}


/*
 * if points are assigned to octants which have children, points get assigned to leaf level octants
 */
static h5_err_t
add_points_to_leaf (
gsell's avatar
gsell committed
2037 2038 2039 2040
	h5t_octree_t* octree,
	h5_oct_point_t* points,
	h5_int32_t nbr_points
	) {
2041 2042 2043 2044 2045 2046 2047
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, points=%p, nbr_points=%d", octree, points, nbr_points);
	h5_float64_t bounding_box[6];
	for (int i = 0; i < nbr_points; i++) {
		TRY (get_bounding_box_of_octant (octree, points[i].oct, bounding_box));
		TRY (points[i].oct = find_leafoctant_of_point (octree, points[i].oct, bounding_box, &points[i]));
	}

gsell's avatar
gsell committed
2048
	H5_RETURN (H5_SUCCESS);
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
}

h5_err_t H5t_add_points_to_leaf (h5t_octree_t* octree, h5_oct_point_t* points, h5_int32_t nbr_points) {
	return add_points_to_leaf (octree, points, nbr_points);
}
/*
 * Get siblings
 */
static h5_err_t
get_siblings(
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_idx_t* siblings_idx
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, siblings_idx=%p", octree, oct_idx, siblings_idx);


	if (oct_idx == 0) {
		/*** root node has no siblings***/
gsell's avatar
gsell committed
2068
		H5_LEAVE (H5_ERR_INVAL);
2069 2070 2071 2072 2073
	} else {
		h5_oct_idx_t parent_idx = octree->octants[oct_idx].parent_idx;
		*siblings_idx = octree->octants[parent_idx].child_idx;
	}

gsell's avatar
gsell committed
2074
	H5_RETURN (H5_SUCCESS);
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
}

/*
 * Get sibling
 */
static h5_oct_idx_t
get_sibling(
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d", octree, oct_idx);
	assert (oct_idx >0);
gsell's avatar
gsell committed
2087
	H5_RETURN (
2088
		octree->octants[octree->octants[oct_idx].parent_idx].child_idx);
2089
}
2090

2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
h5_oct_idx_t H5t_get_sibling(h5t_octree_t* octree, h5_oct_idx_t oct_idx) {
	return get_sibling(octree, oct_idx);
}

#if 0
/*
 * returns memory usage per processor
 */
static h5_err_t
mem_usage (
        h5t_octree_t* octree,
        h5_int32_t* mem_use
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, mem_use=%p", octree, mem_use);
	*mem_use = sizeof (*octree) + (sizeof (*(octree->octants)) + octree->size_userdata) * octree->nbr_alloc_oct;
gsell's avatar
gsell committed
2106
	H5_RETURN (H5_SUCCESS);
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
}
#endif

#if 0
/*
 * free neigh memory
 */
static h5_err_t
free_neigh (
        h5_oct_idx_t* neighbors,
        h5_oct_idx_t* ancestor_of_neigh
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "neighbors=%p, ancestor_of_neigh=%p", neighbors, ancestor_of_neigh);
	TRY (h5_free (neighbors));
	TRY (h5_free (ancestor_of_neigh));
gsell's avatar
gsell committed
2122
	H5_RETURN (H5_SUCCESS);
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
}
#endif

/*
 * compare func for qsort bsearch
 */
int compare_oct_idx(const void *p_a, const void *p_b)
{
return *(h5_oct_idx_t*)p_a - *(h5_oct_idx_t*)p_b;
}
/*
 * add oct_idx to array if it doesn't exists yet (without sorting)
 */
static h5_err_t
add_neigh (
gsell's avatar
gsell committed
2138 2139 2140 2141 2142
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	h5_oct_idx_t* neigh,
	h5_oct_idx_t* num_neigh
	) {
2143 2144 2145 2146 2147 2148 2149
	H5_PRIV_FUNC_ENTER (h5_err_t,
			"octree=%p, oct_idx=%d, ancestor_of_neigh=%p, nbr_anc_of_neigh=%d",
			octree, oct_idx,
			neigh, *num_neigh);
	int i = 0;
	for (; i < *num_neigh; i++) {
		if (oct_idx == neigh[i]) {
gsell's avatar
gsell committed
2150
			H5_LEAVE (H5_SUCCESS);
2151 2152 2153 2154 2155 2156
		}
	}

	neigh[*num_neigh] = oct_idx;
	(*num_neigh)++;

gsell's avatar
gsell committed
2157
	H5_RETURN (H5_SUCCESS);
2158 2159 2160 2161 2162 2163
}
/*
 * add ancestor to array if it doesn't exists yet (sort ancestors)
 */
static h5_err_t
add_ancestor (
gsell's avatar
gsell committed
2164 2165 2166 2167 2168
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	h5_oct_idx_t* ancestor_of_neigh,
	h5_oct_idx_t* nbr_anc_of_neigh
	) {
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
	H5_PRIV_FUNC_ENTER (h5_err_t,
			"octree=%p, oct_idx=%d, ancestor_of_neigh=%p, nbr_anc_of_neigh=%d",
			octree, oct_idx,
			ancestor_of_neigh, *nbr_anc_of_neigh);


	if ( bsearch (&oct_idx, ancestor_of_neigh, (size_t) *nbr_anc_of_neigh, sizeof (*nbr_anc_of_neigh), compare_oct_idx) == NULL) {
		 ancestor_of_neigh[*nbr_anc_of_neigh] = oct_idx;
		 (*nbr_anc_of_neigh)++;
		 qsort(ancestor_of_neigh, (size_t) *nbr_anc_of_neigh, sizeof (*nbr_anc_of_neigh), compare_oct_idx);
	}
gsell's avatar
gsell committed
2180
	H5_RETURN (H5_SUCCESS);
2181 2182 2183 2184 2185 2186
}
/*
 * get ancestors of octant
 */
static h5_err_t
get_ancestors (
gsell's avatar
gsell committed
2187 2188 2189 2190 2191
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	h5_oct_idx_t* ancestor_of_neigh,
	h5_oct_idx_t* nbr_anc_of_neigh
	) {
2192 2193 2194 2195 2196 2197 2198 2199
	H5_PRIV_FUNC_ENTER (h5_err_t,
			"octree=%p, oct_idx=%d, ancestor_of_neigh=%p, nbr_anc_of_neigh=%d",
			octree, oct_idx,
			ancestor_of_neigh, *nbr_anc_of_neigh);
	h5_oct_idx_t parent_idx = oct_idx;
	while ((parent_idx = get_parent (octree, parent_idx)) != -1 ) {
			TRY (add_ancestor (octree, parent_idx, ancestor_of_neigh, nbr_anc_of_neigh));
	}
gsell's avatar
gsell committed
2200
	H5_RETURN (H5_SUCCESS);
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
}
/*
 * get children with given orientation on certain level
 */
static h5_err_t
get_kids_with_orient (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_idx_t* neighbors,
        h5_oct_idx_t* nbr_neigh,
        h5_oct_idx_t* ancestor_of_neigh,
        h5_oct_idx_t* nbr_anc_of_neigh,
        h5_oct_level_t userlevel,
        h5_int32_t orient,
        h5_int32_t direction,
        h5_int32_t ifsibling
        );

static h5_err_t
get_kids_with_orient (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_idx_t* neighbors,
        h5_oct_idx_t* nbr_neigh,
        h5_oct_idx_t* ancestor_of_neigh,
        h5_oct_idx_t* nbr_anc_of_neigh,
        h5_oct_level_t userlevel,
        h5_int32_t orient,
        h5_int32_t direction,
        h5_int32_t ifsibling
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t,
			"octree=%p, oct_idx=%d, neighbors=%p, nbr_neigh=%d,"
			" ancestor_of_neigh=%p, nbr_anc_of_neigh=%d,"
			" userlevel=%d, orient=%d, direction=%d",
			octree, oct_idx, neighbors, *nbr_neigh,
			ancestor_of_neigh, *nbr_anc_of_neigh,
			userlevel, orient, direction);
	assert (*nbr_neigh > -1);
	assert (*nbr_neigh < OCT_MAX_NEIGHBORS);
	assert (*nbr_anc_of_neigh > -1);
	assert (*nbr_anc_of_neigh < OCT_MAX_NEIGHBORS);
	assert (oct_idx > 0);

	if (oct_has_level (octree, oct_idx, userlevel)) {
		// octant is neighbor on correct level
		neighbors[*nbr_neigh] = oct_idx;
		(*nbr_neigh)++;
		TRY (get_ancestors (octree, oct_idx, ancestor_of_neigh, nbr_anc_of_neigh));

	} else {

		// add octant as ancestor and go to children
		h5_oct_idx_t children_oct_idx = get_children (octree, oct_idx);
		if (children_oct_idx == -1) {
gsell's avatar
gsell committed
2256
			H5_LEAVE (H5_ERR_INTERNAL);
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
		}
		for (int i = 0; i < 8; i++) {
			if ( (ifsibling && (i & direction) == (orient & direction)) ||
				(!ifsibling && (i & direction) != (orient & direction))) {
				// check chlidrens children
				TRY (get_kids_with_orient (octree,
								children_oct_idx + i,
								neighbors,
								nbr_neigh,
								ancestor_of_neigh,
								nbr_anc_of_neigh,
								userlevel,
								orient,
								direction,
								ifsibling));
				}


		}
	}


gsell's avatar
gsell committed
2279
	H5_RETURN (H5_SUCCESS);
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
}


/*
 * get nearest common ancestor
 * returns -1 on geom boundary
 */
static h5_err_t
get_nca (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_dir_t direction,
        h5_oct_orient_t* orient_child_nca,
        h5_oct_idx_t* nca
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, direction=%d, orient_child_nca=%p nca=%p",
			octree, oct_idx, direction, orient_child_nca, nca);
	// TODO maybe check that siblings don't fullfill requirement
	h5_oct_idx_t parent_idx = get_parent (octree, oct_idx);
	assert (parent_idx > -1);
	h5_oct_orient_t orient = get_orient (octree, oct_idx);
	h5_oct_orient_t parent_orient = get_orient (octree, parent_idx);
	memcpy (&orient_child_nca[0],&orient, sizeof (orient));
	*nca = -1;
	int counter = 1;
	while (octree->octants[parent_idx].parent_idx != -1) {
		parent_orient = get_orient (octree, parent_idx);
		memcpy (&orient_child_nca[counter++],&parent_orient, sizeof (parent_orient));
		if ((orient & direction) != (parent_orient & direction)) {
			// oct_idx is sibling of neighbors
			*nca = get_parent (octree, parent_idx);
			parent_orient = get_orient (octree, *nca);
			memcpy (&orient_child_nca[counter++],&parent_orient, sizeof (parent_orient));
			break;
		}
		parent_idx = get_parent (octree, parent_idx);
	}

gsell's avatar
gsell committed
2318
	H5_RETURN (H5_SUCCESS);
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
}
/*
 * get the child with given orientation, return -1 if no children
 */

static h5_oct_idx_t
get_child_with_orient (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_orient_t orient
        ) {
	if (octree->octants[oct_idx].child_idx == -1) {
		return -1;
	} else {
		return octree->octants[oct_idx].child_idx + orient;
	}
}


/*
 * get the smallest possible neighbor up to the same octree level & userlevel
 */
static h5_err_t
get_equal_sized_neigh (
        h5t_octree_t* octree,
        h5_oct_idx_t oct_idx,
        h5_oct_dir_t direction,
        h5_oct_orient_t* sibling_orient,
        h5_oct_idx_t nca,
        h5_oct_idx_t* neigh,
        h5_oct_level_t userlevel
        ) {
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, direction=%d, sibling_orient=%p"
			", nca=%d, neigh=%p, userlevel=%d",
			octree,
			oct_idx,
			direction,
			sibling_orient,
			nca,
			neigh,
			userlevel);
	int orient_idx = get_oct_level (octree, oct_idx) - get_oct_level (octree, nca) -1;
	h5_oct_idx_t child = get_child_with_orient (octree, nca, sibling_orient[orient_idx--] ^ direction);
	h5_oct_orient_t child_orient= 0;

	while ((get_oct_level (octree, oct_idx) > get_oct_level (octree, child)) // neighbor is still coarser
			&& (oct_has_level (octree, child, userlevel ) == 0) ) { // userlevel not reached

		child_orient = sibling_orient[orient_idx--] ^ direction;
		if ( (child = get_child_with_orient (octree, child, child_orient)) == -1 ) {
gsell's avatar
gsell committed
2369
			H5_LEAVE (H5_ERR_INVAL);
2370 2371 2372 2373
		}
	}
	*neigh = child;

gsell's avatar
gsell committed
2374
	H5_RETURN (H5_SUCCESS);
2375 2376 2377
}
static h5_err_t
check_neigh_cand (
gsell's avatar
gsell committed
2378 2379 2380 2381 2382
	h5t_octree_t* octree,
	h5_oct_idx_t oct_idx,
	h5_oct_idx_t* neig,
	h5_oct_idx_t* num_neigh
	) {
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
	H5_PRIV_FUNC_ENTER (h5_err_t, "octree=%p, oct_idx=%d, neig=%p, num_neigh=%p, ",
			octree, oct_idx, neig, num_neigh);
	int if_check_fails_is_int_error = 0;
	if (*num_neigh > 1) {
		if_check_fails_is_int_error = 1;
	}
	h5_float64_t bb[6];
	get_bounding_box_of_octant(octree, oct_idx, bb);

	for (int i = 0; i < *num_neigh; i++) {
		h5_float64_t n_bb[6];
		get_bounding_box_of_octant(octree, neig[i], n_bb);
		if (bb[0] > n_bb[3] || bb[3] < n_bb[0] ||
			bb[1] > n_bb[4] || bb[4] < n_bb[1] ||
			bb[2] > n_bb[5] || bb[5] < n_bb[2] 	) {
			// should be deleted
			if (if_check_fails_is_int_error) {
gsell's avatar
gsell committed
2400
				H5_LEAVE (H5_ERR_INTERNAL);
2401 2402 2403 2404 2405 2406 2407 2408
			} else {
				*num_neigh = 0;
			}
		}

	}


gsell's avatar
gsell committed
2409
	H5_RETURN (H5_SUCCESS);
2410 2411 2412 2413 </