Changes between Initial Version and Version 1 of cypress/Programming/HeatMassTransfer/ex3.f90


Ignore:
Timestamp:
May 14, 2015 3:12:10 PM (7 years ago)
Author:
cmaggio
Comment:

migrated content from ccs wiki

Legend:

Unmodified
Added
Removed
Modified
  • cypress/Programming/HeatMassTransfer/ex3.f90

    v1 v1  
     1= Ex3.f90 =
     2{{{#!fortran
     3!
     4!  Large Scale Computing
     5!  Heat/Mass Transfer
     6!  ex3.f90
     7!
     8program diffusion1d
     9  implicit none
     10  double precision :: a,b,d
     11  double precision,dimension(:),allocatable :: phi,phi0
     12  double precision,dimension(:),allocatable :: rhs
     13  double precision :: dx
     14  double precision :: err,merr
     15  double precision,parameter :: tol = 1.0e-8
     16  integer :: i,num,itc
     17 
     18  character(len=32) :: arg
     19 
     20  ! Newer version of Fortran can get commadline arguments
     21  do i=1,4
     22     call get_command_argument(i,arg)
     23     if (len_trim(arg) == 0) then
     24        call get_command_argument(0,arg)
     25        print *,arg," [num] [A] [B] [D]"
     26        call exit(-1)
     27     end if
     28 
     29     ! get inputed depth
     30     if (i == 1) read(arg,*) num
     31     if (i == 2) read(arg,*) a
     32     if (i == 3) read(arg,*) b
     33     if (i == 4) read(arg,*) d
     34  end do
     35 
     36  print *,"num=",num,"A=",a,"B=",b,"D=",d
     37 
     38  ! Memory Allocation
     39  allocate(phi(num))
     40  allocate(phi0(num))
     41  allocate(rhs(num))
     42 
     43  ! Setup
     44  dx = 1.d0 / dble(num - 1)
     45  do i = 1,num
     46     rhs(i) = -dx * dx / d * (dx * dble(i))
     47     phi(i) = 0.d0
     48     phi0(i) = 0.d0
     49  end do
     50 
     51  ! Boundary Condition
     52  phi(1) = a
     53  phi0(1) = a
     54  phi(num) = b
     55  phi0(num) = b
     56 
     57  ! Solve with Jacobi Method
     58  itc = 0;
     59  do
     60     ! update phi
     61     do i = 2,num-1
     62        phi(i) = -0.5 * (rhs(i) - phi0(i + 1) - phi0(i - 1))
     63     end do
     64 
     65    ! Check Convergence
     66     merr = 0.d0
     67     do i = 1, num
     68        err = abs(phi(i) - phi0(i))
     69        if (merr < err) then
     70           merr = err
     71        end if
     72        phi0(i) = phi(i)
     73     end do
     74 
     75     itc = itc + 1
     76     if (merr < tol) exit
     77     if (mod(itc,10000) == 0) print *,"itc=",itc,"err=",merr
     78  end do
     79 
     80  print *,"Number of Iteration=",itc
     81 
     82  ! Output Result
     83  open(10,FILE='res.dat')
     84  do i = 1,num
     85     write(10,'(2e16.8)') dx * dble(i-1),phi(i)
     86  end do
     87  close(10)
     88 
     89  ! END : Deallocate Memory
     90  deallocate(phi)
     91  deallocate(phi0)
     92  deallocate(rhs)
     93  print *,"Done"
     94end program diffusion1d
     95}}}