//		-*- Mode: Java -*-
// Author(s):	J. Robert Buchanan and Zhoude Shao
// Address:	Department of Mathematics
//		Millersville University
//      	P.O. Box 1002
//		Millersville, PA 17551-0302
// Phone:	717-871-7305
// FAX:		717-871-7948
// Email:	Robert.Buchanan@millersville.edu, Zhoude.Shao@millersville.edu
//
// Date:	09/10/2024
//
// Purpose:	Crank-Nicolson implicit finite difference method for solving the
//		homogeneous wave equation with Dirichlet boundary conditions.
//		This program accompanies Exercise 14.4.8.
//
// To compile:	javac exercise15b.java
// To execute:	java exercise15b
//
import java.io.*;

public class exercise15b
{
    public static double f(double x)	// initial displacement
    {
	double result	;
	
	result = 0.0	;

	return(result)	;
    }	// end of function f( )

    public static double g(double x)	// initial velocity
    {
	double result	;

	result = Math.sin(2.0*Math.PI*x)	;
	
	return(result)	;
    }	// end of function g( )

    // Crout factorization for solving a tridiagonal system of equations
    public static void crout(
			     int nrows	,	// dimension of system
			     double [][] a,	// tridiagonal linear system
			     double [] x	// solution of system
			     )
    {
	int i;	// counter(s)
	double l[][] = new double[nrows][];	// lower triangular matrix
	double u[][] = new double[nrows][];	// upper triangular matrix
	double z[] = new double[nrows];
	for ( i=0 ; i<nrows ; i++ ) {
	    l[i] = new double[2];
	    u[i] = new double[2];
	}	// end of for ( i ) loop
	l[0][0] = a[0][0];
	u[0][1] = a[0][1]/l[0][0];
	z[0] = a[0][3]/l[0][0];
	for ( i=1 ; i<nrows-1 ; i++ ) {
	    l[i][0] = a[i][0];
	    l[i][1] = a[i][1]-l[i][0]*u[i-1][1];
	    u[i][1] = a[i][2]/l[i][1];
	    z[i] = (a[i][3]-l[i][0]*z[i-1])/l[i][1];
	}	// end of for ( i ) loop
	l[nrows-1][0] = a[nrows-1][1];
	l[nrows-1][1] = a[nrows-1][2]-l[nrows-1][0]*u[nrows-2][1];
	z[nrows-1] = (a[nrows-1][3]-l[nrows-1][0]*z[nrows-2])/l[nrows-1][1];
	x[nrows-1] = z[nrows-1];
	for ( i=nrows-2 ; i>-1 ; i-- ) {
	    x[i] = z[i]-u[i][1]*x[i+1];
	}	// end of for ( i ) loop
	return	;
    }	// end of function crout( )
    
    public static void main(String args[])
    {
	int i, j;	// counter(s)
	int nelts = 10-1;
	double h;	// spatial discretization step size
	double k;	// temporal discretization step size
	double r;	// method parameter
	double a[][] = new double[nelts][4];	// augmented tridiagonal matrix
	double soln[][] = new double[3][nelts];	// result for new time

	// Set step sizes
	h = 1.0/((double)(nelts+1))	;
	k = 0.05;
	r = k/h	;
	// Set initial conditions and populate augmented tridiagonal matrix
	for ( j=0 ; j<nelts ; j++ ) soln[0][j]=f((double)(j+1)*h);
	// First row of matrix
	a[0][0] = 2.0*(2.0+r*r)	;
	a[0][1] = -r*r	;
	a[0][2] = 0.0	;
	a[0][3] = 2.0*(2.0-r*r)*soln[0][0]+r*r*soln[0][1]	;
	a[0][3] += k*2.0*(2.0+r*r)*g((double)1*h)-k*r*r*g((double)2*h)	;
	// Rows 2 through nelts-1 of matrix
	for ( i=1 ; i<(nelts-1) ; i++ ) {
	    a[i][0] = -r*r	;
	    a[i][1] = 2.0*(2.0+r*r)	;
	    a[i][2] = -r*r	;
	    a[i][3] = r*r*(soln[0][i-1]+soln[0][i+1])+2.0*(2.0-r*r)*soln[0][i];
	    a[i][3] += -k*r*r*(g((double)i*h)+g((double)(i+2)*h))	;
	    a[i][3] += k*2.0*(2.0+r*r)*g((double)(i+1)*h);
	}	// end of for ( i ) loop
	// Last row of matrix
	a[nelts-1][0] = 0.0	;
	a[nelts-1][1] = -r*r	;
	a[nelts-1][2] = 2.0*(2.0+r*r)	;
	a[nelts-1][3] = r*r*soln[0][nelts-2]+2.0*(2.0-r*r)*soln[0][nelts-1];
	a[nelts-1][3] += -k*r*r*g((double)(nelts-1)*h)	;
	a[nelts-1][3] += k*2.0*(2.0+r*r)*g((double)nelts*h)	;
	// Output the first time step
	System.out.printf("%4.2f ", 0.0)	;
	System.out.printf("%8.4f ", 0.0)	;
	for ( i=0 ; i<nelts ; i++ ) System.out.printf("%8.4f ",soln[0][i]);
	System.out.printf("%8.4f\n", 0.0)	;
	// Output the second time step
	crout(nelts,a,soln[1])	;
	System.out.printf("%4.2f ", k)	;
	System.out.printf("%8.4f ", 0.0)	;
	for ( i=0 ; i<nelts ; i++ ) System.out.printf("%8.4f ",soln[1][i]);
	System.out.printf("%8.4f\n", 0.0)	;
	// Iterate for t = 0, k, 2k, ..., 10k - the remaining time steps
	for ( j=2 ; j<10+1 ; j++ ) {
	    // First row of matrix
	    a[0][3] = 4.0*(2.0-r*r)*soln[(j-1)%3][0]+2.0*r*r*soln[(j-1)%3][1];
	    a[0][3] += -2.0*(2.0+r*r)*soln[(j-2)%3][0]+r*r*soln[(j-2)%3][1];
	    // Rows 2 through N-2
	    for ( i=1 ; i<(nelts-1) ; i++ ) {
		a[i][3] = 2.0*r*r*(soln[(j-1)%3][i-1]+soln[(j-1)%3][i+1]);
		a[i][3] += 4.0*(2.0-r*r)*soln[(j-1)%3][i]	;
		a[i][3] += r*r*(soln[(j-2)%3][i-1]+soln[(j-2)%3][i+1]);
		a[i][3] += -2.0*(2.0+r*r)*soln[(j-2)%3][i]	;
	    } // end of for ( i ) loop
	    // Last row of matrix
	    a[nelts-1][3] = 2.0*r*r*soln[(j-1)%3][nelts-2]	;
	    a[nelts-1][3] += 4.0*(2.0-r*r)*soln[(j-1)%3][nelts-1]	;
	    a[nelts-1][3] += r*r*soln[(j-2)%3][nelts-2]	;
	    a[nelts-1][3] += -2.0*(2.0+r*r)*soln[(j-2)%3][nelts-1]	;
	    crout(nelts,a,soln[j%3])	;
	    System.out.printf("%4.2f ", (double)j*k)	;
	    System.out.printf("%8.4f ",0.0)	;
	    for ( i=0 ; i<nelts ; i++ ) {
		System.out.printf("%8.4f ", soln[j%3][i])	;
	    }	// end of for ( i ) loop
	    System.out.printf("%8.4f\n",0.0)	;
	}	// end of for ( j ) loop
    }	// end of function main( )
}

//
//	EOF
//

