program jam1
use physics
implicit none
integer :: i, j, n
integer, parameter ::
yd=50, xd=50, zd=50
real, parameter :: qe=1.602e-19
real :: xi, yi,
zi, q=1.0e10*qe
real :: dx=2, dy=2, dz=2
character*40 :: prompt
= 'enter xi, yi, and zi'
real, dimension(xd) ::
x
real, dimension(yd) ::
y
real, dimension(zd) ::
z
real, dimension(xd,yd,zd)
:: v
print *, prompt
read (*,*) xi, yi, zi
do i=1,xd
x(i)=dx*i
end do
do j=1,yd
y(j)=dy*j
end do
do n=1,zd
z(n)=dz*n
end do
call potential(v, xi,
yi, zi, x, y, z, q)
do n=1, zd
do j=1,
yd
write (7,*) v(:,j,n)
end
do
end do
end program jam1
module physics
! this module contains physics subroutines
type vec2d
real :: x, y
end type vec2d
type vec3d
real :: x, y, z
end type vec3d
! global constants for subroutines
real :: k=8.99E9, rad=1
interface potential
module procedure potential2D,
potential3D
end interface
interface electricfield
module procedure electricfield2D,
electricfield3D
end interface
interface add
module procedure addvec2d,
addvec3d
end interface
contains
subroutine addvec2d(a, b)
! this subroutine adds two 2d vectors
! input: a,b
! output: a
type (vec2d), dimension(:,:) :: a, b
a%x=a%x+b%x
a%y=a%y+b%y
end subroutine addvec2d
subroutine addvec3d(a, b)
! this subroutine adds two 3d vectors
! input: a,b
! output: a
type (vec3d), dimension(:,:,:) :: a,b
a%x=a%x+b%x
a%y=a%y+b%y
a%z=a%z+b%z
end subroutine addvec3d
subroutine electricfield2D(e,point0,x,y,q)
! this subroutine calculates 2D electric field of point charge
! input variables are: point0,q,x,y
! output variables are: e
! point0%x and point0%y are locations of point charge
! q is the charge in coulombs
! x and y are locations in space where electric field is calculated
! e%x and e%y are the components of the electric field
real :: v, r
real :: q
type (vec2d) :: point0
real, dimension(:) :: x, y
type (vec2d), dimension(:,:) :: e
do i=1, size(x)
do j=1, size(y)
r=sqrt((x(i)-point0%x)**2+(y(j)-point0%y)**2)
If (r<rad) r=rad
v=(k*q)/r**2
e(i,j)%x=v*(x(i)-point0%x)/r
e(i,j)%y=v*(y(j)-point0%y)/r
end do
end do
end subroutine electricfield2D
subroutine electricfield3D(e,point0,x,y,z,q)
! this subroutine calculates 3D electric field of point charge
! input variables are: point0,q,x,y,z
! output variables are: e
! Point0%x, point0%y, and point0%z are locations of point charge,
! q is the charge in coulombs
! x, y, and z are locations in space where electric field is calculated
! e%x, e%y, and e%z are the components of the electric field
real :: v, r
real :: q
type (vec3d) :: point0
real, dimension(:) :: x, y, z
type (vec3d), dimension(:,:,:) :: e
do n=1, size(z)
do j=1, size(y)
do i=1, size(x)
r=sqrt((x(i)-point0%x)**2+(y(j)-point0%y)**2+(z(n)-point0%z)**2)
If(r<rad) r=rad
v=(k*q)/r**2
e(i,j,n)%x=v*(x(i)-point0%x)/r
e(i,j,n)%y=v*(y(j)-point0%y)/r
e(i,j,n)%z=v*(z(n)-point0%z)/r
end do
end do
end do
end subroutine electricfield3D
subroutine potential2D(pot,point0,x,y,q)
! this subroutine calculates 2D electrical potential of point charge
! input variables are: point0,q,x,y
! output variables are: pot
! point0%x and point0%y are locations of point charge,
! q is the charge in coulombs
! x and y are locations in space where potential is calculated
! pot is the electrical potential
real :: r
real :: q
type (vec2d) :: point0
real, dimension(:) :: x, y
real, dimension(:,:) :: pot
do i=1, size(x)
do j=1, size(y)
r=sqrt((x(i)-point0%x)**2+(y(j)-point0%y)**2)
If (r<rad) r=rad
pot(i,j)=(k*q)/r
end do
end do
end subroutine potential2D
subroutine potential3D(pot,point0,x,y,z,q)
! this subroutine calculates 3D electrical potential of point charge
! input variables are: point0,q,x,y,z
! output variables are: pot
! point0%x, point0%y, and point0%z are locations of point charge,
! q is the charge in coulombs
! x, y, and z are locations in space where potential is calculated
! pot is the electrical potential
real :: r
real :: q
type (vec3d) :: point0
real, dimension(:) :: x, y, z
real, dimension(:,:,:) :: pot
do n=1, size(z)
do j=1, size(y)
do i=1, size(x)
r=sqrt((x(i)-point0%x)**2+(y(j)-point0%y)**2+(z(n)-point0%z)**2)
If (r<rad) r=rad
pot(i,j,n)=(k*q)/r
end do
end do
end do
end subroutine potential3D
end module physics
Back to Main Page |