| 1 | module maths |
|---|
| 2 | !----------------------------------------------------------------------- |
|---|
| 3 | ! NAME |
|---|
| 4 | ! maths |
|---|
| 5 | ! |
|---|
| 6 | ! DESCRIPTION |
|---|
| 7 | ! The module contains all the mathematical subroutines used in the PEM. |
|---|
| 8 | ! |
|---|
| 9 | ! AUTHORS & DATE |
|---|
| 10 | ! L. Lange |
|---|
| 11 | ! JB Clement, 2023-2025 |
|---|
| 12 | ! |
|---|
| 13 | ! NOTES |
|---|
| 14 | ! Adapted from Schorghofer MSIM (N.S, Icarus 2010) |
|---|
| 15 | !----------------------------------------------------------------------- |
|---|
| 16 | |
|---|
| 17 | ! DEPENDENCIES |
|---|
| 18 | ! ------------ |
|---|
| 19 | use numerics, only: dp, di, k4, minieps |
|---|
| 20 | |
|---|
| 21 | ! DECLARATION |
|---|
| 22 | ! ----------- |
|---|
| 23 | implicit none |
|---|
| 24 | |
|---|
| 25 | ! PARAMETERS |
|---|
| 26 | ! ---------- |
|---|
| 27 | real(dp), parameter :: pi = 4._dp*atan(1._dp) ! PI = 3.14159... |
|---|
| 28 | integer(di), parameter :: limiter_minmod = 1 |
|---|
| 29 | integer(di), parameter :: limiter_MC = 2 |
|---|
| 30 | integer(di), parameter :: limiter_vanLeer = 3 |
|---|
| 31 | |
|---|
| 32 | contains |
|---|
| 33 | !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|---|
| 34 | |
|---|
| 35 | !======================================================================= |
|---|
| 36 | SUBROUTINE deriv1(z,nz,y,y0,ybot,dzY) |
|---|
| 37 | !----------------------------------------------------------------------- |
|---|
| 38 | ! NAME |
|---|
| 39 | ! deriv1 |
|---|
| 40 | ! |
|---|
| 41 | ! DESCRIPTION |
|---|
| 42 | ! Compute the first derivative of a function y(z) on an irregular grid. |
|---|
| 43 | ! |
|---|
| 44 | ! AUTHORS & DATE |
|---|
| 45 | ! N.S (Icarus 2010) |
|---|
| 46 | ! L. Lange |
|---|
| 47 | ! JB Clement, 2023-2025 |
|---|
| 48 | ! |
|---|
| 49 | ! NOTES |
|---|
| 50 | ! Upper boundary conditions: y(0) = y0 |
|---|
| 51 | ! Lower boundary condition: yp = ybottom |
|---|
| 52 | !----------------------------------------------------------------------- |
|---|
| 53 | |
|---|
| 54 | ! DECLARATION |
|---|
| 55 | ! ----------- |
|---|
| 56 | implicit none |
|---|
| 57 | |
|---|
| 58 | ! ARGUMENTS |
|---|
| 59 | ! --------- |
|---|
| 60 | integer(di), intent(in) :: nz ! number of layer |
|---|
| 61 | real(dp), dimension(nz), intent(in) :: z ! depth layer |
|---|
| 62 | real(dp), dimension(nz), intent(in) :: y ! function which needs to be derived |
|---|
| 63 | real(dp), intent(in) :: y0, ybot ! boundary conditions |
|---|
| 64 | real(dp), dimension(nz), intent(out) :: dzY ! derivative of y w.r.t depth |
|---|
| 65 | |
|---|
| 66 | ! LOCAL VARIABLES |
|---|
| 67 | ! --------------- |
|---|
| 68 | integer(di) :: j |
|---|
| 69 | real(dp) :: hm, hp, c1, c2, c3 |
|---|
| 70 | |
|---|
| 71 | ! CODE |
|---|
| 72 | ! ---- |
|---|
| 73 | hp = z(2) - z(1) |
|---|
| 74 | c1 = z(1)/(hp*z(2)) |
|---|
| 75 | c2 = 1/z(1) - 1/(z(2) - z(1)) |
|---|
| 76 | c3 = -hp/(z(1)*z(2)) |
|---|
| 77 | dzY(1) = c1*y(2) + c2*y(1) + c3*y0 |
|---|
| 78 | do j = 2,nz - 1 |
|---|
| 79 | hp = z(j + 1) - z(j) |
|---|
| 80 | hm = z(j) - z(j - 1) |
|---|
| 81 | c1 = +hm/(hp*(z(j + 1) - z(j - 1))) |
|---|
| 82 | c2 = 1._dp/hm - 1._dp/hp |
|---|
| 83 | c3 = -hp/(hm*(z(j + 1) - z(j - 1))) |
|---|
| 84 | dzY(j) = c1*y(j + 1) + c2*y(j) + c3*y(j - 1) |
|---|
| 85 | end do |
|---|
| 86 | dzY(nz) = (ybot - y(nz - 1))/(2._dp*(z(nz) - z(nz - 1))) |
|---|
| 87 | |
|---|
| 88 | END SUBROUTINE deriv1 |
|---|
| 89 | !======================================================================= |
|---|
| 90 | |
|---|
| 91 | !======================================================================= |
|---|
| 92 | SUBROUTINE deriv2_simple(z,nz,y,y0,yNp1,yp2) |
|---|
| 93 | !----------------------------------------------------------------------- |
|---|
| 94 | ! NAME |
|---|
| 95 | ! deriv2_simple |
|---|
| 96 | ! |
|---|
| 97 | ! DESCRIPTION |
|---|
| 98 | ! Compute the second derivative of a function y(z) on an irregular grid. |
|---|
| 99 | ! |
|---|
| 100 | ! AUTHORS & DATE |
|---|
| 101 | ! N.S (Icarus 2010) |
|---|
| 102 | ! JB Clement, 2023-2025 |
|---|
| 103 | ! |
|---|
| 104 | ! NOTES |
|---|
| 105 | ! Boundary conditions: y(0) = y0, y(nz + 1) = yNp1 |
|---|
| 106 | !----------------------------------------------------------------------- |
|---|
| 107 | |
|---|
| 108 | ! DECLARATION |
|---|
| 109 | ! ----------- |
|---|
| 110 | implicit none |
|---|
| 111 | |
|---|
| 112 | ! ARGUMENTS |
|---|
| 113 | ! --------- |
|---|
| 114 | integer(di), intent(in) :: nz |
|---|
| 115 | real(dp), dimension(nz), intent(in) :: z, y |
|---|
| 116 | real(dp), intent(in) :: y0, yNp1 |
|---|
| 117 | real(dp), dimension(nz), intent(out) :: yp2 |
|---|
| 118 | |
|---|
| 119 | ! LOCAL VARIABLES |
|---|
| 120 | ! --------------- |
|---|
| 121 | integer(di) :: j |
|---|
| 122 | real(dp) :: hm, hp, c1, c2, c3 |
|---|
| 123 | |
|---|
| 124 | ! CODE |
|---|
| 125 | ! ---- |
|---|
| 126 | c1 = +2._dp/((z(2) - z(1))*z(2)) |
|---|
| 127 | c2 = -2._dp/((z(2) - z(1))*z(1)) |
|---|
| 128 | c3 = +2._dp/(z(1)*z(2)) |
|---|
| 129 | yp2(1) = c1*y(2) + c2*y(1) + c3*y0 |
|---|
| 130 | do j = 2,nz - 1 |
|---|
| 131 | hp = z(j + 1) - z(j) |
|---|
| 132 | hm = z(j) - z(j - 1) |
|---|
| 133 | c1 = +2._dp/(hp*(z(j + 1) - z(j - 1))) |
|---|
| 134 | c2 = -2._dp/(hp*hm) |
|---|
| 135 | c3 = +2._dp/(hm*(z(j + 1) - z(j - 1))) |
|---|
| 136 | yp2(j) = c1*y(j + 1) + c2*y(j) + c3*y(j - 1) |
|---|
| 137 | end do |
|---|
| 138 | yp2(nz) = (yNp1 - 2._dp*y(nz) + y(nz - 1))/(z(nz) - z(nz - 1))**2 |
|---|
| 139 | |
|---|
| 140 | END SUBROUTINE deriv2_simple |
|---|
| 141 | !======================================================================= |
|---|
| 142 | |
|---|
| 143 | !======================================================================= |
|---|
| 144 | SUBROUTINE deriv1_onesided(j,z,nz,y,dy_zj) |
|---|
| 145 | !----------------------------------------------------------------------- |
|---|
| 146 | ! NAME |
|---|
| 147 | ! deriv1_onesided |
|---|
| 148 | ! |
|---|
| 149 | ! DESCRIPTION |
|---|
| 150 | ! First derivative of function y(z) at z(j) one-sided derivative |
|---|
| 151 | ! on irregular grid. |
|---|
| 152 | ! |
|---|
| 153 | ! AUTHORS & DATE |
|---|
| 154 | ! N.S (Icarus 2010) |
|---|
| 155 | ! JB Clement, 2023-2025 |
|---|
| 156 | ! |
|---|
| 157 | ! NOTES |
|---|
| 158 | ! |
|---|
| 159 | !----------------------------------------------------------------------- |
|---|
| 160 | |
|---|
| 161 | ! DECLARATION |
|---|
| 162 | ! ----------- |
|---|
| 163 | implicit none |
|---|
| 164 | |
|---|
| 165 | ! ARGUMENTS |
|---|
| 166 | ! --------- |
|---|
| 167 | integer(di), intent(in) :: nz, j |
|---|
| 168 | real(dp), dimension(nz), intent(in) :: z, y |
|---|
| 169 | real(dp), intent(out) :: dy_zj |
|---|
| 170 | |
|---|
| 171 | ! LOCAL VARIABLES |
|---|
| 172 | ! --------------- |
|---|
| 173 | real(dp) :: h1, h2, c1, c2, c3 |
|---|
| 174 | |
|---|
| 175 | ! CODE |
|---|
| 176 | ! ---- |
|---|
| 177 | if (j < 1 .or. j > nz - 2) then |
|---|
| 178 | dy_zj = -1._dp |
|---|
| 179 | else |
|---|
| 180 | h1 = z(j + 1) - z(j) |
|---|
| 181 | h2 = z(j + 2)- z(j + 1) |
|---|
| 182 | c1 = -(2._dp*h1 + h2)/(h1*(h1 + h2)) |
|---|
| 183 | c2 = (h1 + h2)/(h1*h2) |
|---|
| 184 | c3 = -h1/(h2*(h1 + h2)) |
|---|
| 185 | dy_zj = c1*y(j) + c2*y(j + 1) + c3*y(j + 2) |
|---|
| 186 | end if |
|---|
| 187 | |
|---|
| 188 | END SUBROUTINE deriv1_onesided |
|---|
| 189 | !======================================================================= |
|---|
| 190 | |
|---|
| 191 | !======================================================================= |
|---|
| 192 | PURE SUBROUTINE colint(y,z,nz,i1,i2,integral) |
|---|
| 193 | !----------------------------------------------------------------------- |
|---|
| 194 | ! NAME |
|---|
| 195 | ! colint |
|---|
| 196 | ! |
|---|
| 197 | ! DESCRIPTION |
|---|
| 198 | ! Column integrates y on irregular grid. |
|---|
| 199 | ! |
|---|
| 200 | ! AUTHORS & DATE |
|---|
| 201 | ! N.S (Icarus 2010) |
|---|
| 202 | ! JB Clement, 2023-2025 |
|---|
| 203 | ! |
|---|
| 204 | ! NOTES |
|---|
| 205 | ! |
|---|
| 206 | !----------------------------------------------------------------------- |
|---|
| 207 | |
|---|
| 208 | ! DECLARATION |
|---|
| 209 | ! ----------- |
|---|
| 210 | implicit none |
|---|
| 211 | |
|---|
| 212 | ! ARGUMENTS |
|---|
| 213 | ! --------- |
|---|
| 214 | integer(di), intent(in) :: nz, i1, i2 |
|---|
| 215 | real(dp), dimension(nz), intent(in) :: y, z |
|---|
| 216 | real(dp), intent(out) :: integral |
|---|
| 217 | |
|---|
| 218 | ! LOCAL VARIABLES |
|---|
| 219 | ! --------------- |
|---|
| 220 | integer(di) :: i |
|---|
| 221 | real(dp), dimension(nz) :: dz |
|---|
| 222 | |
|---|
| 223 | ! CODE |
|---|
| 224 | ! ---- |
|---|
| 225 | dz(1) = (z(2) - 0.)/2 |
|---|
| 226 | do i = 2,nz - 1 |
|---|
| 227 | dz(i) = (z(i + 1) - z(i - 1))/2. |
|---|
| 228 | end do |
|---|
| 229 | dz(nz) = z(nz) - z(nz - 1) |
|---|
| 230 | integral = sum(y(i1:i2)*dz(i1:i2)) |
|---|
| 231 | |
|---|
| 232 | END SUBROUTINE colint |
|---|
| 233 | !======================================================================= |
|---|
| 234 | |
|---|
| 235 | !======================================================================= |
|---|
| 236 | SUBROUTINE findroot(y1,y2,z1,z2,zr) |
|---|
| 237 | !----------------------------------------------------------------------- |
|---|
| 238 | ! NAME |
|---|
| 239 | ! findroot |
|---|
| 240 | ! |
|---|
| 241 | ! DESCRIPTION |
|---|
| 242 | ! Compute the root zr, between two values y1 and y2 at depth z1,z2. |
|---|
| 243 | ! |
|---|
| 244 | ! AUTHORS & DATE |
|---|
| 245 | ! L. Lange |
|---|
| 246 | ! JB Clement, 2023-2025 |
|---|
| 247 | ! |
|---|
| 248 | ! NOTES |
|---|
| 249 | ! |
|---|
| 250 | !----------------------------------------------------------------------- |
|---|
| 251 | |
|---|
| 252 | ! DECLARATION |
|---|
| 253 | ! ----------- |
|---|
| 254 | implicit none |
|---|
| 255 | |
|---|
| 256 | ! ARGUMENTS |
|---|
| 257 | ! --------- |
|---|
| 258 | real(dp), intent(in) :: y1, y2 |
|---|
| 259 | real(dp), intent(in) :: z1, z2 |
|---|
| 260 | real(dp), intent(out) :: zr |
|---|
| 261 | |
|---|
| 262 | ! CODE |
|---|
| 263 | ! ---- |
|---|
| 264 | zr = (y1*z2 - y2*z1)/(y1 - y2) |
|---|
| 265 | |
|---|
| 266 | END SUBROUTINE findroot |
|---|
| 267 | !======================================================================= |
|---|
| 268 | |
|---|
| 269 | !======================================================================= |
|---|
| 270 | SUBROUTINE solve_tridiag(a,b,c,d,n,x,error) |
|---|
| 271 | !----------------------------------------------------------------------- |
|---|
| 272 | ! NAME |
|---|
| 273 | ! solve_tridiag |
|---|
| 274 | ! |
|---|
| 275 | ! DESCRIPTION |
|---|
| 276 | ! Solve a tridiagonal system Ax = d using the Thomas' algorithm |
|---|
| 277 | ! a: sub-diagonal |
|---|
| 278 | ! b: main diagonal |
|---|
| 279 | ! c: super-diagonal |
|---|
| 280 | ! d: right-hand side |
|---|
| 281 | ! x: solution |
|---|
| 282 | ! |
|---|
| 283 | ! AUTHORS & DATE |
|---|
| 284 | ! JB Clement, 2025 |
|---|
| 285 | ! |
|---|
| 286 | ! NOTES |
|---|
| 287 | ! |
|---|
| 288 | !----------------------------------------------------------------------- |
|---|
| 289 | |
|---|
| 290 | ! DECLARATION |
|---|
| 291 | ! ----------- |
|---|
| 292 | implicit none |
|---|
| 293 | |
|---|
| 294 | ! ARGUMENTS |
|---|
| 295 | ! --------- |
|---|
| 296 | integer(di), intent(in) :: n |
|---|
| 297 | real(dp), dimension(n), intent(in) :: b, d |
|---|
| 298 | real(dp), dimension(n - 1), intent(in) :: a, c |
|---|
| 299 | real(dp), dimension(n), intent(out) :: x |
|---|
| 300 | integer(di), intent(out) :: error |
|---|
| 301 | |
|---|
| 302 | ! LOCAL VARIABLES |
|---|
| 303 | ! --------------- |
|---|
| 304 | integer(di) :: i |
|---|
| 305 | real(dp) :: m |
|---|
| 306 | real(dp), dimension(n) :: cp, dp |
|---|
| 307 | |
|---|
| 308 | ! CODE |
|---|
| 309 | ! ---- |
|---|
| 310 | ! Check stability: diagonally dominant condition |
|---|
| 311 | error = 0 |
|---|
| 312 | if (abs(b(1)) < abs(c(1))) then |
|---|
| 313 | error = 1 |
|---|
| 314 | return |
|---|
| 315 | end if |
|---|
| 316 | do i = 2,n - 1 |
|---|
| 317 | if (abs(b(i)) < abs(a(i - 1)) + abs(c(i))) then |
|---|
| 318 | error = 1 |
|---|
| 319 | return |
|---|
| 320 | end if |
|---|
| 321 | end do |
|---|
| 322 | if (abs(b(n)) < abs(a(n - 1))) then |
|---|
| 323 | error = 1 |
|---|
| 324 | return |
|---|
| 325 | end if |
|---|
| 326 | |
|---|
| 327 | ! Initialization |
|---|
| 328 | cp(1) = c(1)/b(1) |
|---|
| 329 | dp(1) = d(1)/b(1) |
|---|
| 330 | |
|---|
| 331 | ! Forward sweep |
|---|
| 332 | do i = 2,n - 1 |
|---|
| 333 | m = b(i) - a(i - 1)*cp(i - 1) |
|---|
| 334 | cp(i) = c(i)/m |
|---|
| 335 | dp(i) = (d(i) - a(i - 1)*dp(i - 1))/m |
|---|
| 336 | end do |
|---|
| 337 | m = b(n) - a(n - 1)*cp(n - 1) |
|---|
| 338 | dp(n) = (d(n) - a(n - 1)*dp(n - 1))/m |
|---|
| 339 | |
|---|
| 340 | ! Backward substitution |
|---|
| 341 | x(n) = dp(n) |
|---|
| 342 | do i = n - 1,1,-1 |
|---|
| 343 | x(i) = dp(i) - cp(i)*x(i + 1) |
|---|
| 344 | end do |
|---|
| 345 | |
|---|
| 346 | END SUBROUTINE solve_tridiag |
|---|
| 347 | !======================================================================= |
|---|
| 348 | |
|---|
| 349 | !======================================================================= |
|---|
| 350 | SUBROUTINE solve_steady_heat(n,z,mz,kappa,mkappa,T_left,q_right,T) |
|---|
| 351 | !----------------------------------------------------------------------- |
|---|
| 352 | ! NAME |
|---|
| 353 | ! solve_steady_heat |
|---|
| 354 | ! |
|---|
| 355 | ! DESCRIPTION |
|---|
| 356 | ! Solve 1D steady-state heat equation with space-dependent thermal diffusivity. |
|---|
| 357 | ! Uses Thomas algorithm to solve tridiagonal system. |
|---|
| 358 | ! Left boundary: prescribed temperature (Dirichlet). |
|---|
| 359 | ! Right boundary: prescribed thermal flux (Neumann). |
|---|
| 360 | ! |
|---|
| 361 | ! AUTHORS & DATE |
|---|
| 362 | ! JB Clement, 2025 |
|---|
| 363 | ! |
|---|
| 364 | ! NOTES |
|---|
| 365 | ! Grid points at z, mid-grid points at mz. |
|---|
| 366 | ! Thermal diffusivity specified at both grids (kappa, mkappa). |
|---|
| 367 | !----------------------------------------------------------------------- |
|---|
| 368 | |
|---|
| 369 | ! DEPENDENCIES |
|---|
| 370 | ! ------------ |
|---|
| 371 | use stoppage, only: stop_clean |
|---|
| 372 | |
|---|
| 373 | ! DECLARATION |
|---|
| 374 | ! ----------- |
|---|
| 375 | implicit none |
|---|
| 376 | |
|---|
| 377 | ! ARGUMENTS |
|---|
| 378 | ! --------- |
|---|
| 379 | integer(di), intent(in) :: n |
|---|
| 380 | real(dp), dimension(n), intent(in) :: z, kappa |
|---|
| 381 | real(dp), dimension(n - 1), intent(in) :: mz, mkappa |
|---|
| 382 | real(dp), intent(in) :: T_left, q_right |
|---|
| 383 | real(dp), dimension(n), intent(out) :: T |
|---|
| 384 | |
|---|
| 385 | ! LOCAL VARIABLES |
|---|
| 386 | ! --------------- |
|---|
| 387 | integer(di) :: i, error |
|---|
| 388 | real(dp), dimension(n) :: b, d |
|---|
| 389 | real(dp), dimension(n - 1) :: a, c |
|---|
| 390 | |
|---|
| 391 | ! CODE |
|---|
| 392 | ! ---- |
|---|
| 393 | ! Initialization |
|---|
| 394 | a = 0._dp; b = 0._dp; c = 0._dp; d = 0._dp |
|---|
| 395 | |
|---|
| 396 | ! Left boundary condition (Dirichlet: prescribed temperature) |
|---|
| 397 | b(1) = 1._dp |
|---|
| 398 | d(1) = T_left |
|---|
| 399 | |
|---|
| 400 | ! Internal points |
|---|
| 401 | do i = 2,n - 1 |
|---|
| 402 | a(i - 1) = -mkappa(i - 1)/((mz(i) - mz(i - 1))*(z(i) - z(i - 1))) |
|---|
| 403 | c(i) = -mkappa(i)/((mz(i) - mz(i - 1))*(z(i + 1) - z(i))) |
|---|
| 404 | b(i) = -(a(i - 1) + c(i)) |
|---|
| 405 | end do |
|---|
| 406 | |
|---|
| 407 | ! Right boundary condition (Neumann: prescribed temperature) |
|---|
| 408 | a(n - 1) = kappa(n - 1)/(z(n) - z(n - 1)) |
|---|
| 409 | b(n) = -kappa(n)/(z(n) - z(n - 1)) |
|---|
| 410 | d(n) = q_right |
|---|
| 411 | |
|---|
| 412 | ! Solve the tridiagonal linear system with the Thomas' algorithm |
|---|
| 413 | call solve_tridiag(a,b,c,d,n,T,error) |
|---|
| 414 | if (error /= 0) call stop_clean(__FILE__,__LINE__,"unstable solving!",1) |
|---|
| 415 | |
|---|
| 416 | END SUBROUTINE solve_steady_heat |
|---|
| 417 | !======================================================================= |
|---|
| 418 | |
|---|
| 419 | end module maths |
|---|