00001 module myFunctions
00002
00003 implicit none
00004
00005
00006
00007 contains
00008
00009
00010
00011
00012 function distance(p1,p2)
00013 implicit none
00014
00015 real distance
00016
00017 real(8) p1(:),p2(:)
00018
00019
00020 distance=dsqrt((p1(1)-p2(1))**2.d0+(p1(2)-p2(2))**2.d0+(p1(3)-p2(3))**2.d0)
00021
00022
00023
00024
00025 end function distance
00026
00027
00028
00029
00030 function get_ptr_lb(array,lb1) result(ptr)
00031
00032 integer,intent(in):: lb1
00033
00034 integer,dimension(lb1:),intent(in),target:: array
00035
00036 integer,dimension(:),pointer:: ptr
00037 ptr => array
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 end function
00056
00057
00058
00059
00060
00061 function getElementNodes(myNx,myNy,myNz) result(info)
00062
00063 implicit none
00064
00065 integer myNx,myNy,myNz
00066
00067 integer info(2)
00068
00069 integer numberOfElementalNodes
00070
00071 integer axis
00072
00073 select case(myNz)
00074
00075 case(1)
00076
00077 select case(myNy)
00078
00079 case(1)
00080
00081 select case (myNx)
00082
00083 case(1)
00084
00085 print*,'no grid point !'
00086
00087 print*,'demona stop'
00088
00089 stop
00090
00091 axis=0
00092
00093 case default
00094
00095 numberOfElementalNodes=2
00096 axis=1
00097
00098 end select
00099
00100
00101 case default
00102
00103 numberOfElementalNodes=4
00104 axis=2
00105
00106 end select
00107
00108
00109 case default
00110
00111 numberOfElementalNodes=8
00112
00113 axis=3
00114
00115
00116 end select
00117
00118
00119 info(1)=numberOfElementalNodes
00120 info(2)=axis
00121
00122
00123
00124
00125
00126 end function getElementNodes
00127
00128
00129
00130
00131
00132
00133
00134 FUNCTION reallocate_vector(p, n) ! reallocate REAL
00135 REAL(8), POINTER, DIMENSION(:) :: p, reallocate_vector
00136 INTEGER, intent(in) :: n
00137 INTEGER :: nold, ierr
00138
00139 ALLOCATE(reallocate_vector(1:n), STAT=ierr)
00140
00141 REALLOCATE_vector=0.d0
00142
00143 IF(ierr /= 0) STOP "allocate error"
00144 IF(.NOT. ASSOCIATED(p)) RETURN
00145
00146 nold = MIN(SIZE(p), n)
00147 reallocate_vector(1:nold) = p(1:nold)
00148
00149 DEALLOCATE(p)
00150
00151 END FUNCTION REALLOCATE_vector
00152
00153 FUNCTION reallocate_vector_integer(p, n) ! reallocate REAL
00154 INTEGER, POINTER, DIMENSION(:) :: p, reallocate_vector_integer
00155 INTEGER, intent(in) :: n
00156 INTEGER :: nold, ierr
00157
00158 ALLOCATE(reallocate_vector_integer(1:n), STAT=ierr)
00159
00160 reallocate_vector_integer=0
00161
00162 IF(ierr /= 0) STOP "allocate error"
00163 IF(.NOT. ASSOCIATED(p)) RETURN
00164
00165 nold = MIN(SIZE(p), n)
00166 reallocate_vector_integer(1:nold) = p(1:nold)
00167
00168 DEALLOCATE(p)
00169
00170 END FUNCTION REALLOCATE_vector_integer
00171
00172 FUNCTION reallocate_matrix(p,n,m) ! reallocate REAL
00173 REAL(8), POINTER, DIMENSION(:,:) :: p, reallocate_matrix
00174 INTEGER, intent(in) :: n,m
00175 INTEGER :: nold,mold,ierr
00176
00177 ALLOCATE(reallocate_matrix(n,m), STAT=ierr)
00178
00179 reallocate_matrix=0.d0
00180
00181 IF(ierr /= 0) STOP "allocate error"
00182 IF(.NOT. ASSOCIATED(p)) RETURN
00183
00184 nold = SIZE(p,DIM=1)
00185 mold = SIZE(p,DIM=2)
00186
00187 reallocate_matrix(1:nold,1:mold) = p(1:nold,1:mold)
00188
00189
00190 DEALLOCATE(p)
00191
00192 END FUNCTION REALLOCATE_matrix
00193
00194 FUNCTION reallocate_matrix_integer(p,n,m) ! reallocate REAL
00195 INTEGER, POINTER, DIMENSION(:,:) :: p, reallocate_matrix_integer
00196 INTEGER, intent(in) :: n,m
00197 INTEGER :: nold,mold,ierr
00198
00199 ALLOCATE(reallocate_matrix_integer(n,m), STAT=ierr)
00200
00201 reallocate_matrix_integer=0
00202
00203 IF(ierr /= 0) STOP "allocate error"
00204 IF(.NOT. ASSOCIATED(p)) RETURN
00205
00206 nold = SIZE(p,DIM=1)
00207 mold = SIZE(p,DIM=2)
00208
00209 reallocate_matrix_integer(1:nold,1:mold) = p(1:nold,1:mold)
00210
00211
00212 DEALLOCATE(p)
00213
00214 END FUNCTION REALLOCATE_matrix_integer
00215
00216 function multiplier(R,sint,cost,j)
00217 implicit none
00218 integer i,j
00219 real(8) sint,cost
00220 real(8),dimension(:,:) ::R
00221 real(8) multiplier(size(R,DIM=1),size(R,DIM=2))
00222 j=size(R,DIM=2)
00223 multiplier=R
00224 multiplier(j,j)=cost*R(j,j)+sint*R(j+1,j)
00225 multiplier(j+1,j)=-sint*R(j,j)+cost*R(j+1,j)
00226 end function multiplier
00227
00228 function previous(H,sint,cost)
00229 implicit none
00230 integer i,j
00231 real(8),dimension(:) :: sint,cost
00232 real(8),dimension(:) ::H
00233 real(8) :: previous(size(H))
00234 real(8) aux
00235 j=size(sint)
00236 previous=H
00237
00238
00239 do i=1,j-1
00240 aux=previous(i)
00241 previous(i)=cost(i)*previous(i)+sint(i)*previous(i+1)
00242 previous(i+1)=-sint(i)*aux+cost(i)*previous(i+1)
00243 end do
00244 end function previous
00245
00246 function backwardsub(U,b)
00247 implicit none
00248 real(8) U(:,:),b(:)
00249 real(8) backwardsub(size(b))
00250 real(8) c
00251 integer i,j,n
00252 backwardsub=0.d0
00253 n=size(b)
00254 backwardsub(n)=b(n)/U(n,n)
00255 do i=n-1,1,-1
00256 c=0.d0
00257 do j=i+1,n
00258 c=backwardsub(j)*U(i,j)+c
00259 end do
00260 backwardsub(i)=(b(i)-c)/U(i,i)
00261 end do
00262 end function backwardsub
00263
00264 function scaler(a,b)
00265 implicit none
00266 integer i,n
00267 real(8) a(:),b(:)
00268 real(8) scaler(size(a))
00269 n=size(a)
00270 scaler=0.d0
00271
00272 do i=1,n
00273 scaler(i)=a(i)*b(i)
00274 end do
00275
00276
00277 end function scaler
00278
00279
00280
00281 function invscaler(a,b)
00282 implicit none
00283 integer i,n
00284 real(8) a(:),b(:)
00285 real(8) invscaler(size(a))
00286 n=size(a)
00287 invscaler=0.d0
00288
00289 do i=1,n
00290 invscaler(i)=a(i)/b(i)
00291 end do
00292
00293
00294 end function invscaler
00295
00296
00297
00298 function signum(x)
00299 implicit none
00300 real(8) signum,eps,x
00301
00302
00303 eps=epsilon(0.d0)
00304
00305 signum=x/dabs(x+eps)
00306
00307
00308 end function signum
00309
00310 function wNorm(myVec,myWVec)
00311
00312 implicit none
00313
00314 real(8) myVec(:),myWVec(:)
00315
00316 real(8) wNorm
00317
00318 wNorm=0.d0
00319
00320 myVec=invscaler(myVec,myWvec)
00321
00322 wNorm=dsqrt(dot_product(myVec,myVec))
00323
00324 end function wNorm
00325
00326
00327
00328 subroutine fileNamer(phyNum,domNum,file,timeNum)
00329
00330
00331
00332 implicit none
00333 character(len=1) :: phy
00334 character(len=1) :: dom
00335 character(len=36) :: file
00336 character(len=4) :: ext
00337 character(len=2) :: phyStr
00338 character(len=2) :: domStr
00339 character(len=15) :: timeStr
00340 character(len=8) :: folderStr
00341
00342
00343
00344 integer phyNum
00345 integer domNum
00346 integer timeNum
00347
00348 WRITE(phyStr,'(I2)') phyNum
00349 WRITE(domStr,'(I2)') domNum
00350 WRITE(timeStr,'(I15)') timeNum
00351
00352 folderStr='results/'
00353 phy='P'
00354 dom='D'
00355 ext='.dat'
00356 file=folderStr//phy//phyStr//' - '//dom//domStr//timeStr//ext
00357
00358 end subroutine fileNamer
00359
00360
00361
00362
00363 function cubic(df0,f0,fprev1,fprev2,lambdaprev1,lambdaprev2) !<cubic backtracking
00364 implicit none
00365 real(8) df0,f0,fprev1,fprev2,lambdaprev1,lambdaprev2
00366 real(8) cubic,lambda
00367 real(8) constant,BS(2,2),C(2,1),D(2,1)
00368 real(8) a,b
00369 cubic=0.d0
00370 lambda=0.d0
00371 constant=1.d0/(lambdaprev1-lambdaprev2)
00372 BS(1,1)= 1.d0/lambdaprev1**2.d0
00373 BS(1,2)=-1.d0/lambdaprev2**2.d0
00374 BS(2,1)=-lambdaprev2/lambdaprev1**2.d0
00375 BS(2,2)= lambdaprev1/lambdaprev2**2.d0
00376 C(1,1)=fprev1-f0-df0*lambdaprev1
00377 C(2,1)=fprev2-f0-df0*lambdaprev2
00378 D=constant*matmul(BS,C)
00379 a=D(1,1)
00380 b=D(2,1)
00381 lambda=(-b+dsqrt(b**2.d0-3.d0*a*df0))/(3.d0*a)
00382 cubic=lambda
00383 end function
00384
00385 function machineps()
00386
00387 implicit none
00388 real(8) machineps
00389 machineps=1.d0
00390 do
00391 machineps=machineps/2.d0
00392 if ((1.d0+machineps).EQ.1.d0) exit
00393 end do
00394 machineps=machineps*2.d0
00395 end function machineps
00396
00397 function quadratic(df0,f0,f1) !<quadratic backtracking
00398 implicit none
00399 real(8) df0,f0,f1
00400 real(8) quadratic,lambda
00401 quadratic=0.d0
00402 lambda=0.d0
00403 lambda=-0.5d0*df0/(f1-f0-df0)
00404 quadratic=lambda
00405 end function
00406
00407 function ser(normOld,normNew,oldDt,serpower)
00408 implicit none
00409
00410 real(8) ser
00411 real(8) serTest
00412 real(8) serpower
00413 real(8) normOld,normNew,oldDt
00414
00415
00416
00417 ser=oldDt
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 serTest=((normOld/normNew)**serpower)*oldDt
00428
00429
00430
00431
00432 serTest=min(serTest,2.d0*oldDt)
00433 serTest=max(serTest,0.1d0*OldDt)
00434
00435
00436
00437
00438
00439 ser=serTest
00440
00441
00442
00443
00444
00445 end function ser
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 end module myFunctions