-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_analysis_sm.f90
110 lines (95 loc) · 2.92 KB
/
vector_analysis_sm.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
! Implementation code for vector_analysis module
submodule (vector_analysis) vector_analysis_sm
contains
! Calculate the gradient
module function grad(p, phi) result(r)
real(8) :: r(3)
real(8), intent(in) :: p(3)
procedure(f) :: phi
type(auto_var) :: q(3), s
integer :: i
do i=1,3
call make_var(p, q, i)
s = phi(q)
r(i) = s%get_derivative()
end do
end function grad
! Calculate the divergence
module function div(p, phi)
real(8) :: div
real(8), intent(in) :: p(3)
procedure(f) :: phi
real(8) :: q(3)
q = grad(p, phi)
div = sum(q)
end function div
!! Calculate the del_squared (== div(grad))
!module function del_squared(x, y, phi)
! real(8) :: del_squared
! real(8), intent(in) :: x, y
! procedure(f) :: phi
! real(8) :: grad(2)
! type(auto_var) :: xa, ya, ra
!
! ! x-component varies, y-component constant
! call xa%set(x)
! call ya%set_constant(y)
! ra = phi(xa, ya)
! grad(1) = ra%get_derivative()
!
! ! y-component varies, x-component constant
! call xa%set_constant(x)
! call ya%set(y)
! ra = phi(xa, ya)
! grad(2) = ra%get_derivative()
!
! del_squared = sum(grad**2)
!end function del_squared
! This is actually the z-component of curl
!module function curl(x, y, phi)
! real(8) :: curl
! real(8), intent(in) :: x, y
! procedure(f) :: phi
! real(8) :: grad(2)
! type(auto_var) :: xa, ya, ra
!
! ! x-component varies, y-component constant
! call xa%set(x)
! call ya%set_constant(y)
! ra = phi(xa, ya)
! grad(1) = ra%get_derivative()
!
! ! y-component varies, x-component constant
! call xa%set_constant(x)
! call ya%set(y)
! ra = phi(xa, ya)
! grad(2) = ra%get_derivative()
!
! curl = grad(1) - grad(2)
!end function curl
! Evaluate the given function
module function evaluate(p, phi)
real(8) :: evaluate
real(8), intent(in) :: p(3)
procedure(f) :: phi
type(auto_var) :: q(3), ra
integer :: i
! Make parameters constant - no derivatives
do i=1,3
call q(i)%set_constant(p(i))
end do
ra = phi(q)
evaluate = ra%get_value()
end function evaluate
! Cross product of two vectors
module pure function cross(a, b) result(r)
real(8), intent(in) :: a(3), b(3)
real(8) :: r(3)
integer :: i, j, k
do i=1,3
j = merge(1,i+1,i==3)
k = merge(1,j+1,j==3)
r(i) = a(j)*b(k)-a(k)*b(j)
end do
end function cross
end submodule vector_analysis_sm