File: vector_mod.f90

package info (click to toggle)
mocassin 2.02.73.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 42,116 kB
  • sloc: f90: 18,400; makefile: 75
file content (318 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download | duplicates (3)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
! Copyright (C) 2005 Barbara Ercolano
!
! Version 2.02
module vector_mod


  use constants_mod

  implicit none

  public

  ! The definition of the vector type

  type vector
     real :: x
     real :: y
     real :: z
  end type vector

  ! Define multiply

  interface operator(*)
     module procedure mult
  end interface

  ! and divide

  interface operator(/)
     module procedure divideVec
  end interface

  ! add

  interface operator(+)
     module procedure add
  end interface

  ! subtract

  interface operator(-)
     module procedure subtract
  end interface

  ! equivalence

  interface operator(==)
      module procedure equivalence
  end interface

  ! notEquivalence(/=)

  interface operator(/=)
      module procedure notEquivalence
  end interface

  ! note that the following two operators are costumised
  ! for use in the mocassinPlot routines, these are only
  ! meaningful in the context of this program

  ! >=

  interface operator(>=)
     module procedure greaterEqual
  end interface

  ! <=

  interface operator(<=)
     module procedure lessEqual
  end interface

  ! dot product

  interface operator(.dot.)
     module procedure dotProd
  end interface

  ! cross product

  interface operator(.cross.)
     module procedure crossProd
  end interface

contains

  ! the dot product function

  real function dotProd(a , b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    dotProd = a%x*b%x + a%y*b%y + a%z*b%z

  end function dotProd

  ! the cross product function

  type(vector) function crossProd(a ,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    crossProd%x =  (a%y*b%z - a%z*b%y)
    crossProd%y = -(a%x*b%z - a%z*b%x)
    crossProd%z =  (a%x*b%y - a%y*b%x)
  end function crossProd

  ! normalization subroutine - checks for zero vector

  subroutine normalize(a)
    type(vector) :: a
    real :: m

    m = modulus(a)

    if (m == 0.) then
       write(*,'(a)') "! Attempt to normalize the zero vector"
       STOP
    endif

    a%x = a%x / m
    a%y = a%y / m
    a%z = a%z / m

  end subroutine normalize

  ! find the modulus of a vector

  real function modulus(a)
    type(vector) :: a

    modulus = a%x*a%x + a%y*a%y + a%z*a%z
    modulus = sqrt(modulus)

  end function modulus

  ! multiply function

  type(vector) function mult(a,b)
    real, intent(in) :: a
    type(vector), intent(in) :: b

    mult%x = a * b%x
    mult%y = a * b%y
    mult%z = a * b%z

  end function mult


  ! divide vector by a scalar

  type(vector) function divideVec(a,b)
    type(vector), intent(in) :: a
    real, intent(in) :: b

    divideVec%x = a%x / b
    divideVec%y = a%y / b
    divideVec%z = a%z / b

  end function divideVec

  ! add two vectors

  type(vector) function add(a,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    add%x = a%x + b%x
    add%y = a%y + b%y
    add%z = a%z + b%z

  end function add

  ! subtract two vectors

  type(vector) function subtract(a,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    subtract%x = a%x - b%x
    subtract%y = a%y - b%y
    subtract%z = a%z - b%z

  end function subtract

  logical function equivalence(a,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    if( (a%x == b%x) .and.&
         & (a%y == b%y) .and.&
         & (a%z == b%z) ) then
      equivalence = .true.
    else
      equivalence = .false.
    end if
  end function equivalence

  logical function greaterEqual(a,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    if( (a%z <= b%z) .and.&
         & ( a%x/(sqrt(1.-a%z*a%z)) >= &
         & b%x/(sqrt(1.-b%z*b%z))) ) then
      greaterEqual = .true.
    else
      greaterEqual = .false.
    end if
  end function greaterEqual

  logical function lessEqual(a,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    if( (a%z >= b%z) .and.&
         & ( a%x/(sqrt(1.-a%z*a%z)) <= &
         & b%x/(sqrt(1.-b%z*b%z))) ) then
      lessEqual = .true.
    else
      lessEqual = .false.
    end if
  end function lessEqual


  logical function notEquivalence(a,b)
    type(vector), intent(in) :: a
    type(vector), intent(in) :: b

    if( (a%x == b%x) .and.&
         & (a%y == b%y) .and.&
         & (a%z == b%z) ) then
      notEquivalence = .false.
    else
      notEquivalence = .true.
    end if
  end function notEquivalence


  ! get polar form of a cartesian vector

  subroutine getPolar(vec, r, theta, phi)

    implicit none
    type(vector) :: vec
    real :: r, theta, phi, cosTheta

    r = modulus(vec)
    if ((vec%y == 0.) .and. (vec%x == 0)) then
       phi = 0.
    else
       phi = atan2(vec%y, vec%x)
    endif
    if (phi < 0.) phi = phi + twoPi
    cosTheta = vec%z/r
    theta = acos(cosTheta)
  end subroutine getPolar

  ! rotate a vector "a" about the z-axis by angle b

  type(vector) function rotateZ(a,b)
    type(vector), intent(in) :: a
    real, intent(in) :: b   ! angle in radians
    real :: cosb, sinb

    cosb = cos(b)
    sinb = sin(b)

    rotateZ%x = cosb * a%x + sinb * a%y
    rotateZ%y =-sinb * a%x + cosb * a%y
    rotateZ%z = a%z

  end function rotateZ

  type(vector) function rotateX(a,b)
    type(vector), intent(in) :: a
    real, intent(in) :: b   ! angle in radians
    real :: cosb, sinb

    cosb = cos(b)
    sinb = sin(b)

    rotateX%x = a%x
    rotateX%y = cosb * a%y + sinb * a%z
    rotateX%z =-sinb * a%y + cosb * a%z

  end function rotateX

  type(vector) function rotateY(a,b)
    type(vector), intent(in) :: a
    real, intent(in) :: b   ! angle in radians
    real :: cosb, sinb

    cosb = cos(b)
    sinb = sin(b)

    rotateY%x = cosb * a%x + sinb * a%z
    rotateY%y = a%y
    rotateY%z =-sinb * a%x + cosb * a%z

  end function rotateY

    type(vector) function randomUnitVector()
    real :: r1, r2, u, v, w, t, ang
    call random_number(r1)
    w = 2.*r1 - 1.
    t = sqrt(1.-w*w)
    call random_number(r2)
    ang = 3.141592654*(2.*r2-1.)
    u = t*cos(ang)
    v = t*sin(ang)

    randomUnitVector = vector(u,v,w)
  end function randomUnitVector


end module vector_mod