! ******************************************************************
! Generacja siatki MES
! Milenin Andre, AGH, 2008, milenin@agh.edu.pl
! ******************************************************************

SUBROUTINE GenGrid2d(Hmax, Bmax, nhH,nhB, Gr)
use My_typ;
use GlobData;
implicit none;
integer*4 :: i,j,inh,ine,id, nhH,nhB;
integer*4 :: iost,St(4);
integer*4 :: i1,i2,i3,i4;
real*8    :: Hmax, Bmax,x,y,dx,dy;
logical	  :: St_OK(4);
type(gr2d):: Gr;

	Gr%nh = nhH*nhB;
	Gr%ne = (nhH-1)*(nhB-1);
	Gr%nbn = 4;
	Gr%ncn = 4;
	Gr%nhPov = nhB;
	ALLOCATE ( Gr%nd(Gr%nh) ,stat=iost);
	ALLOCATE ( Gr%EL(Gr%ne) ,stat=iost);

	dx = Bmax/(nhB-1);
	dy = Hmax/(nhH-1);

	x=0;
	inh=0;
	do i=1,nhB
	y=0;
		do j=1,nhH
		inh = inh + 1;
		Gr%nd(inh)%x = x;
		Gr%nd(inh)%y = y;
		Gr%nd(inh)%status = 0;
		y=y+dy;
		end do;
	x=x+dx;
	end do;

	ine=0;
	do i=1,nhB-1
		do j=1,nhH-1
		ine = ine + 1;
		i1 = (i-1)*nhH + j;
		i2 = i*nhH + j;
		i3 = i*nhH + j + 1;
		i4 = (i-1)*nhH + j + 1;
		Gr%EL(ine)%nop(1) = i1;
		Gr%EL(ine)%nop(2) = i2;
		Gr%EL(ine)%nop(3) = i3;
		Gr%EL(ine)%nop(4) = i4;
		end do;
	end do;
	
	do i=1, Gr%nh
		x = Gr%nd(i)%x;
		y = Gr%nd(i)%y;
		Gr%nd(i)%status = 0;
		Gr%nd(i)%t = mTbegin;

		if (x>=(Bmax-0.00001)) then 
		                       Gr%nd(i)%status=1;  ! Boczna powierchnia
							   end if;

		if (x<=0.00001)		   then
							   Gr%nd(i)%status=1;  ! Boczna powierchnia
							   end if;

		if (y>=(Hmax-0.00001)) then 
		                       Gr%nd(i)%status=1;  ! Gorna powierchnia
							   end if;
		if (y<=0.00001)        then 
		                       Gr%nd(i)%status=1; ! dolna powierchnia
							   end if;
	end do;

	do ine = 1, Gr%ne
		DO i=1,4
			Id = ABS(Gr%EL(ine)%NOP(I));
			St(i) = Gr%nd(Id)%status;
		END DO
		St_OK(2)=(St(1)>=1).and.(St(2)>=1);
		St_OK(3)=(St(2)>=1).and.(St(3)>=1);
		St_OK(4)=(St(3)>=1).and.(St(4)>=1);
		St_OK(1)=(St(4)>=1).and.(St(1)>=1);
		Gr%EL(ine)%Npov=0;
		do i = 1, 4
			if(St_OK(i)) then
			Gr%EL(ine)%Npov = Gr%EL(ine)%Npov + 1;
			Gr%EL(ine)%aPov( Gr%EL(ine)%Npov ) = i;
			end if;
		end do;
	end do;

END SUBROUTINE GenGrid2d



SUBROUTINE WriteControlPoints( );
use My_typ;
use GlobData;
implicit none;

	WRITE(88,'(" ", F9.4, 9(" ",F7.1))') mTau, mGr%nd(mContrPoints(1))%t, mGr%nd(mContrPoints(2))%t, &
	mGr%nd(mContrPoints(3))%t, mGr%nd(mContrPoints(4))%t, mGr%nd(mContrPoints(5))%t, mGr%nd(mContrPoints(6))%t, &
	mGr%nd(mContrPoints(7))%t, mGr%nd(mContrPoints(8))%t, mGr%nd(mContrPoints(9))%t;

	WRITE(89,'(" ", F9.4, 9(" ",F7.1))') mTau, mGr%nd(mContrPoints(1))%CR, mGr%nd(mContrPoints(2))%CR, &
	mGr%nd(mContrPoints(3))%CR, mGr%nd(mContrPoints(4))%CR, mGr%nd(mContrPoints(5))%CR, mGr%nd(mContrPoints(6))%CR, &
	mGr%nd(mContrPoints(7))%CR, mGr%nd(mContrPoints(8))%CR, mGr%nd(mContrPoints(9))%CR;

	WRITE( *,'(" ", F9.4, 4(" ",F7.1))' ) mTau, mGr%nd(mContrPoints(1))%t, &
	mGr%nd(mContrPoints(3))%t, mGr%nd(mContrPoints(4))%t,mGr%nd(mContrPoints(6))%t;

END SUBROUTINE WriteControlPoints;



SUBROUTINE WriteControlPointsBegin( );
use My_typ;
use GlobData;
implicit none;
integer*4 :: iPrint,i

	OPEN (88, FILE='OutDataT.txt');
	OPEN (89, FILE='OutDataCR.txt');

	do iPrint=88,89
		WRITE(iPrint,'(" **********************************************************")')
		WRITE(iPrint,'(" * PROGRAM PLATE2d                                        *")')
		WRITE(iPrint,'(" * Milenin Andryj CopyRight, 2008                         *")')
		WRITE(iPrint,'(" * milenin@agh.edu.pl                               *")')
		WRITE(iPrint,'(" ********************************************************** ")')
		WRITE(iPrint,'("  ")')
		WRITE(iPrint,'("  ")')

		WRITE(iPrint,'(" ********** Coordinates of the control points ************")')
		do i=1,9
			WRITE(iPrint,'(" No=", I4," X=", F8.2," Y=", F8.2)') mContrPoints(i),mcpX(i),mcpY(i);
		end do
		WRITE(iPrint,'(" ********************************************************** ")')
	end do;
END SUBROUTINE WriteControlPointsBegin;




SUBROUTINE SetControlPoints( );
use My_typ;
use GlobData;
implicit none;
integer*4 :: i,j;
real*8    :: Rr,Rmin;

	mcpX(1) = 0.0;			mcpY(1) = 0.0;
	mcpX(2) = mB0/2;		mcpY(2) = 0.0
	mcpX(3) = mB0;			mcpY(3) = 0.0
	mcpX(4) = 0.0;			mcpY(4) = mH0/2.0;
	mcpX(5) = mB0/2;		mcpY(5) = mH0/2.0;
	mcpX(6) = mB0;			mcpY(6) = mH0/2.0;
	mcpX(7) = 0.0;			mcpY(7) = mH0
	mcpX(8) = mB0/2;		mcpY(8) = mH0
	mcpX(9) = mB0;			mcpY(9) = mH0;

	do j=1, 9
	Rmin=1e10;
		do i=1,mGr%nh
		Rr = sqrt( (mcpX(j)-mGr%nd(i)%x)**2 + (mcpY(j)-mGr%nd(i)%y)**2 );
			if (Rr<=Rmin) then
				mContrPoints(j)=i;
				Rmin=Rr;
			end if;
		end do;
	end do;

END SUBROUTINE SetControlPoints


