/*To solve heat diffusion equation using FVM. AS described in Versteeg H and !Malalasekra !To set constant temperature boundary condition on both ends*/ #include #include #include PetscErrorCode bc_const_temp_both_sides(Mat A, Vec b, PetscReal temp_l, PetscReal temp_r, PetscReal k, PetscReal area, PetscReal dx, PetscInt Nx, PetscReal q); static char help[] = "Solves the 1D heat diffusion equation.\n \ Usage: ./heat_diff_cu -Nx 1000\n"; int main(int argc, char **argv){ PetscInt Nx, Istart, Iend, col_idx[3], rank, num_procs; PetscReal dx, xl, xr, start_time, anal_term1, anal_term2, start_tot_time; std::vector grid; //PetscScalar, pointer :: soln(:); PetscScalar k, ap, ae, aw, su, sp, area, ka_dx, Tlb, Trb, val[3], zero, q; Vec x, b, u, vout; Mat A; KSP ksp; PC pc ; PetscViewer viewer ; VecScatter ctx; PetscBool flg ; const PetscScalar *soln; PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); //PetscCall(InitializeOptions(&user)); //start_tot_time = MPI_Wtime(); //Default simulation parameters (source term zero) xl=0.0; xr=0.5; Nx=100; //total number of grid points k=1000.0; //thermal conductivity (W/(m.k)) area=0.01; //(m^2) Tlb = 100.0; //temperature on the left boundary Tlb.....Trb (k) Trb = 500.0; //temperature on the right boundary (k) q=0.0; //heat flux (W/m^3) // Default simulation parameters (with source term) // xl=0.0 // xr=0.02 // Nx=5 !total number of grid points // k=0.5 !thermal conductivity (W/(m.k)) // area=1.0 !(m^2) // Tlb = 100.0 !temperature on the left boundary Tlb.....Trb (k) // Trb = 200.0 !temperature on the right boundary (k) // q=1.0e6 ! heat flux (W/m^3) //get command line inputs PetscCall(PetscOptionsGetInt(NULL, NULL, "-Nx", &Nx, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-xl", &xl, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-xr", &xr, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-k", &k, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-area", &area, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-Tl", &Tlb, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-Tr", &Trb, &flg)); PetscCall(PetscOptionsGetReal(NULL, NULL, "-q", &q, &flg)); MPI_Comm_size(PETSC_COMM_WORLD, &num_procs); MPI_Comm_rank(PETSC_COMM_WORLD, &rank); /*std::cout << "Size and ranks are: " << num_procs << "\t" << rank << std::endl;*/ dx = (xr-xl)/(Nx-1); for(int i=0; i0 && i