You are on page 1of 4

program to generate an NaCl crystal in fortran

program main

use lattice implicit none integer,allocatable :: latarray(:,:,:) integer :: x,y,z,first

print*,'Enter the dimensions separated by space:' read*,x,y,z call coordgen(x,y,z,latarray) print*,'Enter the charge at the origin:' read*,first call assignlat(latarray,first) call printlat(latarray) end program

module lattice contains subroutine coordgen(sizex,sizey,sizez,latarray) implicit none integer,intent(in) :: sizez,sizey,sizex integer :: ok

integer,dimension(:,:,:),allocatable :: latarray

allocate(latarray(1:sizex,1:sizey,1:sizez),STAT=ok) if (ok/=0) then stop "***Cannot allocate memory***" end if

latarray(:,:,:)=0 end subroutine

subroutine assignlat(lattice,first) implicit none integer,intent(inout),dimension(:,:,:) :: lattice integer,intent(in) :: first integer,allocatable,dimension(:) :: row integer,allocatable,dimension(:,:) :: plane,plntemp integer :: ok,index,charge,length,width

print*,'Enter the assignlat' allocate(row(size(lattice, dim=1)),STAT=ok) if (ok/=0) stop "***Cannot allocate memory***" allocate(plane(1:size(lattice, dim=1),1:size(lattice, dim=2)),STAT=ok) if (ok/=0) stop "***Cannot allocate memory***"

charge=first

do index=1,size(lattice, dim=1) lattice(1,index,1)=charge charge=charge*(-1) end do

row(:)=lattice(1,:,1) length=size(row, dim=1) do index=2,size(lattice, dim=1) row(:)=-1*row(:) lattice(index,:,1)=row(:) end do

plane(:,:)=lattice(:,:,1) length=size(lattice, dim=1) width=size(lattice, dim=2) do index=2,size(lattice,dim=3) plane(:,:)=-1*plane(:,:) lattice(:,:,index)=plane(:,:) end do print*,'Exit the assignlat' end subroutine

subroutine printlat(lattice) implicit none integer,intent(inout),dimension(:,:,:) :: lattice

integer :: index1,index2,index3,count=0

open(1,FILE='E:\fortran\course\lab3\question2\output.txt',ACCESS='APPEND',STATUS='NEW')

write(1,*) 'X

charge'

do index1=1,size(lattice,dim=1) do index2=1,size(lattice,dim=2) do index3=1,size(lattice,dim=3) write(1,*) index1,index2,index3,lattice(index1,index2,index3) count=count+1 end do end do end do Write(1,*) 'The total number of points:',count close(1) end subroutine end module

You might also like