| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2021- Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
module vectorutils |
| 23 |
implicit none |
| 24 |
public :: minmaxave,cross_product3D,curl3D_epsijk,det |
| 25 |
public :: matrixinvert3D,rotatevec |
| 26 |
|
| 27 |
interface cross_product3D |
| 28 |
module procedure cross_product3D_r4, cross_product3D_r8 |
| 29 |
end interface |
| 30 |
|
| 31 |
private |
| 32 |
|
| 33 |
contains |
| 34 |
!------------------------------------------------------------------- |
| 35 |
! simple routine to take min, max and average of a quantity |
| 36 |
!------------------------------------------------------------------- |
| 37 |
subroutine minmaxave(x,xmin,xmax,xav,npts) |
| 38 |
integer :: i |
| 39 |
integer, intent(in) :: npts |
| 40 |
real, intent(in) :: x(npts) |
| 41 |
real, intent(out) :: xmin,xmax,xav |
| 42 |
|
| 43 |
xav = 0. |
| 44 |
xmin = huge(xmin) |
| 45 |
xmax = -xmin |
| 46 |
do i=1,npts |
| 47 |
xav = xav + x(i) |
| 48 |
xmin = min(xmin,x(i)) |
| 49 |
xmax = max(xmax,x(i)) |
| 50 |
enddo |
| 51 |
xav = xav/real(npts) |
| 52 |
|
| 53 |
return |
| 54 |
end subroutine minmaxave |
| 55 |
|
| 56 |
pure subroutine cross_product3D_r4(veca,vecb,vecc) |
| 57 |
real(4), intent(in) :: veca(3),vecb(3) |
| 58 |
real(4), intent(out) :: vecc(3) |
| 59 |
|
| 60 |
vecc(1) = veca(2)*vecb(3) - veca(3)*vecb(2) |
| 61 |
vecc(2) = veca(3)*vecb(1) - veca(1)*vecb(3) |
| 62 |
vecc(3) = veca(1)*vecb(2) - veca(2)*vecb(1) |
| 63 |
|
| 64 |
end subroutine cross_product3D_r4 |
| 65 |
|
| 66 |
pure subroutine cross_product3D_r8(veca,vecb,vecc) |
| 67 |
real(8), intent(in) :: veca(3),vecb(3) |
| 68 |
real(8), intent(out) :: vecc(3) |
| 69 |
|
| 70 |
vecc(1) = veca(2)*vecb(3) - veca(3)*vecb(2) |
| 71 |
vecc(2) = veca(3)*vecb(1) - veca(1)*vecb(3) |
| 72 |
vecc(3) = veca(1)*vecb(2) - veca(2)*vecb(1) |
| 73 |
|
| 74 |
end subroutine cross_product3D_r8 |
| 75 |
|
| 76 |
pure subroutine curl3D_epsijk(gradAvec,curlA) |
| 77 |
real, intent(in) :: gradAvec(3,3) |
| 78 |
real, intent(out) :: curlA(3) |
| 79 |
|
| 80 |
curlA(1) = gradAvec(2,3) - gradAvec(3,2) |
| 81 |
curlA(2) = gradAvec(3,1) - gradAvec(1,3) |
| 82 |
curlA(3) = gradAvec(1,2) - gradAvec(2,1) |
| 83 |
|
| 84 |
end subroutine curl3D_epsijk |
| 85 |
|
| 86 |
!---------------------------------------------------------------- |
| 87 |
!+ |
| 88 |
! Internal subroutine that inverts a 3x3 matrix |
| 89 |
!+ |
| 90 |
!---------------------------------------------------------------- |
| 91 |
pure subroutine matrixinvert3D(A,Ainv,ierr) |
| 92 |
real, intent(in) :: A(3,3) |
| 93 |
real, intent(out) :: Ainv(3,3) |
| 94 |
integer, intent(out) :: ierr |
| 95 |
real :: x0(3),x1(3),x2(3),result(3) |
| 96 |
real :: det, ddet |
| 97 |
|
| 98 |
ierr = 0 |
| 99 |
|
| 100 |
x0 = A(1,:) |
| 101 |
x1 = A(2,:) |
| 102 |
x2 = A(3,:) |
| 103 |
|
| 104 |
call cross_product3D(x1,x2,result) |
| 105 |
det = dot_product(x0,result) |
| 106 |
|
| 107 |
if (abs(det) > tiny(det)) then |
| 108 |
ddet = 1./det |
| 109 |
else |
| 110 |
ddet = 0. |
| 111 |
Ainv = 0. |
| 112 |
ierr = 1 |
| 113 |
return |
| 114 |
endif |
| 115 |
|
| 116 |
Ainv(:,1) = result(:)*ddet |
| 117 |
call cross_product3D(x2,x0,result) |
| 118 |
Ainv(:,2) = result(:)*ddet |
| 119 |
call cross_product3D(x0,x1,result) |
| 120 |
Ainv(:,3) = result(:)*ddet |
| 121 |
|
| 122 |
return |
| 123 |
end subroutine matrixinvert3D |
| 124 |
|
| 125 |
real function det(A) |
| 126 |
real, intent(in) :: A(3,3) |
| 127 |
real :: x0(3),x1(3),x2(3),result(3) |
| 128 |
|
| 129 |
x0 = A(1,:) |
| 130 |
x1 = A(2,:) |
| 131 |
x2 = A(3,:) |
| 132 |
|
| 133 |
call cross_product3D(x1,x2,result) |
| 134 |
det = dot_product(x0,result) |
| 135 |
|
| 136 |
return |
| 137 |
end function det |
| 138 |
|
| 139 |
!------------------------------------------------------------------------ |
| 140 |
! |
| 141 |
! rotate a vector (u) around an axis defined by another vector (v) |
| 142 |
! by an angle (theta) using the Rodrigues rotation formula |
| 143 |
! |
| 144 |
!------------------------------------------------------------------------ |
| 145 |
pure subroutine rotatevec(u,v,theta) |
| 146 |
real, dimension(3), intent(inout) :: u |
| 147 |
real, dimension(3), intent(in) :: v |
| 148 |
real, intent(in) :: theta |
| 149 |
real, dimension(3) :: k,w |
| 150 |
|
| 151 |
!--normalise v |
| 152 |
k = v/sqrt(dot_product(v,v)) |
| 153 |
|
| 154 |
!--Rodrigues rotation formula |
| 155 |
call cross_product3D(k,u,w) |
| 156 |
u = u*cos(theta) + w*sin(theta) + k*dot_product(k,u)*(1-cos(theta)) |
| 157 |
|
| 158 |
end subroutine rotatevec |
| 159 |
|
| 160 |
end module vectorutils |