Skip to content
Snippets Groups Projects
grid3d.h 42.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Debolskiy Andrey's avatar
    Debolskiy Andrey committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 169 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 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 238 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 300 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 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 594 595 596 597 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 661 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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 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 805 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 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 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 908 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 941 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 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    #pragma once
    
    // [grid3d.h]: 3D primary virtual grid
    //
    // -------------------------------------------------------------------------------------------- //
    // TO DO:
    // 
    
    #include "nse-sys.h"
    #include "mpi-com3d.h"
    #include "cart-sys3d.h"
    #include "vecmath.h"
    
    #include "grid-id.h"
    
    #ifndef EXCLUDE_GPU_BRANCH
    #include "grid.cuh"
    #endif
    
    // use hand-coded openmp array reduction 
    //   in -xy averaging without critical sections
    //   * - use on Intel Xeon Phi
    #define USE_OMP_PAR_REDUCE_IN_AVG_XY
    
    #define USE_GRID3D_BINARY_LOCATE
    
    
    namespace nse
    {
    	namespace nse_const3d 
    	{
    		enum nodeType {
    			nodeU = 0, nodeV = 1, nodeW = 2, nodeC = 3,
    			nodeUV = 4, nodeUW = 5, nodeVW = 6, nodeUVW = 7
    		};
    	}
    
    	// --- grid node after averaging
    	nse_const3d::nodeType avg_node(
    		const nse_const3d::nodeType node,
    		const nse_const3d::axisType axis);
    	// ------------------------------------------------------------------------------------------------ //
    
    
    	// * 3D primart grid: Grid3d< T > [ T = float, double ] * //
    	// =======================================================================
    	template< typename T, memType mem = memCPU >
    	class Grid3d
    	{
    	public:
    		Grid3d();
    		Grid3d(const Grid3d& grid);
    		virtual ~Grid3d();
    
    		// grid parameters by axis
    		int dim_size(const nse_const3d::axisType axis) const;
    		int mpi_dim_size(const nse_const3d::axisType axis) const;
    
    		int ghost_region_size(	// single layer //
    			const nse_const3d::axisType axis) const;	
    
    
    		// MPI global cell index [== -1 - on failure]
    		virtual int mpi_locate_x(const T x) const = 0;
    		virtual int mpi_locate_y(const T y) const = 0;
    		virtual int mpi_locate_z(const T z) const = 0;
    
    		// local cell index [== -1 - on failure]
    		//  - only one MPI process returns >=0 for (x,y,z) calls
    		virtual int locate_x(const T x) const;
    		virtual int locate_y(const T y) const;
    		virtual int locate_z(const T z) const;
    
    		// local local cell index on single MPI process [== -1 - on failure]
    		//  - searching in segment [iexp - iwidth, iexp + iwdith]
    		virtual int locate_local_x(const T x, const int iexp, const int iwidth) const;
    		virtual int locate_local_y(const T y, const int jexp, const int jwidth) const;
    		virtual int locate_local_z(const T z, const int kexp, const int kwidth) const;
    		//	* search for a list of coordinates
    		virtual void locate_local_x(const T* _RESTRICT const x,
    			int* _RESTRICT iexp, const int iwidth, const int n) const;
    		virtual void locate_local_y(const T* _RESTRICT const y,
    			int* _RESTRICT jexp, const int jwidth, const int n) const;
    		virtual void locate_local_z(const T* _RESTRICT const z,
    			int* _RESTRICT kexp, const int kwidth, const int n) const;
    
    		// (i,j,k) MPI-local coordinates [input - global index]
    		//	- only one MPI process returns >= 0
    		virtual int i_local_coord(const int i) const;
    		virtual int j_local_coord(const int j) const;
    		virtual int k_local_coord(const int k) const;
    
    		// interpolation (local relative to (x,y,z) position in processor domain)
    		virtual T c_interp(const T* X, const T x, const T y, const T z) const = 0;
    		virtual T u_interp(const T* U, const T x, const T y, const T z) const = 0;
    		virtual T v_interp(const T* V, const T x, const T y, const T z) const = 0;
    		virtual T w_interp(const T* W, const T x, const T y, const T z) const = 0;
    
    		// local interpolation on single MPI process 
    		// [unsafe] - not checking if coordinates [(x,y,z),(i,j,k)] are correct
    		virtual T u_interp_local(const T* _RESTRICT const U,
    			const T x, const T y, const T z, const int i, const int j, const int k) const = 0;
    		virtual T v_interp_local(const T* _RESTRICT const V,
    			const T x, const T y, const T z, const int i, const int j, const int k) const = 0;
    		virtual T w_interp_local(const T* _RESTRICT const W,
    			const T x, const T y, const T z, const int i, const int j, const int k) const = 0;
    
    		virtual void u_interp_local(T* _RESTRICT uinterp, const T* _RESTRICT const U,
    			const T* _RESTRICT const x, const T* _RESTRICT const y, const T* _RESTRICT const z,
    			const int* _RESTRICT const i, const int* _RESTRICT const j, const int* _RESTRICT const k, const int n) const = 0;
    		virtual void v_interp_local(T* _RESTRICT vinterp, const T* _RESTRICT const V,
    			const T* _RESTRICT const x, const T* _RESTRICT const y, const T* _RESTRICT const z,
    			const int* _RESTRICT const i, const int* _RESTRICT const j, const int* _RESTRICT const k, const int n) const = 0;
    		virtual void w_interp_local(T* _RESTRICT winterp, const T* _RESTRICT const W,
    			const T* _RESTRICT const x, const T* _RESTRICT const y, const T* _RESTRICT const z,
    			const int* _RESTRICT const i, const int* _RESTRICT const j, const int* _RESTRICT const k, const int n) const = 0;
    
    		// interpolation (global to (x,y,z) point position)
    		virtual T mpi_c_interp(const T* X, const T x, const T y, const T z) const;
    		virtual T mpi_u_interp(const T* U, const T x, const T y, const T z) const;
    		virtual T mpi_v_interp(const T* V, const T x, const T y, const T z) const;
    		virtual T mpi_w_interp(const T* W, const T x, const T y, const T z) const;
    
    		// slicing
    		// -xy slice, [C,U,V,W] -> [C,U,V,C]
    		void c_slice_at_z(T* Pxy, const T* X, const T z) const;
    		void u_slice_at_z(T* Pxy, const T* U, const T z) const;
    		void v_slice_at_z(T* Pxy, const T* V, const T z) const;
    		void w_slice_at_z(T* Pxy, const T* W, const T z) const;
    		// -xz slice, [C,U,V,W] -> [C,U,C,W]
    		void c_slice_at_y(T* Pxz, const T* X, const T y) const;
    		void u_slice_at_y(T* Pxz, const T* U, const T y) const;
    		void v_slice_at_y(T* Pxz, const T* V, const T y) const;
    		void w_slice_at_y(T* Pxz, const T* W, const T y) const;
    		// -yz slice [C,U,V,W] -> [C,C,V,W]
    		void c_slice_at_x(T* Pyz, const T* X, const T x) const;
    		void u_slice_at_x(T* Pyz, const T* U, const T x) const;
    		void v_slice_at_x(T* Pyz, const T* V, const T x) const;
    		void w_slice_at_x(T* Pyz, const T* W, const T x) const;
    
    		// MPI gather-scatter by axis //
    		template< memType memOUT = memCPU, memType memIN = memCPU, typename Tin >
    		void mpi_gather(Tin* _RESTRICT out, const Tin* _RESTRICT in,
    			const int host, const nse_const3d::axisType axis) const;
    		template< memType memOUT = memCPU, memType memIN = memCPU, typename Tin >
    		void mpi_scatter(Tin* _RESTRICT out, const Tin* _RESTRICT in,
    			const int host, const nse_const3d::axisType axis) const;
    
    		// MPI gather coordinates //
    		template< memType memOUT = memCPU >
    		void mpi_gather_center_coord(T* _RESTRICT out,
    			const int host, const nse_const3d::axisType axis) const;
    		template< memType memOUT = memCPU >
    		void mpi_gather_edge_coord(T* _RESTRICT out,
    			const int host, const nse_const3d::axisType axis) const;
    
    		template< memType memOUT = memCPU >
    		void mpi_gather_center_coord(
    			T* _RESTRICT xout, T* _RESTRICT yout, T* _RESTRICT zout, const int host) const;
    		template< memType memOUT = memCPU >
    		void mpi_gather_edge_coord(
    			T* _RESTRICT xout, T* _RESTRICT yout, T* _RESTRICT zout, const int host) const;
    
    
    		// grid re-interpolation out(current grid), in(input grid)
    		virtual void grid_reinterp(T* Xout, const T* Xin,				// local in array //
    			const nse_const3d::nodeType node, const GridId< T >& id) const = 0;
    
    		// GridId on 3D grids //
    		virtual void set_id(GridId< T >& id) const;
    		virtual bool check_id(const GridId< T >& id) const;
    
    		bool check_id_dims(const GridId< T >& id) const;
    
    
    		virtual void set_id(GridId< T >& id, const nse_const3d::axisType axis) const;
    		virtual bool check_id(const GridId< T >& id, const nse_const3d::axisType axis) const;
    
    		bool check_id_dims(const GridId< T >& id, const nse_const3d::axisType axis) const;
    
    	public:
    
    		mpiCom3d mpi_com;
    
    		int size, nx, ny, nz, nyz;
    		int mpi_nx, mpi_ny, mpi_nz,
    			mpi_nxy, mpi_nxz, mpi_nyz, mpi_size;
    
    		int gcx, gcy, gcz;
    
    		T x, y, z;
    		T length, width, height;
    
    		T mpi_x, mpi_y, mpi_z;
    		T mpi_length, mpi_width, mpi_height;
    
    		T *px, *py, *pz;		// cell-center coordinates //
    		T *ex, *ey, *ez;		// cell-edge coordinates //
    	};
    }
    
    // Implementation [misc]
    // -------------------------------------------------------------------------------------------- //
    inline
    nse::nse_const3d::nodeType nse::avg_node(const nse_const3d::nodeType node,
    	const nse_const3d::axisType axis)
    {
    	if (axis == nse_const3d::axisX) {
    		if ((node == nse_const3d::nodeU) || (node == nse_const3d::nodeUV) ||
    			(node == nse_const3d::nodeUW) || (node == nse_const3d::nodeUVW))
    			return nse_const3d::nodeU;
    
    		return nse_const3d::nodeC;
    	}
    	if (axis == nse_const3d::axisY) {
    		if ((node == nse_const3d::nodeV) || (node == nse_const3d::nodeUV) ||
    			(node == nse_const3d::nodeVW) || (node == nse_const3d::nodeUVW))
    			return nse_const3d::nodeV;
    
    		return nse_const3d::nodeC;
    	}
    	if (axis == nse_const3d::axisZ) {
    		if ((node == nse_const3d::nodeW) || (node == nse_const3d::nodeUW) ||
    			(node == nse_const3d::nodeVW) || (node == nse_const3d::nodeUVW))
    			return nse_const3d::nodeW;
    
    		return nse_const3d::nodeC;
    	}
    
    	if (axis == nse_const3d::axisXY) {
    		if ((node == nse_const3d::nodeV) || (node == nse_const3d::nodeVW))
    			return nse_const3d::nodeV;
    		if ((node == nse_const3d::nodeU) || (node == nse_const3d::nodeUW))
    			return nse_const3d::nodeU;
    		if ((node == nse_const3d::nodeUV) || (node == nse_const3d::nodeUVW))
    			return nse_const3d::nodeUV;
    
    		return nse_const3d::nodeC;
    	}
    	if (axis == nse_const3d::axisXZ) {
    		if ((node == nse_const3d::nodeW) || (node == nse_const3d::nodeVW))
    			return nse_const3d::nodeW;
    		if ((node == nse_const3d::nodeU) || (node == nse_const3d::nodeUV))
    			return nse_const3d::nodeU;
    		if ((node == nse_const3d::nodeUW) || (node == nse_const3d::nodeUVW))
    			return nse_const3d::nodeUW;
    
    		return nse_const3d::nodeC;
    	}
    	if (axis == nse_const3d::axisYZ) {
    		if ((node == nse_const3d::nodeW) || (node == nse_const3d::nodeUW))
    			return nse_const3d::nodeW;
    		if ((node == nse_const3d::nodeV) || (node == nse_const3d::nodeUV))
    			return nse_const3d::nodeV;
    		if ((node == nse_const3d::nodeVW) || (node == nse_const3d::nodeUVW))
    			return nse_const3d::nodeVW;
    
    		return nse_const3d::nodeC;
    	}
    
    	// axis == nse_const3d::axisXYZ
    	return node;
    }
    // -------------------------------------------------------------------------------------------- //
    
    // Implementation, Grid3d:
    // -------------------------------------------------------------------------------------------- //
    namespace nse
    {
    	template< typename T, memType mem >
    	Grid3d< T, mem > ::Grid3d(
    	) : mpi_com(),
    		size(0), nx(0), ny(0), nz(0), nyz(0),
    		mpi_size(0), mpi_nx(0), mpi_ny(0), mpi_nz(0),
    		mpi_nxy(0), mpi_nxz(0), mpi_nyz(0),
    		gcx(0), gcy(0), gcz(0),
    
    		x((T)0), y((T)0), z((T)0),
    		length((T)0), width((T)0), height((T)0),
    		mpi_x((T)0), mpi_y((T)0), mpi_z((T)0),
    		mpi_length((T)0), mpi_width((T)0), mpi_height((T)0)
    	{
    	}
    
    	template< typename T, memType mem >
    	Grid3d< T, mem > ::Grid3d(
    		const Grid3d< T, mem >& grid)
    		: mpi_com(grid.mpi_com),
    		size(grid.size), nx(grid.nx), ny(grid.ny), nz(grid.nz), nyz(grid.nyz),
    		mpi_size(grid.size), mpi_nx(grid.mpi_nx), mpi_ny(grid.mpi_ny), mpi_nz(grid.mpi_nz),
    		mpi_nxy(grid.mpi_nxy), mpi_nxz(grid.mpi_nxz), mpi_nyz(grid.mpi_nyz),
    		gcx(grid.gcx), gcy(grid.gcy), gcz(grid.gcz),
    
    		x(grid.x), y(grid.y), z(grid.z),
    		mpi_x(grid.mpi_x), mpi_y(grid.mpi_y), mpi_z(grid.mpi_z),
    
    		length(grid.length), width(grid.width), height(grid.mpi_height),
    		mpi_length(grid.mpi_length), mpi_width(grid.mpi_width), mpi_height(grid.mpi_height)
    	{
    		if (size > 0) {
    			allocate<mem>(&px, nx); mcopy<mem, mem>(px, grid.px, nx);
    			allocate<mem>(&py, ny); mcopy<mem, mem>(py, grid.py, ny);
    			allocate<mem>(&pz, nz); mcopy<mem, mem>(pz, grid.pz, nz);
    
    			allocate<mem>(&ex, nx); mcopy<mem, mem>(ex, grid.ex, nx);
    			allocate<mem>(&ey, ny); mcopy<mem, mem>(ey, grid.ey, ny);
    			allocate<mem>(&ez, nz); mcopy<mem, mem>(ez, grid.ez, nz);
    		}
    	}
    
    	template< typename T, memType mem >
    	Grid3d< T, mem > :: ~Grid3d(
    	)
    	{
    		if (size > 0) {
    
    			deallocate<mem>(px); deallocate<mem>(py); deallocate<mem>(pz);
    			deallocate<mem>(ex); deallocate<mem>(ey); deallocate<mem>(ez);
    		}
    
    	}
    
    	template< typename T, memType mem >
    	int Grid3d< T, mem > ::dim_size(const nse_const3d::axisType axis) const
    	{
    		switch (axis)
    		{
    		case nse_const3d::axisX: return nx;
    		case nse_const3d::axisY: return ny;
    		case nse_const3d::axisZ: return nz;
    		case nse_const3d::axisXY: return nx * ny;
    		case nse_const3d::axisXZ: return nx * nz;
    		case nse_const3d::axisYZ: return nyz;
    		case nse_const3d::axisXYZ: return size;
    		default: return -1;
    		}
    	}
    
    	template< typename T, memType mem >
    	int Grid3d< T, mem > ::mpi_dim_size(const nse_const3d::axisType axis) const
    	{
    		switch (axis)
    		{
    		case nse_const3d::axisX: return mpi_nx;
    		case nse_const3d::axisY: return mpi_ny;
    		case nse_const3d::axisZ: return mpi_nz;
    		case nse_const3d::axisXY: return mpi_nxy;
    		case nse_const3d::axisXZ: return mpi_nxz;
    		case nse_const3d::axisYZ: return mpi_nyz;
    		case nse_const3d::axisXYZ: return mpi_size;
    		default: return -1;
    		}
    	}
    
    	template< typename T, memType mem >
    	int Grid3d< T, mem > ::ghost_region_size(const nse_const3d::axisType axis) const
    	{
    		switch (axis)
    		{
    		case nse_const3d::axisX: return gcx;
    		case nse_const3d::axisY: return gcy;
    		case nse_const3d::axisZ: return gcz;
    		case nse_const3d::axisXY: return gcx * gcy;
    		case nse_const3d::axisXZ: return gcx * gcz;
    		case nse_const3d::axisYZ: return gcy * gcz;
    		case nse_const3d::axisXYZ: return gcx * gcy * gcz;
    		default: return -1;
    		}
    	}
    
    	template< typename T, memType mem >
    	inline int Grid3d< T, mem > ::locate_x(const T _x) const
    	{
    #ifndef EXCLUDE_GPU_BRANCH
    		if (mem == memGPU) {
    			if (mpi_com.rank_x == 0)
    				return nse_gpu::locate(_x, ex, nx, gcx, 0);
    			else
    				return nse_gpu::locate(_x, ex, nx, gcx, 1);
    		}
    		else
    #endif
    		{	// memCPU //
    
    #ifdef USE_GRID3D_BINARY_LOCATE
    			const int min_binary_size = 16;
    #endif
    
    			int i;
    			int ibeg = gcx, iend = nx - gcx - 1;
    
    
    			if (mpi_com.rank_x == 0)
    			{
    #ifdef USE_GRID3D_BINARY_LOCATE	
    				while (iend - ibeg >= min_binary_size) {
    					i = (ibeg + iend) / 2;
    					if (_x < ex[i]) {
    						iend = i - 1; continue;
    					}
    					if (_x > ex[i + 1]) {
    						ibeg = i + 1; continue;
    					}
    
    					return i;
    				}
    #endif
    				for (i = ibeg; i <= iend; i++)
    					if ((_x >= ex[i]) && (_x <= ex[i + 1])) { return i; }
    			}
    			else
    			{
    #ifdef USE_GRID3D_BINARY_LOCATE
    				while (iend - ibeg >= min_binary_size) {
    					i = (ibeg + iend) / 2;
    					if (_x <= ex[i]) {
    						iend = i - 1; continue;
    					}
    					if (_x > ex[i + 1]) {
    						ibeg = i + 1; continue;
    					}
    
    					return i;
    				}
    #endif
    				for (i = ibeg; i <= iend; i++)
    					if ((_x > ex[i]) && (_x <= ex[i + 1])) { return i; }
    			}
    
    			return -1;
    		}
    	}
    
    	template< typename T, memType mem >
    	inline int Grid3d< T, mem > ::locate_y(const T _y) const
    	{
    #ifndef EXCLUDE_GPU_BRANCH
    		if (mem == memGPU) {
    			if (mpi_com.rank_y == 0)
    				return nse_gpu::locate(_y, ey, ny, gcy, 0);
    			else
    				return nse_gpu::locate(_y, ey, ny, gcy, 1);
    		}
    		else
    #endif
    		{	// memCPU //
    
    #ifdef USE_GRID3D_BINARY_LOCATE
    			const int min_binary_size = 16;
    #endif
    
    			int j;
    			int jbeg = gcy, jend = ny - gcy - 1;
    
    
    			if (mpi_com.rank_y == 0) {
    #ifdef USE_GRID3D_BINARY_LOCATE
    
    				while (jend - jbeg >= min_binary_size) {
    					j = (jbeg + jend) / 2;
    					if (_y < ey[j]) {
    						jend = j - 1; continue;
    					}
    					if (_y > ey[j + 1]) {
    						jbeg = j + 1; continue;
    					}
    
    					return j;
    				}
    #endif
    				for (j = jbeg; j <= jend; j++)
    					if ((_y >= ey[j]) && (_y <= ey[j + 1])) { return j; }
    			}
    			else
    			{
    #ifdef USE_GRID3D_BINARY_LOCATE
    				while (jend - jbeg >= min_binary_size) {
    					j = (jbeg + jend) / 2;
    					if (_y <= ey[j]) {
    						jend = j - 1; continue;
    					}
    					if (_y > ey[j + 1]) {
    						jbeg = j + 1; continue;
    					}
    
    					return j;
    				}
    #endif
    				for (j = jbeg; j <= jend; j++)
    					if ((_y > ey[j]) && (_y <= ey[j + 1])) { return j; }
    			}
    
    			return -1;
    		}
    	}
    
    	template< typename T, memType mem >
    	inline int Grid3d< T, mem > ::locate_z(const T _z) const
    	{
    #ifndef EXCLUDE_GPU_BRANCH
    		if (mem == memGPU) {
    			if (mpi_com.rank_z == 0)
    				return nse_gpu::locate(_z, ez, nz, gcz, 0);
    			else
    				return nse_gpu::locate(_z, ez, nz, gcz, 1);
    		}
    		else
    #endif
    		{	// memCPU //
    
    #ifdef USE_GRID3D_BINARY_LOCATE
    			const int min_binary_size = 16;
    #endif
    
    			int k;
    			int kbeg = gcz, kend = nz - gcz - 1;
    
    
    			if (mpi_com.rank_z == 0) {
    #ifdef USE_GRID3D_BINARY_LOCATE
    				while (kend - kbeg >= min_binary_size) {
    					k = (kbeg + kend) / 2;
    					if (_z < ez[k]) {
    						kend = k - 1; continue;
    					}
    					if (_z > ez[k + 1]) {
    						kbeg = k + 1; continue;
    					}
    
    					return k;
    				}
    #endif
    				for (k = kbeg; k <= kend; k++)
    					if ((_z >= ez[k]) && (_z <= ez[k + 1])) { return k; }
    			}
    			else
    			{
    #ifdef USE_GRID3D_BINARY_LOCATE
    				while (kend - kbeg >= min_binary_size) {
    					k = (kbeg + kend) / 2;
    					if (_z <= ez[k]) {
    						kend = k - 1; continue;
    					}
    					if (_z > ez[k + 1]) {
    						kbeg = k + 1; continue;
    					}
    
    					return k;
    				}
    #endif
    				for (k = kbeg; k <= kend; k++)
    					if ((_z > ez[k]) && (_z <= ez[k + 1])) { return k; }
    			}
    
    			return -1;
    		}
    	}
    
    	template< typename T, memType mem >
    	inline int Grid3d< T, mem > ::locate_local_x(
    		const T _x, const int iexp, const int iwidth) const
    	{
    #ifdef USE_GRID3D_BINARY_LOCATE
    		const int min_binary_size = 16;
    #endif
    
    		int i;
    		int ibeg = max(iexp - iwidth, gcx);
    		int iend = min(iexp + iwidth, nx - gcx - 1);
    
    		if (mpi_com.rank_x == 0) {
    
    #ifdef USE_GRID3D_BINARY_LOCATE
    			while (iend - ibeg >= min_binary_size) {
    				i = (ibeg + iend) / 2;
    				if (_x < ex[i]) {
    					iend = i - 1; continue;
    				}
    				if (_x > ex[i + 1]) {
    					ibeg = i + 1; continue;
    				}
    
    				return i;
    			}
    #endif
    			for (i = ibeg; i <= iend; i++)
    				if ((_x >= ex[i]) && (_x <= ex[i + 1])) { return i; }
    		}
    		else
    		{
    #ifdef USE_GRID3D_BINARY_LOCATE
    			while (iend - ibeg >= min_binary_size) {
    				i = (ibeg + iend) / 2;
    				if (_x <= ex[i]) {
    					iend = i - 1; continue;
    				}
    				if (_x > ex[i + 1]) {
    					ibeg = i + 1; continue;
    				}
    
    				return i;
    			}
    #endif
    			for (i = ibeg; i <= iend; i++)
    				if ((_x > ex[i]) && (_x <= ex[i + 1])) { return i; }
    		}
    
    		return -1;
    	}
    
    	template< typename T, memType mem >
    	inline int Grid3d< T, mem > ::locate_local_y(
    		const T _y, const int jexp, const int jwidth) const
    	{
    #ifdef USE_GRID3D_BINARY_LOCATE
    		const int min_binary_size = 16;
    #endif
    
    		int j;
    		int jbeg = max(jexp - jwidth, gcy);
    		int jend = min(jexp + jwidth, ny - gcy - 1);
    
    		if (mpi_com.rank_y == 0) {
    #ifdef USE_GRID3D_BINARY_LOCATE
    			while (jend - jbeg >= min_binary_size) {
    				j = (jbeg + jend) / 2;
    				if (_y < ey[j]) {
    					jend = j - 1; continue;
    				}
    				if (_y > ey[j + 1]) {
    					jbeg = j + 1; continue;
    				}
    
    				return j;
    			}
    #endif
    			for (j = jbeg; j <= jend; j++)
    				if ((_y >= ey[j]) && (_y <= ey[j + 1])) { return j; }
    		}
    		else
    		{
    #ifdef USE_GRID3D_BINARY_LOCATE
    			while (jend - jbeg >= min_binary_size) {
    				j = (jbeg + jend) / 2;
    				if (_y <= ey[j]) {
    					jend = j - 1; continue;
    				}
    				if (_y > ey[j + 1]) {
    					jbeg = j + 1; continue;
    				}
    
    				return j;
    			}
    #endif
    			for (j = jbeg; j <= jend; j++)
    				if ((_y > ey[j]) && (_y <= ey[j + 1])) { return j; }
    		}
    
    		return -1;
    	}
    
    	template< typename T, memType mem >
    	inline int Grid3d< T, mem > ::locate_local_z(
    		const T _z, const int kexp, const int kwidth) const
    	{
    #ifdef USE_GRID3D_BINARY_LOCATE
    		const int min_binary_size = 16;
    #endif
    
    		int k;
    		int kbeg = max(kexp - kwidth, gcz);
    		int kend = min(kexp + kwidth, nz - gcz - 1);
    
    		if (mpi_com.rank_z == 0) {
    #ifdef USE_GRID3D_BINARY_LOCATE
    			while (kend - kbeg >= min_binary_size) {
    				k = (kbeg + kend) / 2;
    				if (_z < ez[k]) {
    					kend = k - 1; continue;
    				}
    				if (_z > ez[k + 1]) {
    					kbeg = k + 1; continue;
    				}
    
    				return k;
    			}
    #endif
    			for (k = kbeg; k <= kend; k++)
    				if ((_z >= ez[k]) && (_z <= ez[k + 1])) { return k; }
    		}
    		else
    		{
    #ifdef USE_GRID3D_BINARY_LOCATE
    			while (kend - kbeg >= min_binary_size) {
    				k = (kbeg + kend) / 2;
    				if (_z <= ez[k]) {
    					kend = k - 1; continue;
    				}
    				if (_z > ez[k + 1]) {
    					kbeg = k + 1; continue;
    				}
    
    				return k;
    			}
    #endif
    			for (k = kbeg; k <= kend; k++)
    				if ((_z > ez[k]) && (_z <= ez[k + 1])) { return k; }
    		}
    
    		return -1;
    	}
    
    	template< typename T, memType mem >
    	inline void Grid3d< T, mem > ::locate_local_x(const T* _RESTRICT const _x,
    		int* _RESTRICT ipos, const int iwidth, const int n) const
    	{
    		int m, i;
    		int ibeg, iend, idx;
    
    		if (mpi_com.rank_x == 0) {
    #pragma omp parallel for private(m, i, ibeg, iend, idx) shared(ipos)
    			for (m = 0; m < n; m++) {
    
    				ibeg = max(ipos[m] - iwidth, gcx);
    				iend = min(ipos[m] + iwidth, nx - gcx - 1);
    
    				idx = -1;
    				for (i = ibeg; i <= iend; i++)
    					if ((_x[m] >= ex[i]) && (_x[m] <= ex[i + 1])) { idx = i; break; }
    				ipos[m] = idx;
    			}
    		}
    		else
    		{
    #pragma omp parallel for private(m, i, ibeg, iend, idx) shared(ipos)
    			for (m = 0; m < n; m++) {
    
    				ibeg = max(ipos[m] - iwidth, gcx);
    				iend = min(ipos[m] + iwidth, nx - gcx - 1);
    
    				idx = -1;
    				for (i = ibeg; i <= iend; i++)
    					if ((_x[m] > ex[i]) && (_x[m] <= ex[i + 1])) { idx = i; break; }
    				ipos[m] = idx;
    			}
    		}
    	}
    
    	template< typename T, memType mem >
    	inline void Grid3d< T, mem > ::locate_local_y(const T* _RESTRICT const _y,
    		int* _RESTRICT jpos, const int jwidth, const int n) const
    	{
    		int m, j;
    		int jbeg, jend, idx;
    
    		if (mpi_com.rank_y == 0) {
    #pragma omp parallel for private(m, j, jbeg, jend, idx) shared(jpos)
    			for (m = 0; m < n; m++) {
    
    				jbeg = max(jpos[m] - jwidth, gcy);
    				jend = min(jpos[m] + jwidth, ny - gcy - 1);
    
    				idx = -1;
    				for (j = jbeg; j <= jend; j++)
    					if ((_y[m] >= ey[j]) && (_y[m] <= ey[j + 1])) { idx = j; break; }
    				jpos[m] = idx;
    			}
    		}
    		else
    		{
    #pragma omp parallel for private(m, j, jbeg, jend, idx) shared(jpos)
    			for (m = 0; m < n; m++) {
    
    				jbeg = max(jpos[m] - jwidth, gcy);
    				jend = min(jpos[m] + jwidth, ny - gcy - 1);
    
    				idx = -1;
    				for (j = jbeg; j <= jend; j++)
    					if ((_y[m] > ey[j]) && (_y[m] <= ey[j + 1])) { idx = j; break; }
    				jpos[m] = idx;
    			}
    		}
    	}
    
    	template< typename T, memType mem >
    	inline void Grid3d< T, mem > ::locate_local_z(const T* _RESTRICT const _z,
    		int* _RESTRICT kpos, const int kwidth, const int n) const
    	{
    		int m, k;
    		int kbeg, kend, idx;
    
    		if (mpi_com.rank_z == 0) {
    #pragma omp parallel for private(m, k, kbeg, kend, idx) shared(kpos)
    			for (m = 0; m < n; m++) {
    
    				kbeg = max(kpos[m] - kwidth, gcz);
    				kend = min(kpos[m] + kwidth, nz - gcz - 1);
    
    				idx = -1;
    				for (k = kbeg; k <= kend; k++)
    					if ((_z[m] >= ez[k]) && (_z[m] <= ez[k + 1])) { idx = k; break; }
    				kpos[m] = idx;
    			}
    		}
    		else
    		{
    #pragma omp parallel for private(m, k, kbeg, kend, idx) shared(kpos)
    			for (m = 0; m < n; m++) {
    
    				kbeg = max(kpos[m] - kwidth, gcz);
    				kend = min(kpos[m] + kwidth, nz - gcz - 1);
    
    				idx = -1;
    				for (k = kbeg; k <= kend; k++)
    					if ((_z[m] > ez[k]) && (_z[m] <= ez[k + 1])) { idx = k; break; }
    				kpos[m] = idx;
    			}
    		}
    	}
    
    	template< typename T, memType mem >
    	int Grid3d< T, mem > ::i_local_coord(const int i) const
    	{
    		if ((i < 0) & (i > mpi_nx - 1)) return -1;
    
    		const int shmx = (mpi_com.rank_x == 0) ? gcx : 0;
    		const int shpx = (mpi_com.rank_x == mpi_com.size_x - 1) ? gcx : 0;
    
    		int ip = i - par_local_offset(mpi_nx, gcx,
    			mpi_com.rank_x, mpi_com.size_x);
    
    		return ((ip >= gcx - shmx) && (ip < nx - gcx + shpx)) ? ip : -1;
    	}
    
    	template< typename T, memType mem >
    	int Grid3d< T, mem > ::j_local_coord(const int j) const
    	{
    		if ((j < 0) && (j > mpi_ny - 1)) return -1;
    
    		const int shmy = (mpi_com.rank_y == 0) ? gcy : 0;
    		const int shpy = (mpi_com.rank_y == mpi_com.size_y - 1) ? gcy : 0;
    
    		int jp = j - par_local_offset(mpi_ny, gcy,
    			mpi_com.rank_y, mpi_com.size_y);
    
    		return ((jp >= gcy - shmy) && (jp < ny - gcy + shpy)) ? jp : -1;
    	}
    
    	template< typename T, memType mem >
    	int Grid3d< T, mem > ::k_local_coord(const int k) const
    	{
    		if ((k < 0) && (k > mpi_nz - 1)) return -1;
    
    		const int shmz = (mpi_com.rank_z == 0) ? gcz : 0;
    		const int shpz = (mpi_com.rank_z == mpi_com.size_z - 1) ? gcz : 0;
    
    		int kp = k - par_local_offset(mpi_nz, gcz,
    			mpi_com.rank_z, mpi_com.size_z);
    
    		return ((kp >= gcz - shmz) && (kp < nz - gcz + shpz)) ? kp : -1;
    	}
    
    	template< typename T, memType mem >
    	T Grid3d< T, mem > ::mpi_c_interp(const T* X, const T _px, const T _py, const T _pz) const
    	{
    		return mpi_allreduce(
    			c_interp(X, _px, _py, _pz), MPI_SUM, mpi_com.comm);
    	}
    
    	template< typename T, memType mem >
    	T Grid3d< T, mem > ::mpi_u_interp(const T* U, const T _px, const T _py, const T _pz) const
    	{
    		return mpi_allreduce(
    			u_interp(U, _px, _py, _pz), MPI_SUM, mpi_com.comm);
    	}
    
    	template< typename T, memType mem >
    	T Grid3d< T, mem > ::mpi_v_interp(const T* V, const T _px, const T _py, const T _pz) const
    	{
    		return mpi_allreduce(
    			v_interp(V, _px, _py, _pz), MPI_SUM, mpi_com.comm);
    	}
    
    	template< typename T, memType mem >
    	T Grid3d< T, mem > ::mpi_w_interp(const T* W, const T _px, const T _py, const T _pz) const
    	{
    		return mpi_allreduce(
    			w_interp(W, _px, _py, _pz), MPI_SUM, mpi_com.comm);
    	}
    
    	template< typename T, memType mem >
    	void Grid3d< T, mem >::c_slice_at_z(T* Pxy, const T* X, const T _pz) const
    	{
    		int i, j, k = locate_z(_pz);
    		int index, host_rank = -1;
    
    		null(Pxy, nx * ny);
    		if ((k >= gcz) && (k < nz - gcz)) {
    
    			const int kpos = (_pz < pz[k]) ? k : k + 1;
    			const T alpha = (_pz - pz[kpos - 1]) / (pz[kpos] - pz[kpos - 1]);
    
    #pragma omp parallel for private( i, j, index ) shared( Pxy )
    			for (i = gcx; i < nx - gcx; i++) {
    				for (j = gcy; j < ny - gcy; j++) {
    					index = i * nyz + j * nz + kpos;
    
    					Pxy[i * ny + j] = ((T)1.0 - alpha) * X[index - 1] + alpha * X[index];
    				}
    			}
    
    			MPI_Comm_rank(mpi_com.comm_z, &host_rank);
    		}
    
    		mpi_allreduce(&host_rank, MPI_MAX, mpi_com.comm_z);
    		if (host_rank >= 0)
    			mpi_broadcast(Pxy, nx * ny, host_rank, mpi_com.comm_z);
    	}
    
    	template< typename T, memType mem >
    	void Grid3d< T, mem >::u_slice_at_z(T* Pxy, const T* U, const T _pz) const
    	{
    		int i, j, k = locate_z(_pz);
    		int index, host_rank = -1;
    
    		null(Pxy, nx * ny);
    		if ((k >= gcz) && (k < nz - gcz)) {
    
    			const int kpos = (_pz < pz[k]) ? k : k + 1;
    			const T alpha = (_pz - pz[kpos - 1]) / (pz[kpos] - pz[kpos - 1]);
    
    #pragma omp parallel for private( i, j, index ) shared( Pxy )
    			for (i = gcx; i < nx - gcx + 1; i++) {
    				for (j = gcy; j < ny - gcy; j++) {
    					index = i * nyz + j * nz + kpos;
    
    					Pxy[i * ny + j] = ((T)1.0 - alpha) * U[index - 1] + alpha * U[index];
    				}
    			}
    
    			MPI_Comm_rank(mpi_com.comm_z, &host_rank);
    		}
    
    		mpi_allreduce(&host_rank, MPI_MAX, mpi_com.comm_z);
    		if (host_rank >= 0)
    			mpi_broadcast(Pxy, nx * ny, host_rank, mpi_com.comm_z);
    	}
    
    	template< typename T, memType mem >
    	void Grid3d< T, mem >::v_slice_at_z(T* Pxy, const T* V, const T _pz) const
    	{
    		int i, j, k = locate_z(_pz);
    		int index, host_rank = -1;
    
    		null(Pxy, nx * ny);
    		if ((k >= gcz) && (k < nz - gcz)) {
    
    			const int kpos = (_pz < pz[k]) ? k : k + 1;
    			const T alpha = (_pz - pz[kpos - 1]) / (pz[kpos] - pz[kpos - 1]);
    
    #pragma omp parallel for private( i, j, index ) shared( Pxy )
    			for (i = gcx; i < nx - gcx; i++) {
    				for (j = gcy; j < ny - gcy + 1; j++) {
    					index = i * nyz + j * nz + kpos;
    
    					Pxy[i * ny + j] = ((T)1.0 - alpha) * V[index - 1] + alpha * V[index];
    				}
    			}
    
    			MPI_Comm_rank(mpi_com.comm_z, &host_rank);
    		}
    
    		mpi_allreduce(&host_rank, MPI_MAX, mpi_com.comm_z);
    		if (host_rank >= 0)
    			mpi_broadcast(Pxy, nx * ny, host_rank, mpi_com.comm_z);
    	}
    
    	template< typename T, memType mem >
    	void Grid3d< T, mem >::w_slice_at_z(T* Pxy, const T* W, const T _pz) const
    	{
    		int i, j, k = locate_z(_pz);
    		int index, host_rank = -1;
    
    		null(Pxy, nx * ny);
    		if ((k >= gcz) && (k < nz - gcz))
    		{
    			const int kpos = k + 1;
    			const T alpha = (_pz - ez[kpos - 1]) / (ez[kpos] - ez[kpos - 1]);
    
    #pragma omp parallel for private( i, j, index ) shared( Pxy )
    			for (i = gcx; i < nx - gcx; i++) {
    				for (j = gcy; j < ny - gcy; j++) {
    					index = i * nyz + j * nz + kpos;
    
    					Pxy[i * ny + j] = ((T)1.0 - alpha) * W[index - 1] + alpha * W[index];
    				}
    			}
    
    			MPI_Comm_rank(mpi_com.comm_z, &host_rank);
    		}