/********** ExplicitDiffusion2: solves the diffusion equation for the test problem using the explicit difference method This uses an entire array for the solution - compare Mathematica code uses a few vectors. [in]: int M : number of time steps int N : number of positive and negative space steps [out] : array of values in file for solution at given time This uses static dimensionalization to avoid array class issues Version 1.0, William Shaw, 2006. *************/ #include #include #include using namespace std; void ExplicitDiffusion(int N, int M) { int i, j; double dt = 0.1/M ; double dx = 2.0/N;; const double PI = 3.141592653589793; double solvals[500][500] = {0.0}; // stores solution values, hard wired! double alpha = 0.0; // key diffusion parameter alpha = dt/(dx*dx); //output dt cout << "dt = " << dt << "\n"; //output dt cout << "dx = " << dx << "\n"; // output alpha cout << "alpha = " << alpha << "\n"; // initialize initital data for (j = 0; j <= 2*N; j++) { solvals[0][j] = sin(PI*(j-N)*dx/2); } // initialize boundary conditions for (i = 1; i< M; i++) { solvals[i][0] = 0; solvals[i][2*N] = 0; } // Run the explicit algorithm for (i= 1; i<= M; i++) { for (j = 1; j<= 2*N-1; j++) {solvals[i][j] = (1-2*alpha)*solvals[i-1][j] + alpha*(solvals[i-1][j-1]+solvals[i-1][j+1]); } } ofstream out("fdoutput.txt"); for (j=0; j<= 2*N; j++) { out.precision(15); out << solvals[M][j] << "\n"; } return ; } #include #include using namespace std; int main() { double result; char name[20]; int N; int M; cout << "Explicit Diffusion Example \n "; cout << "Enter number of time steps (M) \n "; cin >> M; cout << M << " time steps \n" ; cout << "Enter space step parameter (N) \n "; cin >> N; cout << 2*N << " space steps \n" ; cout << " Result being written to fdoutput.txt \n " << endl; ExplicitDiffusion(N,M); cout << "Hit any key+ to finish \n "; cin >> name; return(0); }