| 1 |
c* /////////////////////////////////////////////////////////////////////////// |
| 2 |
c* @file mikpckd.f |
| 3 |
c* @author Michael Holst |
| 4 |
c* @brief A collection of useful low-level routines (timing, etc). |
| 5 |
c* @version $Id: mikpckd.f,v 1.2 2010/08/12 05:45:34 fetk Exp $ |
| 6 |
c* @attention |
| 7 |
c* @verbatim |
| 8 |
c* |
| 9 |
c* PMG -- Parallel algebraic MultiGrid |
| 10 |
c* Copyright (C) 1994-- Michael Holst. |
| 11 |
c* |
| 12 |
c* Michael Holst <[email protected]> |
| 13 |
c* University of California, San Diego |
| 14 |
c* Department of Mathematics, 5739 AP&M |
| 15 |
c* 9500 Gilman Drive, Dept. 0112 |
| 16 |
c* La Jolla, CA 92093-0112 USA |
| 17 |
c* http://math.ucsd.edu/~mholst |
| 18 |
c* |
| 19 |
c* This file is part of PMG. |
| 20 |
c* |
| 21 |
c* PMG is free software; you can redistribute it and/or modify |
| 22 |
c* it under the terms of the GNU General Public License as published by |
| 23 |
c* the Free Software Foundation; either version 2 of the License, or |
| 24 |
c* (at your option) any later version. |
| 25 |
c* |
| 26 |
c* PMG is distributed in the hope that it will be useful, |
| 27 |
c* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 |
c* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 |
c* GNU General Public License for more details. |
| 30 |
c* |
| 31 |
c* You should have received a copy of the GNU General Public License |
| 32 |
c* along with PMG; if not, write to the Free Software |
| 33 |
c* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 34 |
c* |
| 35 |
c* Linking PMG statically or dynamically with other modules is making a |
| 36 |
c* combined work based on PMG. Thus, the terms and conditions of the GNU |
| 37 |
c* General Public License cover the whole combination. |
| 38 |
c* |
| 39 |
c* SPECIAL GPL EXCEPTION |
| 40 |
c* In addition, as a special exception, the copyright holders of PMG |
| 41 |
c* give you permission to combine the PMG program with free software |
| 42 |
c* programs and libraries that are released under the GNU LGPL or with |
| 43 |
c* code included in releases of ISIM, PMV, PyMOL, SMOL, VMD, and Vision. |
| 44 |
c* Such combined software may be linked with PMG and redistributed together |
| 45 |
c* in original or modified form as mere aggregation without requirement that |
| 46 |
c* the entire work be under the scope of the GNU General Public License. |
| 47 |
c* This special exception permission is also extended to any software listed |
| 48 |
c* in the SPECIAL GPL EXCEPTION clauses by the FEtk and APBS libraries. |
| 49 |
c* |
| 50 |
c* Note that people who make modified versions of PMG are not obligated |
| 51 |
c* to grant this special exception for their modified versions; it is |
| 52 |
c* their choice whether to do so. The GNU General Public License gives |
| 53 |
c* permission to release a modified version without this exception; this |
| 54 |
c* exception also makes it possible to release a modified version which |
| 55 |
c* carries forward this exception. |
| 56 |
c* |
| 57 |
c* @endverbatim |
| 58 |
c* /////////////////////////////////////////////////////////////////////////// |
| 59 |
|
| 60 |
function epsmac (idum) |
| 61 |
c* ********************************************************************* |
| 62 |
c* purpose: |
| 63 |
c* |
| 64 |
c* this routine computes the unit roundoff of the machine in single |
| 65 |
c* precision. this is defined as the smallest positive machine |
| 66 |
c* number u such that 1.0d0 + u .ne. 1.0d0 (in single precision). |
| 67 |
c* |
| 68 |
c* author: michael holst |
| 69 |
c* ********************************************************************* |
| 70 |
implicit none |
| 71 |
integer idum |
| 72 |
double precision epsmac |
| 73 |
c* ***double precision u, comp |
| 74 |
c* |
| 75 |
c* ****** determine the machine epsilon *** |
| 76 |
c* ***u = 1.0d0 |
| 77 |
c* ***10 continue |
| 78 |
c* *** u = u*0.50d0 |
| 79 |
c* *** comp = 1.0d0 + u |
| 80 |
c* ***if (comp .ne. 1.0d0) goto 10 |
| 81 |
c* ***epsmac = u*2.0d0 |
| 82 |
c* |
| 83 |
c* *** hardcoded *** |
| 84 |
epsmac = 1.0d-9 |
| 85 |
c* |
| 86 |
c* *** return and end *** |
| 87 |
return |
| 88 |
end |
| 89 |
subroutine tstart(before,overhd) |
| 90 |
c* ********************************************************************* |
| 91 |
c* purpose: |
| 92 |
c* |
| 93 |
c* this routine starts the timer on the particular machine. |
| 94 |
c* |
| 95 |
c* the routine uses my own "c" routine timer, which works for most |
| 96 |
c* unix systems, including (atleast): |
| 97 |
c* |
| 98 |
c* convex c240 |
| 99 |
c* sun 4 |
| 100 |
c* sun 3 |
| 101 |
c* alliant fx/80 |
| 102 |
c* |
| 103 |
c* this routine is also correct for the native "tsecnd" (which is |
| 104 |
c* for multitasked programs, rather than "second" for single task |
| 105 |
c* programs) routine on the following machines: |
| 106 |
c* |
| 107 |
c* cray ymp |
| 108 |
c* |
| 109 |
c* author: michael holst |
| 110 |
c* ********************************************************************* |
| 111 |
implicit none |
| 112 |
double precision before,overhd,garbge,t0,t1 |
| 113 |
double precision tsecnd |
| 114 |
c* |
| 115 |
c* *** compute overhead and mark timer *** |
| 116 |
garbge = tsecnd() |
| 117 |
t0 = tsecnd() |
| 118 |
t1 = tsecnd() |
| 119 |
overhd = t1 - t0 |
| 120 |
before = tsecnd() |
| 121 |
c* |
| 122 |
c* *** return and end *** |
| 123 |
return |
| 124 |
end |
| 125 |
subroutine tstop (before,overhd,cputme) |
| 126 |
c* ********************************************************************* |
| 127 |
c* purpose: |
| 128 |
c* |
| 129 |
c* this routine stops the timer on the particular machine. |
| 130 |
c* |
| 131 |
c* the routine uses my own "c" routine timer, which works for most |
| 132 |
c* unix systems, including (atleast): |
| 133 |
c* |
| 134 |
c* convex c240 |
| 135 |
c* sun 4 |
| 136 |
c* sun 3 |
| 137 |
c* alliant fx/80 |
| 138 |
c* |
| 139 |
c* this routine is also correct for the native "tsecnd" (which is |
| 140 |
c* for multitasked programs, rather than "second" for single task |
| 141 |
c* programs) routine on the following machines: |
| 142 |
c* |
| 143 |
c* cray ymp |
| 144 |
c* |
| 145 |
c* author: michael holst |
| 146 |
c* ********************************************************************* |
| 147 |
implicit none |
| 148 |
double precision before,overhd,cputme,after |
| 149 |
double precision tsecnd |
| 150 |
c* |
| 151 |
c* *** stop timer, compute elapsed time, add to time counter *** |
| 152 |
after = tsecnd() |
| 153 |
cputme = (after - before) - overhd |
| 154 |
c* |
| 155 |
c* *** return and end *** |
| 156 |
return |
| 157 |
end |
| 158 |
subroutine xaxpy(nx,ny,nz,alpha,x,y) |
| 159 |
c* ********************************************************************* |
| 160 |
c* purpose: |
| 161 |
c* |
| 162 |
c* saxpy operation for a grid function with boundary values. |
| 163 |
c* |
| 164 |
c* author: michael holst |
| 165 |
c* ********************************************************************* |
| 166 |
implicit none |
| 167 |
integer nx,ny,nz,i,j,k |
| 168 |
double precision x(nx,ny,nz),y(nx,ny,nz),alpha |
| 169 |
c* |
| 170 |
cmdir 0 0 |
| 171 |
c* |
| 172 |
c* *** do it *** |
| 173 |
cmdir 3 1 |
| 174 |
do 10 k = 2, nz-1 |
| 175 |
cmdir 3 2 |
| 176 |
do 11 j = 2, ny-1 |
| 177 |
cmdir 3 3 |
| 178 |
do 12 i = 2, nx-1 |
| 179 |
y(i,j,k) = y(i,j,k) + alpha * x(i,j,k) |
| 180 |
12 continue |
| 181 |
11 continue |
| 182 |
10 continue |
| 183 |
c* |
| 184 |
c* *** return and end *** |
| 185 |
return |
| 186 |
end |
| 187 |
subroutine xcopy(nx,ny,nz,x,y) |
| 188 |
c* ********************************************************************* |
| 189 |
c* purpose: |
| 190 |
c* |
| 191 |
c* copy operation for a grid function with boundary values. |
| 192 |
c* |
| 193 |
c* author: michael holst |
| 194 |
c* ********************************************************************* |
| 195 |
implicit none |
| 196 |
integer nx,ny,nz,i,j,k |
| 197 |
double precision x(nx,ny,nz),y(nx,ny,nz) |
| 198 |
c* |
| 199 |
cmdir 0 0 |
| 200 |
c* |
| 201 |
c* *** do it *** |
| 202 |
cmdir 3 1 |
| 203 |
do 10 k = 2, nz-1 |
| 204 |
cmdir 3 2 |
| 205 |
do 11 j = 2, ny-1 |
| 206 |
cmdir 3 3 |
| 207 |
do 12 i = 2, nx-1 |
| 208 |
y(i,j,k) = x(i,j,k) |
| 209 |
12 continue |
| 210 |
11 continue |
| 211 |
10 continue |
| 212 |
c* |
| 213 |
c* *** return and end *** |
| 214 |
return |
| 215 |
end |
| 216 |
function xnrm1(nx,ny,nz,x) |
| 217 |
c* ********************************************************************* |
| 218 |
c* purpose: |
| 219 |
c* |
| 220 |
c* norm operation for a grid function with boundary values. |
| 221 |
c* |
| 222 |
c* author: michael holst |
| 223 |
c* ********************************************************************* |
| 224 |
implicit none |
| 225 |
integer nx,ny,nz,i,j,k |
| 226 |
double precision x(nx,ny,nz) |
| 227 |
double precision tmp,xnrm1 |
| 228 |
c* |
| 229 |
cmdir 0 0 |
| 230 |
c* |
| 231 |
c* *** initialize *** |
| 232 |
tmp = 0.0d0 |
| 233 |
c* |
| 234 |
c* *** do it *** |
| 235 |
cmdir 3 1 |
| 236 |
do 10 k = 2, nz-1 |
| 237 |
cmdir 3 2 |
| 238 |
do 11 j = 2, ny-1 |
| 239 |
cmdir 3 3 |
| 240 |
do 12 i = 2, nx-1 |
| 241 |
tmp = tmp + dabs(x(i,j,k)) |
| 242 |
12 continue |
| 243 |
11 continue |
| 244 |
10 continue |
| 245 |
c* |
| 246 |
c* *** finish up *** |
| 247 |
xnrm1 = tmp |
| 248 |
c* |
| 249 |
c* *** return and end *** |
| 250 |
return |
| 251 |
end |
| 252 |
function xnrm2(nx,ny,nz,x) |
| 253 |
c* ********************************************************************* |
| 254 |
c* purpose: |
| 255 |
c* |
| 256 |
c* norm operation for a grid function with boundary values. |
| 257 |
c* |
| 258 |
c* author: michael holst |
| 259 |
c* ********************************************************************* |
| 260 |
implicit none |
| 261 |
integer nx,ny,nz,i,j,k |
| 262 |
double precision x(nx,ny,nz) |
| 263 |
double precision tmp,xnrm2 |
| 264 |
c* |
| 265 |
cmdir 0 0 |
| 266 |
c* |
| 267 |
c* *** initialize *** |
| 268 |
tmp = 0.0d0 |
| 269 |
c* |
| 270 |
c* *** do it *** |
| 271 |
cmdir 3 1 |
| 272 |
do 10 k = 2, nz-1 |
| 273 |
cmdir 3 2 |
| 274 |
do 11 j = 2, ny-1 |
| 275 |
cmdir 3 3 |
| 276 |
do 12 i = 2, nx-1 |
| 277 |
tmp = tmp + x(i,j,k)*x(i,j,k) |
| 278 |
12 continue |
| 279 |
11 continue |
| 280 |
10 continue |
| 281 |
c* |
| 282 |
c* *** finish up *** |
| 283 |
xnrm2 = dsqrt(tmp) |
| 284 |
c* |
| 285 |
c* *** return and end *** |
| 286 |
return |
| 287 |
end |
| 288 |
function xnrm8(nx,ny,nz,x) |
| 289 |
c* ********************************************************************* |
| 290 |
c* purpose: |
| 291 |
c* |
| 292 |
c* norm operation for a grid function with boundary values. |
| 293 |
c* |
| 294 |
c* author: michael holst |
| 295 |
c* ********************************************************************* |
| 296 |
implicit none |
| 297 |
integer nx,ny,nz,i,j,k |
| 298 |
double precision x(nx,ny,nz) |
| 299 |
double precision tmp,xnrm8 |
| 300 |
c* |
| 301 |
cmdir 0 0 |
| 302 |
c* |
| 303 |
c* *** initialize *** |
| 304 |
tmp = 0.0d0 |
| 305 |
c* |
| 306 |
c* *** do it *** |
| 307 |
cmdir 3 1 |
| 308 |
do 10 k = 2, nz-1 |
| 309 |
cmdir 3 2 |
| 310 |
do 11 j = 2, ny-1 |
| 311 |
cmdir 3 3 |
| 312 |
do 12 i = 2, nx-1 |
| 313 |
tmp = dmax1( tmp , dabs(x(i,j,k)) ) |
| 314 |
12 continue |
| 315 |
11 continue |
| 316 |
10 continue |
| 317 |
c* |
| 318 |
c* *** finish up *** |
| 319 |
xnrm8 = tmp |
| 320 |
c* |
| 321 |
c* *** return and end *** |
| 322 |
return |
| 323 |
end |
| 324 |
subroutine xscal(nx,ny,nz,fac,x) |
| 325 |
c* ********************************************************************* |
| 326 |
c* purpose: |
| 327 |
c* |
| 328 |
c* scale operation for a grid function with boundary values. |
| 329 |
c* |
| 330 |
c* author: michael holst |
| 331 |
c* ********************************************************************* |
| 332 |
implicit none |
| 333 |
integer nx,ny,nz,i,j,k |
| 334 |
double precision x(nx,ny,nz),fac |
| 335 |
c* |
| 336 |
cmdir 0 0 |
| 337 |
c* |
| 338 |
c* *** do it *** |
| 339 |
cmdir 3 1 |
| 340 |
do 10 k = 2, nz-1 |
| 341 |
cmdir 3 2 |
| 342 |
do 11 j = 2, ny-1 |
| 343 |
cmdir 3 3 |
| 344 |
do 12 i = 2, nx-1 |
| 345 |
x(i,j,k) = fac*x(i,j,k) |
| 346 |
12 continue |
| 347 |
11 continue |
| 348 |
10 continue |
| 349 |
c* |
| 350 |
c* *** return and end *** |
| 351 |
return |
| 352 |
end |
| 353 |
function xdot(nx,ny,nz,x,y) |
| 354 |
c* ********************************************************************* |
| 355 |
c* purpose: |
| 356 |
c* |
| 357 |
c* inner product operation for a grid function with boundary values. |
| 358 |
c* |
| 359 |
c* author: michael holst |
| 360 |
c* ********************************************************************* |
| 361 |
implicit none |
| 362 |
integer nx,ny,nz,i,j,k |
| 363 |
double precision x(nx,ny,nz),y(nx,ny,nz),xdot |
| 364 |
c* |
| 365 |
cmdir 0 0 |
| 366 |
c* |
| 367 |
c* *** initialize *** |
| 368 |
xdot = 0.0d0 |
| 369 |
c* |
| 370 |
c* *** do it *** |
| 371 |
cmdir 3 1 |
| 372 |
do 10 k = 2, nz-1 |
| 373 |
cmdir 3 2 |
| 374 |
do 11 j = 2, ny-1 |
| 375 |
cmdir 3 3 |
| 376 |
do 12 i = 2, nx-1 |
| 377 |
xdot = xdot + x(i,j,k)*y(i,j,k) |
| 378 |
12 continue |
| 379 |
11 continue |
| 380 |
10 continue |
| 381 |
c* |
| 382 |
c* *** return and end *** |
| 383 |
return |
| 384 |
end |
| 385 |
function xdot3(nx,ny,nz,x,y) |
| 386 |
c* ********************************************************************* |
| 387 |
c* purpose: |
| 388 |
c* |
| 389 |
c* necessary mess. |
| 390 |
c* |
| 391 |
c* author: michael holst |
| 392 |
c* ********************************************************************* |
| 393 |
implicit none |
| 394 |
integer nx,ny,nz,i,j,k |
| 395 |
double precision x(nx,ny,nz),y(nx,ny,nz),xdot3 |
| 396 |
c* |
| 397 |
cmdir 0 0 |
| 398 |
c* |
| 399 |
c* *** initialize *** |
| 400 |
xdot3 = 0.0d0 |
| 401 |
c* |
| 402 |
c* *** do it *** |
| 403 |
cmdir 3 1 |
| 404 |
do 10 k = 2, nz-1 |
| 405 |
cmdir 3 2 |
| 406 |
do 11 j = 2, ny-1 |
| 407 |
cmdir 3 3 |
| 408 |
do 12 i = 2, nx-1 |
| 409 |
xdot3 = xdot3 + x(i,j,k) * y(i,j,k) * y(i,j,k) |
| 410 |
12 continue |
| 411 |
11 continue |
| 412 |
10 continue |
| 413 |
c* |
| 414 |
c* *** return and end *** |
| 415 |
return |
| 416 |
end |
| 417 |
subroutine zeros(nx,ny,nz,x) |
| 418 |
c* ********************************************************************* |
| 419 |
c* purpose: |
| 420 |
c* |
| 421 |
c* zero out operation for a grid function with boundary values. |
| 422 |
c* |
| 423 |
c* author: michael holst |
| 424 |
c* ********************************************************************* |
| 425 |
implicit none |
| 426 |
integer nx,ny,nz,i,j,k |
| 427 |
double precision x(nx,ny,nz) |
| 428 |
c* |
| 429 |
cmdir 0 0 |
| 430 |
c* |
| 431 |
c* *** do it *** |
| 432 |
cmdir 3 1 |
| 433 |
do 10 k = 2, nz-1 |
| 434 |
cmdir 3 2 |
| 435 |
do 11 j = 2, ny-1 |
| 436 |
cmdir 3 3 |
| 437 |
do 12 i = 2, nx-1 |
| 438 |
x(i,j,k) = 0.0d0 |
| 439 |
12 continue |
| 440 |
11 continue |
| 441 |
10 continue |
| 442 |
c* |
| 443 |
c* *** return and end *** |
| 444 |
return |
| 445 |
end |
| 446 |
subroutine xrand(nx,ny,nz,x) |
| 447 |
c* ********************************************************************* |
| 448 |
c* purpose: |
| 449 |
c* |
| 450 |
c* fill grid function with random values. |
| 451 |
c* |
| 452 |
c* author: michael holst |
| 453 |
c* ********************************************************************* |
| 454 |
implicit none |
| 455 |
integer nx,ny,nz,i,j,k,iflag |
| 456 |
double precision x(nx,ny,nz) |
| 457 |
double precision xdum |
| 458 |
real rand |
| 459 |
c* |
| 460 |
cmdir 0 0 |
| 461 |
c* |
| 462 |
c* *** do it *** |
| 463 |
iflag = 1 |
| 464 |
xdum = dble(rand(iflag)) |
| 465 |
cmdir 3 1 |
| 466 |
do 10 k = 2, nz-1 |
| 467 |
cmdir 3 2 |
| 468 |
do 11 j = 2, ny-1 |
| 469 |
cmdir 3 3 |
| 470 |
do 12 i = 2, nx-1 |
| 471 |
x(i,j,k) = dble(rand(iflag)) |
| 472 |
12 continue |
| 473 |
11 continue |
| 474 |
10 continue |
| 475 |
c* |
| 476 |
c* *** return and end *** |
| 477 |
return |
| 478 |
end |
| 479 |
subroutine cinit(nx,ny,nz,x,value) |
| 480 |
c* ********************************************************************* |
| 481 |
c* purpose: |
| 482 |
c* |
| 483 |
c* initialize a grid function to a specific constant, without |
| 484 |
c* changing the boundary values. |
| 485 |
c* |
| 486 |
c* author: michael holst |
| 487 |
c* ********************************************************************* |
| 488 |
implicit none |
| 489 |
integer nx,ny,nz,i,j,k |
| 490 |
double precision x(nx,ny,nz),value |
| 491 |
c* |
| 492 |
cmdir 0 0 |
| 493 |
c* |
| 494 |
c* *** do it *** |
| 495 |
cmdir 3 1 |
| 496 |
do 10 k = 2, nz-1 |
| 497 |
cmdir 3 2 |
| 498 |
do 11 j = 2, ny-1 |
| 499 |
cmdir 3 3 |
| 500 |
do 12 i = 2, nx-1 |
| 501 |
x(i,j,k) = value |
| 502 |
12 continue |
| 503 |
11 continue |
| 504 |
10 continue |
| 505 |
c* |
| 506 |
c* *** return and end *** |
| 507 |
return |
| 508 |
end |
| 509 |
subroutine azeros(nx,ny,nz,x) |
| 510 |
c* ********************************************************************* |
| 511 |
c* purpose: |
| 512 |
c* |
| 513 |
c* zero out operation for a grid function, including boundary values. |
| 514 |
c* |
| 515 |
c* author: michael holst |
| 516 |
c* ********************************************************************* |
| 517 |
implicit none |
| 518 |
integer nx,ny,nz |
| 519 |
double precision x(*) |
| 520 |
integer n,i,ii,nproc,ipara,ivect |
| 521 |
parameter (nproc=1) |
| 522 |
c* |
| 523 |
cmdir 0 0 |
| 524 |
c* |
| 525 |
c* *** find parallel loops (ipara), remainder (ivect) *** |
| 526 |
n = nx * ny * nz |
| 527 |
ipara = n / nproc |
| 528 |
ivect = mod(n,nproc) |
| 529 |
c* |
| 530 |
c* *** do parallel loops *** |
| 531 |
cmdir 2 1 |
| 532 |
do 10 ii = 1, nproc |
| 533 |
cmdir 2 2 |
| 534 |
do 11 i = 1+(ipara*(ii-1)), ipara*ii |
| 535 |
x(i) = 0.0d0 |
| 536 |
11 continue |
| 537 |
10 continue |
| 538 |
c* |
| 539 |
c* *** do vector loops *** |
| 540 |
cmdir 1 1 |
| 541 |
do 20 i = ipara*nproc+1, n |
| 542 |
x(i) = 0.0d0 |
| 543 |
20 continue |
| 544 |
c* |
| 545 |
c* *** return and end *** |
| 546 |
return |
| 547 |
end |
| 548 |
subroutine axrand(nx,ny,nz,x) |
| 549 |
c* ********************************************************************* |
| 550 |
c* purpose: |
| 551 |
c* |
| 552 |
c* fill grid function with random values, including boundary values. |
| 553 |
c* |
| 554 |
c* author: michael holst |
| 555 |
c* ********************************************************************* |
| 556 |
implicit none |
| 557 |
integer nx,ny,nz,iflag |
| 558 |
double precision x(*) |
| 559 |
double precision xdum |
| 560 |
integer n,i,ii,nproc,ipara,ivect |
| 561 |
parameter (nproc=1) |
| 562 |
real rand |
| 563 |
c* |
| 564 |
cmdir 0 0 |
| 565 |
c* |
| 566 |
c* *** find parallel loops (ipara), remainder (ivect) *** |
| 567 |
n = nx * ny * nz |
| 568 |
ipara = n / nproc |
| 569 |
ivect = mod(n,nproc) |
| 570 |
iflag = 1 |
| 571 |
xdum = dble(rand(iflag)) |
| 572 |
c* |
| 573 |
c* *** do parallel loops *** |
| 574 |
cmdir 2 1 |
| 575 |
do 10 ii = 1, nproc |
| 576 |
cmdir 2 2 |
| 577 |
do 11 i = 1+(ipara*(ii-1)), ipara*ii |
| 578 |
x(i) = dble(rand(iflag)) |
| 579 |
11 continue |
| 580 |
10 continue |
| 581 |
c* |
| 582 |
c* *** do vector loops *** |
| 583 |
cmdir 1 1 |
| 584 |
do 20 i = ipara*nproc+1, n |
| 585 |
x(i) = dble(rand(iflag)) |
| 586 |
20 continue |
| 587 |
c* |
| 588 |
c* *** return and end *** |
| 589 |
return |
| 590 |
end |
| 591 |
subroutine xcopy_small(nx,ny,nz,x,y) |
| 592 |
c* ********************************************************************* |
| 593 |
c* purpose: |
| 594 |
c* |
| 595 |
c* copy operation for a grid function with boundary values. |
| 596 |
c* |
| 597 |
c* author: michael holst |
| 598 |
c* ********************************************************************* |
| 599 |
implicit none |
| 600 |
integer nx,ny,nz,i,j,k |
| 601 |
double precision x(nx,ny,nz),y(nx-2,ny-2,nz-2) |
| 602 |
c* |
| 603 |
cmdir 0 0 |
| 604 |
c* |
| 605 |
c* *** do it *** |
| 606 |
cmdir 3 1 |
| 607 |
do 10 k = 2, nz-1 |
| 608 |
cmdir 3 2 |
| 609 |
do 11 j = 2, ny-1 |
| 610 |
cmdir 3 3 |
| 611 |
do 12 i = 2, nx-1 |
| 612 |
y(i-1,j-1,k-1) = x(i,j,k) |
| 613 |
12 continue |
| 614 |
11 continue |
| 615 |
10 continue |
| 616 |
c* |
| 617 |
c* *** return and end *** |
| 618 |
return |
| 619 |
end |
| 620 |
subroutine xcopy_large(nx,ny,nz,x,y) |
| 621 |
c* ********************************************************************* |
| 622 |
c* purpose: |
| 623 |
c* |
| 624 |
c* copy operation for a grid function with boundary values. |
| 625 |
c* |
| 626 |
c* author: michael holst |
| 627 |
c* ********************************************************************* |
| 628 |
implicit none |
| 629 |
integer nx,ny,nz,i,j,k |
| 630 |
double precision x(nx-2,ny-2,nz-2),y(nx,ny,nz) |
| 631 |
c* |
| 632 |
cmdir 0 0 |
| 633 |
c* |
| 634 |
c* *** do it *** |
| 635 |
cmdir 3 1 |
| 636 |
do 10 k = 2, nz-1 |
| 637 |
cmdir 3 2 |
| 638 |
do 11 j = 2, ny-1 |
| 639 |
cmdir 3 3 |
| 640 |
do 12 i = 2, nx-1 |
| 641 |
y(i,j,k) = x(i-1,j-1,k-1) |
| 642 |
12 continue |
| 643 |
11 continue |
| 644 |
10 continue |
| 645 |
c* |
| 646 |
c* *** return and end *** |
| 647 |
return |
| 648 |
end |
| 649 |
subroutine fbound(ibound,nx,ny,nz,x,gxc,gyc,gzc) |
| 650 |
c* ********************************************************************* |
| 651 |
c* purpose: |
| 652 |
c* |
| 653 |
c* initialize a grid function to have a certain boundary value, |
| 654 |
c* given in the arrays gxc,gyc,gzc |
| 655 |
c* |
| 656 |
c* author: michael holst |
| 657 |
c* ********************************************************************* |
| 658 |
implicit none |
| 659 |
integer ibound,nx,ny,nz,i,j,k |
| 660 |
double precision x(nx,ny,nz) |
| 661 |
double precision gxc(ny,nz,2),gyc(nx,nz,2),gzc(nx,ny,2) |
| 662 |
c* |
| 663 |
cmdir 0 0 |
| 664 |
c* |
| 665 |
c* *** zero dirichlet? *** |
| 666 |
if (ibound .eq. 0) then |
| 667 |
call fbound00(nx,ny,nz,x) |
| 668 |
c* |
| 669 |
c* *** nonzero dirichlet *** |
| 670 |
else |
| 671 |
c* *** the (i=1) and (i=nx) boundaries *** |
| 672 |
cmdir 2 1 |
| 673 |
do 10 k = 1, nz |
| 674 |
cmdir 2 2 |
| 675 |
do 11 j = 1, ny |
| 676 |
x(1 ,j,k) = gxc(j,k,1) |
| 677 |
x(nx,j,k) = gxc(j,k,2) |
| 678 |
11 continue |
| 679 |
10 continue |
| 680 |
c* |
| 681 |
c* *** the (j=1) and (j=ny) boundaries *** |
| 682 |
cmdir 2 1 |
| 683 |
do 20 k = 1, nz |
| 684 |
cmdir 2 2 |
| 685 |
do 21 i = 1, nx |
| 686 |
x(i,1 ,k) = gyc(i,k,1) |
| 687 |
x(i,ny,k) = gyc(i,k,2) |
| 688 |
21 continue |
| 689 |
20 continue |
| 690 |
c* |
| 691 |
c* *** the (k=1) and (k=nz) boundaries *** |
| 692 |
cmdir 2 1 |
| 693 |
do 30 j = 1, ny |
| 694 |
cmdir 2 2 |
| 695 |
do 31 i = 1, nx |
| 696 |
x(i,j,1 ) = gzc(i,j,1) |
| 697 |
x(i,j,nz) = gzc(i,j,2) |
| 698 |
31 continue |
| 699 |
30 continue |
| 700 |
endif |
| 701 |
c* |
| 702 |
c* *** return and end *** |
| 703 |
return |
| 704 |
end |
| 705 |
subroutine fbound00(nx,ny,nz,x) |
| 706 |
c* ********************************************************************* |
| 707 |
c* purpose: |
| 708 |
c* |
| 709 |
c* initialize a grid function to have a zero boundary value, |
| 710 |
c* given in the arrays gxc,gyc,gzc |
| 711 |
c* |
| 712 |
c* author: michael holst |
| 713 |
c* ********************************************************************* |
| 714 |
implicit none |
| 715 |
integer nx,ny,nz,i,j,k |
| 716 |
double precision x(nx,ny,nz) |
| 717 |
c* |
| 718 |
cmdir 0 0 |
| 719 |
c* |
| 720 |
c* *** the (i=1) and (i=nx) boundaries *** |
| 721 |
cmdir 2 1 |
| 722 |
do 10 k = 1, nz |
| 723 |
cmdir 2 2 |
| 724 |
do 11 j = 1, ny |
| 725 |
x(1 ,j,k) = 0.0d0 |
| 726 |
x(nx,j,k) = 0.0d0 |
| 727 |
11 continue |
| 728 |
10 continue |
| 729 |
c* |
| 730 |
c* *** the (j=1) and (j=ny) boundaries *** |
| 731 |
cmdir 2 1 |
| 732 |
do 20 k = 1, nz |
| 733 |
cmdir 2 2 |
| 734 |
do 21 i = 1, nx |
| 735 |
x(i,1 ,k) = 0.0d0 |
| 736 |
x(i,ny,k) = 0.0d0 |
| 737 |
21 continue |
| 738 |
20 continue |
| 739 |
c* |
| 740 |
c* *** the (k=1) and (k=nz) boundaries *** |
| 741 |
cmdir 2 1 |
| 742 |
do 30 j = 1, ny |
| 743 |
cmdir 2 2 |
| 744 |
do 31 i = 1, nx |
| 745 |
x(i,j,1 ) = 0.0d0 |
| 746 |
x(i,j,nz) = 0.0d0 |
| 747 |
31 continue |
| 748 |
30 continue |
| 749 |
c* |
| 750 |
c* *** return and end *** |
| 751 |
return |
| 752 |
end |
| 753 |
subroutine xprint(nx,ny,nz,x) |
| 754 |
c* ********************************************************************* |
| 755 |
c* purpose: |
| 756 |
c* |
| 757 |
c* this routine prints a grid function. |
| 758 |
c* |
| 759 |
c* author: michael holst |
| 760 |
c* ********************************************************************* |
| 761 |
implicit none |
| 762 |
integer nx,ny,nz,i,j,k |
| 763 |
double precision x(nx,ny,nz) |
| 764 |
character*10 title |
| 765 |
c* |
| 766 |
c* *** doit *** |
| 767 |
print*, title |
| 768 |
do 10 k = 2, nz-1 |
| 769 |
do 11 j = 2, ny-1 |
| 770 |
write(6,100) (x(i,j,k),i=2,nx-1) |
| 771 |
100 format(100(1x,1pe8.1)) |
| 772 |
11 continue |
| 773 |
10 continue |
| 774 |
c* |
| 775 |
c* *** return and end *** |
| 776 |
return |
| 777 |
end |
| 778 |
subroutine prtmatd(nx,ny,nz,ipc,rpc,ac) |
| 779 |
c* ********************************************************************* |
| 780 |
c* purpose: |
| 781 |
c* |
| 782 |
c* interface to routine to print sparse diagonal-format matrix. |
| 783 |
c* |
| 784 |
c* note: |
| 785 |
c* |
| 786 |
c* the matrix is symmetric, therefore we print only the main and |
| 787 |
c* upper diagonals. |
| 788 |
c* |
| 789 |
c* author: michael holst |
| 790 |
c* ********************************************************************* |
| 791 |
implicit none |
| 792 |
integer ipc(*),nx,ny,nz,numdia |
| 793 |
double precision rpc(*),ac(nx*ny*nz,*) |
| 794 |
c* |
| 795 |
c* *** do the printing *** |
| 796 |
numdia = ipc(11) |
| 797 |
if (numdia .eq. 7) then |
| 798 |
call prtmatd7(nx,ny,nz,ipc,rpc, |
| 799 |
2 ac(1,1),ac(1,2),ac(1,3),ac(1,4)) |
| 800 |
elseif (numdia .eq. 27) then |
| 801 |
call prtmatd27(nx,ny,nz,ipc,rpc, |
| 802 |
2 ac(1,1),ac(1,2),ac(1,3),ac(1,4), |
| 803 |
3 ac(1,5),ac(1,6),ac(1,7),ac(1,8),ac(1,9), |
| 804 |
4 ac(1,10),ac(1,11),ac(1,12),ac(1,13),ac(1,14)) |
| 805 |
else |
| 806 |
print*,'% PRTMATD: invalid stencil type given...' |
| 807 |
endif |
| 808 |
c* |
| 809 |
c* *** go home *** |
| 810 |
return |
| 811 |
end |
| 812 |
subroutine prtmatd7(nx,ny,nz,ipc,rpc,oC,oE,oN,uC) |
| 813 |
c* ********************************************************************* |
| 814 |
c* purpose: |
| 815 |
c* |
| 816 |
c* routine to print sparse 7-diagonal-format matrix. |
| 817 |
c* |
| 818 |
c* note: |
| 819 |
c* |
| 820 |
c* the matrix is symmetric, therefore we print only the main and |
| 821 |
c* upper diagonals. |
| 822 |
c* |
| 823 |
c* author: michael holst |
| 824 |
c* ********************************************************************* |
| 825 |
implicit none |
| 826 |
integer ipc(*),nx,ny,nz,n,i,j,k |
| 827 |
double precision rpc(*),oC(nx,ny,nz) |
| 828 |
double precision oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz) |
| 829 |
c* |
| 830 |
c* *** recover matrix dimension *** |
| 831 |
n = (nx-2) * (ny-2) * (nz-2) |
| 832 |
print*,'% ' |
| 833 |
print*,'% dimension of matrix = ',n |
| 834 |
print*,'% *begin diagonal matrix* ' |
| 835 |
c* |
| 836 |
c* *** handle first block *** |
| 837 |
do 10 k = 2, nz-1 |
| 838 |
do 11 j = 2, ny-1 |
| 839 |
do 12 i = 2, nx-1 |
| 840 |
write(*,104) oC(i,j,k),oE(i,j,k),oN(i,j,k),uC(i,j,k) |
| 841 |
12 continue |
| 842 |
11 continue |
| 843 |
10 continue |
| 844 |
c* |
| 845 |
c* *** finish up *** |
| 846 |
print*,'% *end diagonal matrix* ' |
| 847 |
c* |
| 848 |
c* *** format statements *** |
| 849 |
104 format (4(1pe8.1,1x)) |
| 850 |
c* |
| 851 |
c* *** return and end *** |
| 852 |
return |
| 853 |
end |
| 854 |
subroutine prtmatd27(nx,ny,nz,ipc,rpc,oC,oE,oN,uC, |
| 855 |
2 oNE,oNW,uE,uW,uN,uS,uNE,uNW,uSE,uSW) |
| 856 |
c* ********************************************************************* |
| 857 |
c* purpose: |
| 858 |
c* |
| 859 |
c* routine to print sparse 27-diagonal-format matrix. |
| 860 |
c* |
| 861 |
c* note: |
| 862 |
c* |
| 863 |
c* the matrix is symmetric, therefore we print only the main and |
| 864 |
c* upper diagonals. |
| 865 |
c* |
| 866 |
c* author: michael holst |
| 867 |
c* ********************************************************************* |
| 868 |
implicit none |
| 869 |
integer ipc(*),nx,ny,nz,n,i,j,k |
| 870 |
double precision rpc(*),oC(nx,ny,nz) |
| 871 |
double precision oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz) |
| 872 |
double precision oNE(nx,ny,nz),oNW(nx,ny,nz),uE(nx,ny,nz) |
| 873 |
double precision uW(nx,ny,nz),uN(nx,ny,nz),uS(nx,ny,nz) |
| 874 |
double precision uNE(nx,ny,nz),uNW(nx,ny,nz),uSE(nx,ny,nz) |
| 875 |
double precision uSW(nx,ny,nz) |
| 876 |
c* |
| 877 |
c* *** recover matrix dimension *** |
| 878 |
n = (nx-2) * (ny-2) * (nz-2) |
| 879 |
print*,'% ' |
| 880 |
print*,'% dimension of matrix = ',n |
| 881 |
print*,'% *begin diagonal matrix* ' |
| 882 |
c* |
| 883 |
c* *** handle first block *** |
| 884 |
do 10 k = 2, nz-1 |
| 885 |
do 11 j = 2, ny-1 |
| 886 |
do 12 i = 2, nx-1 |
| 887 |
write(*,114) oC(i,j,k),oE(i,j,k), |
| 888 |
2 oNW(i,j,k),oN(i,j,k),oNE(i,j,k), |
| 889 |
3 uSW(i,j,k),uS(i,j,k),uSE(i,j,k), |
| 890 |
4 uW(i,j,k),uC(i,j,k),uE(i,j,k), |
| 891 |
5 uNW(i,j,k),uN(i,j,k),uNE(i,j,k) |
| 892 |
12 continue |
| 893 |
11 continue |
| 894 |
10 continue |
| 895 |
c* |
| 896 |
c* *** finish up *** |
| 897 |
print*,'% *end diagonal matrix* ' |
| 898 |
c* |
| 899 |
c* *** format statements *** |
| 900 |
114 format (14(1pe8.1,1x)) |
| 901 |
c* |
| 902 |
c* *** return and end *** |
| 903 |
return |
| 904 |
end |
| 905 |
subroutine prtmatb(a,n,m,lda) |
| 906 |
c*********************************************************************** |
| 907 |
c* purpose: |
| 908 |
c* |
| 909 |
c* routine to print sparse banded-format matrix. |
| 910 |
c* |
| 911 |
c* note: |
| 912 |
c* |
| 913 |
c* the matrix is symmetric, therefore we print only the main and |
| 914 |
c* upper diagonal bands. |
| 915 |
c* |
| 916 |
c*********************************************************************** |
| 917 |
implicit none |
| 918 |
integer n,m,lda,i,j |
| 919 |
double precision a(lda,*) |
| 920 |
c |
| 921 |
c *** recover matrix dimension *** |
| 922 |
print*,' ' |
| 923 |
print*,' dimension of matrix = ',n |
| 924 |
print*,' *begin banded matrix* ' |
| 925 |
c |
| 926 |
c *** do the printing *** |
| 927 |
do 10 j = 1, n |
| 928 |
write(*,100) (a(i,j),i=lda,1,-1) |
| 929 |
10 continue |
| 930 |
c |
| 931 |
c *** finish up *** |
| 932 |
print*,' *end banded matrix* ' |
| 933 |
c |
| 934 |
c *** format statements *** |
| 935 |
100 format (30(1x,1pe8.1)) |
| 936 |
c |
| 937 |
c *** go home *** |
| 938 |
return |
| 939 |
end |
| 940 |
subroutine linesearch(nx,ny,nz,alpha, |
| 941 |
2 ipc,rpc,ac,cc,fc,p,x,r,ap,zk,zkp1) |
| 942 |
c* ********************************************************************* |
| 943 |
c* purpose: |
| 944 |
c* |
| 945 |
c* line search for a minimizing steplength given a descent direction. |
| 946 |
c* |
| 947 |
c* input: |
| 948 |
c* |
| 949 |
c* nx,ny,nz = mesh dimensions |
| 950 |
c* ipc,rpc = operator/level information |
| 951 |
c* ac,cc,fc = operator/level entries |
| 952 |
c* p = the direction |
| 953 |
c* x = the previous solution |
| 954 |
c* r = the previous nonlinear residual, r=r(x) |
| 955 |
c* |
| 956 |
c* temporary vectors: |
| 957 |
c* |
| 958 |
c* ap,zk,zkp1 = used as temp vectors, passed back as needed for the |
| 959 |
c* nonlinear fletcher-reeves conjugate gradient algorithm |
| 960 |
c* (can be ignored otherwise) |
| 961 |
c* r = is definitely overwritten |
| 962 |
c* |
| 963 |
c* output: |
| 964 |
c* |
| 965 |
c* alpha = steplength for minimizing the 1d problem in direction p. |
| 966 |
c* |
| 967 |
c* author: michael holst |
| 968 |
c* ********************************************************************* |
| 969 |
implicit none |
| 970 |
c* |
| 971 |
c* *** other declarations *** |
| 972 |
integer ipc(*),nx,ny,nz,ipkey |
| 973 |
double precision rpc(*),ac(*),cc(*),fc(*) |
| 974 |
double precision p(*),x(*),r(*),ap(*),zk(*),zkp1(*) |
| 975 |
double precision alpha,xdot |
| 976 |
double precision ALP,pAp,pr,val,xdot3,ALP_old |
| 977 |
double precision zNN,zntol,DzNN |
| 978 |
integer niters,nitmax,ifail_tol |
| 979 |
c* |
| 980 |
c* *** nonlinear iteration tolerance and itmax *** |
| 981 |
nitmax = 20 |
| 982 |
zntol = 1.0e-5 |
| 983 |
ifail_tol = 0 |
| 984 |
ipkey = ipc(10) |
| 985 |
c* |
| 986 |
c* ********************************************************************* |
| 987 |
c* *** begin linear case for choosing alpha |
| 988 |
c* ********************************************************************* |
| 989 |
c* |
| 990 |
c* *** compute the hackbusch/reusken damping parameter *** |
| 991 |
c* *** which is equivalent to the standard linear cg steplength *** |
| 992 |
call matvec(nx,ny,nz,ipc,rpc,ac,cc,p,ap) |
| 993 |
pr = xdot(nx,ny,nz,p,r) |
| 994 |
pAp = xdot(nx,ny,nz,p,ap) |
| 995 |
alpha = pr / pAp |
| 996 |
c* |
| 997 |
c* ********************************************************************* |
| 998 |
c* *** begin nonlinear case for choosing alpha |
| 999 |
c* ********************************************************************* |
| 1000 |
c* |
| 1001 |
c* *** initialize ALP (use linear approx) and zkp1 *** |
| 1002 |
ALP = alpha |
| 1003 |
call xcopy(nx,ny,nz,x,r) |
| 1004 |
call xaxpy(nx,ny,nz,ALP,p,r) |
| 1005 |
call c_vec(cc,x,zk,nx,ny,nz,ipkey) |
| 1006 |
call c_vec(cc,r,zkp1,nx,ny,nz,ipkey) |
| 1007 |
c* |
| 1008 |
c* *** evaluate residual of 1d system |
| 1009 |
call xcopy(nx,ny,nz,zkp1,r) |
| 1010 |
call xaxpy(nx,ny,nz,(-1.0d0),zk,r) |
| 1011 |
val = xdot(nx,ny,nz,p,r) |
| 1012 |
zNN = ALP * pAp - pr + val |
| 1013 |
c* |
| 1014 |
c* *** setup for newton's method |
| 1015 |
niters = 0 |
| 1016 |
c* |
| 1017 |
c* *** the 1d newton's method *** |
| 1018 |
if (niters .gt. nitmax) goto 16 |
| 1019 |
ifail_tol = 0 |
| 1020 |
ALP_old = ALP |
| 1021 |
15 continue |
| 1022 |
niters = niters + 1 |
| 1023 |
c* |
| 1024 |
c* *** some i/o *** |
| 1025 |
c* ** write(6,110) |
| 1026 |
c* **2 'LINESEARCH: 1d newton iterations: niters, alpha = ', |
| 1027 |
c* **3 niters,alpha |
| 1028 |
c* ** format('% ',a,i5,1pe10.2) |
| 1029 |
c* |
| 1030 |
c* *** construct jacobian matrix of NN *** |
| 1031 |
call dc_vec(cc,r,zkp1,nx,ny,nz,ipkey) |
| 1032 |
val = xdot3(nx,ny,nz,zkp1,p) |
| 1033 |
DzNN = pAp + val |
| 1034 |
c* |
| 1035 |
c* *** update the solution *** |
| 1036 |
ALP_old = ALP |
| 1037 |
ALP = ALP - zNN / DzNN |
| 1038 |
c* |
| 1039 |
c* *** evaluate residual of 1d system *** |
| 1040 |
call xcopy(nx,ny,nz,x,r) |
| 1041 |
call xaxpy(nx,ny,nz,ALP,p,r) |
| 1042 |
call c_vec(cc,r,zkp1,nx,ny,nz,ipkey) |
| 1043 |
call xcopy(nx,ny,nz,zkp1,r) |
| 1044 |
call xaxpy(nx,ny,nz,(-1.0d0),zk,r) |
| 1045 |
val = xdot(nx,ny,nz,p,r) |
| 1046 |
zNN = ALP * pAp - pr + val |
| 1047 |
c* |
| 1048 |
c* *** tolerance and itmax check *** |
| 1049 |
if ((dabs(zNN) .lt. zntol) .and. |
| 1050 |
2 (dabs((ALP-ALP_old)/ALP) .lt. zntol) ) goto 17 |
| 1051 |
if (niters .gt. nitmax) goto 16 |
| 1052 |
c* |
| 1053 |
c* *** loop *** |
| 1054 |
goto 15 |
| 1055 |
c* |
| 1056 |
c* *** tolerance not reached *** |
| 1057 |
16 continue |
| 1058 |
ifail_tol = ifail_tol + 1 |
| 1059 |
c* |
| 1060 |
c* *** tolerance reached *** |
| 1061 |
17 continue |
| 1062 |
c* |
| 1063 |
c* *** newton's method complete -- update solution value *** |
| 1064 |
alpha = ALP |
| 1065 |
c* |
| 1066 |
c* *** messages *** |
| 1067 |
if (ifail_tol .gt. 0) then |
| 1068 |
print*,'% LINESEARCH: exceeded 1d newton itmax: = ',nitmax |
| 1069 |
endif |
| 1070 |
c* |
| 1071 |
c* *** return and end *** |
| 1072 |
return |
| 1073 |
end |