-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_minpack.f90
514 lines (453 loc) · 15.3 KB
/
mo_minpack.f90
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
module mo_minpack
! F90 interface to minpack from munipack http://munipack.physics.muni.cz/
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2012 Matthias Cuntz - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
use mo_kind, only: dp
implicit none
! set to .true. for debug prints
logical, parameter, private :: dbg = .false.
interface
subroutine chkder(m,n,x,fvec,fjac,ldfjac,xp,fvecp,mode,err)
use mo_kind, only: dp
implicit none
integer, intent(in) :: m,n,ldfjac,mode
real(dp) :: x(n),fvec(m),fjac(ldfjac,n),xp(n),fvecp(m),err(m)
end subroutine chkder
subroutine covar(n,r,ldr,ipvt,tol,wa)
use mo_kind, only: dp
implicit none
integer, intent(in) :: n,ldr
integer:: ipvt(n)
real(dp) :: tol
real(dp) :: r(ldr,n),wa(n)
end subroutine covar
subroutine dmchar(ibeta,it,irnd,ngrd,machep,negep,iexp,minexp, &
maxexp,eps,epsneg,xmin,xmax)
use mo_kind, only: dp
implicit none
integer :: ibeta,it,irnd,ngrd,machep,negep,iexp,minexp,maxexp
real(dp) :: eps,epsneg,xmin,xmax
end subroutine dmchar
subroutine dogleg(n,r,lr,diag,qtb,delta,x,wa1,wa2)
use mo_kind, only: dp
implicit none
integer :: n,lr
real(dp) :: delta
real(dp) :: r(lr),diag(n),qtb(n),x(n),wa1(n),wa2(n)
end subroutine dogleg
function dpmpar(i)
use mo_kind, only: dp
implicit none
real(dp) :: dpmpar
integer, intent(in) :: i
end function dpmpar
function enorm(n,x)
use mo_kind, only: dp
implicit none
integer, intent(in) :: n
real(dp) :: enorm,x(n)
end function enorm
subroutine errjac(n,x,fjac,ldfjac,nprob)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,nprob
real(dp) :: x(n),fjac(ldfjac,n)
end subroutine errjac
subroutine fdjac1(fcn,n,x,fvec,fjac,ldfjac,iflag,ml,mu,epsfcn,wa1,wa2)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,iflag,ml,mu
real(dp) :: epsfcn
real(dp) :: x(n),fvec(n),fjac(ldfjac,n),wa1(n),wa2(n)
interface
subroutine fcn(n,x,fvec,iflag)
use mo_kind, only: dp
implicit none
integer :: n,iflag
real(dp) :: x(n),fvec(n)
end subroutine fcn
end interface
end subroutine fdjac1
subroutine fdjac2(fcn,m,n,x,fvec,fjac,ldfjac,iflag,epsfcn,wa)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,iflag
real(dp) :: epsfcn
real(dp) :: x(n),fvec(m),fjac(ldfjac,n),wa(m)
interface
subroutine fcn(m,n,x,fvec,iflag)
use mo_kind, only: dp
implicit none
integer :: m,n,iflag
real(dp) :: x(n),fvec(m)
end subroutine fcn
end interface
end subroutine fdjac2
subroutine grdfcn(n,x,g,nprob)
use mo_kind, only: dp
implicit none
integer :: n,nprob
real(dp) :: x(n),g(n)
end subroutine grdfcn
subroutine hesfcn(n,x,h,ldh,nprob)
use mo_kind, only: dp
implicit none
integer :: n,ldh,nprob
real(dp) :: x(n),h(ldh,n)
end subroutine hesfcn
subroutine initpt(n,x,nprob,factor)
use mo_kind, only: dp
implicit none
integer :: n,nprob
real(dp) :: factor
real(dp) :: x(n)
end subroutine initpt
subroutine hybrd(fcn,n,x,fvec,xtol,maxfev,ml,mu,epsfcn,diag, &
mode,factor,nprint,info,nfev,fjac,ldfjac,r,lr, &
qtf,wa1,wa2,wa3,wa4)
use mo_kind, only: dp
implicit none
integer :: n,maxfev,ml,mu,mode,nprint,info,nfev,ldfjac,lr
real(dp) :: xtol,epsfcn,factor
real(dp) :: x(n),fvec(n),diag(n),fjac(ldfjac,n),r(lr), &
qtf(n),wa1(n),wa2(n),wa3(n),wa4(n)
interface
subroutine fcn(n,x,fvec,iflag)
use mo_kind, only: dp
implicit none
integer,intent(in) :: n
integer,intent(inout) :: iflag
real(dp),intent(in) :: x(n)
real(dp),intent(out) :: fvec(n)
end subroutine fcn
end interface
end subroutine hybrd
subroutine hybrd1(fcn,n,x,fvec,tol,info,wa,lwa)
use mo_kind, only: dp
implicit none
integer :: n,info,lwa
real(dp) :: tol
real(dp) :: x(n),fvec(n),wa(lwa)
interface
subroutine fcn(n,x,fvec,iflag)
use mo_kind, only: dp
implicit none
integer :: n,iflag
real(dp) :: x(n),fvec(n)
end subroutine fcn
end interface
end subroutine hybrd1
subroutine hybrj(fcn,n,x,fvec,fjac,ldfjac,xtol,maxfev,diag,mode, &
factor,nprint,info,nfev,njev,r,lr,qtf,wa1,wa2, &
wa3,wa4)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,maxfev,mode,nprint,info,nfev,njev,lr
real(dp) :: xtol,factor
real(dp) :: x(n),fvec(n),fjac(ldfjac,n),diag(n),r(lr), &
qtf(n),wa1(n),wa2(n),wa3(n),wa4(n)
interface
subroutine fcn(n,x,fvec,fjac,ldfjac,iflag)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,iflag
real(dp) :: x(n),fvec(n),fjac(ldfjac,n)
end subroutine fcn
end interface
end subroutine hybrj
subroutine hybrj1(fcn,n,x,fvec,fjac,ldfjac,tol,info,wa,lwa)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,info,lwa
real(dp) :: tol
real(dp) :: x(n),fvec(n),fjac(ldfjac,n),wa(lwa)
interface
subroutine fcn(n,x,fvec,fjac,ldfjac,iflag)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,iflag
real(dp) :: x(n),fvec(n),fjac(ldfjac,n)
end subroutine fcn
end interface
end subroutine hybrj1
subroutine lmder(fcn,m,n,x,fvec,fjac,ldfjac,ftol,xtol,gtol, &
maxfev,diag,mode,factor,nprint,info,nfev,njev, &
ipvt,qtf,wa1,wa2,wa3,wa4)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,maxfev,mode,nprint,info,nfev,njev
integer :: ipvt(n)
real(dp) :: ftol,xtol,gtol,factor
real(dp) :: x(n),fvec(m),fjac(ldfjac,n),diag(n),qtf(n), &
wa1(n),wa2(n),wa3(n),wa4(m)
interface
subroutine fcn(m,n,x,fvec,fjac,ldfjac,iflag)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,iflag
real(dp) :: x(n),fvec(m),fjac(ldfjac,n)
end subroutine fcn
end interface
end subroutine lmder
subroutine lmder1(fcn,m,n,x,fvec,fjac,ldfjac,tol,info,ipvt,wa,lwa)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,info,lwa
integer :: ipvt(n)
real(dp) :: tol
real(dp) :: x(n),fvec(m),fjac(ldfjac,n),wa(lwa)
interface
subroutine fcn(m,n,x,fvec,fjac,ldfjac,iflag)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,iflag
real(dp) :: x(n),fvec(m),fjac(ldfjac,n)
end subroutine fcn
end interface
end subroutine lmder1
subroutine lmdif(fcn,m,n,x,fvec,ftol,xtol,gtol,maxfev,epsfcn, &
diag,mode,factor,nprint,info,nfev,fjac,ldfjac, &
ipvt,qtf,wa1,wa2,wa3,wa4)
use mo_kind, only: dp
implicit none
integer :: m,n,maxfev,mode,nprint,info,nfev,ldfjac
integer :: ipvt(n)
real(dp) :: ftol,xtol,gtol,epsfcn,factor
real(dp) :: x(n),fvec(m),diag(n),fjac(ldfjac,n),qtf(n), &
wa1(n),wa2(n),wa3(n),wa4(m)
interface
subroutine fcn(m,n,x,fvec,iflag)
use mo_kind, only: dp
implicit none
integer :: m,n,iflag
real(dp) :: x(n),fvec(m)
end subroutine fcn
end interface
end subroutine lmdif
subroutine lmdif1(fcn,m,n,x,fvec,tol,info,iwa,wa,lwa)
use mo_kind, only: dp
implicit none
integer :: m,n,info,lwa
integer :: iwa(n)
real(dp) :: tol
real(dp) :: x(n),fvec(m),wa(lwa)
interface
subroutine fcn(m,n,x,fvec,iflag)
use mo_kind, only: dp
implicit none
integer :: m,n,iflag
real(dp) :: x(n),fvec(m)
end subroutine fcn
end interface
end subroutine lmdif1
subroutine lmpar(n,r,ldr,ipvt,diag,qtb,delta,par,x,sdiag,wa1,wa2)
use mo_kind, only: dp
implicit none
integer :: n,ldr
integer :: ipvt(n)
real(dp) :: delta,par
real(dp) :: r(ldr,n),diag(n),qtb(n),x(n),sdiag(n),wa1(n),wa2(n)
end subroutine lmpar
subroutine lmstr(fcn,m,n,x,fvec,fjac,ldfjac,ftol,xtol,gtol, &
maxfev,diag,mode,factor,nprint,info,nfev,njev, &
ipvt,qtf,wa1,wa2,wa3,wa4)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,maxfev,mode,nprint,info,nfev,njev
integer :: ipvt(n)
real(dp) :: ftol,xtol,gtol,factor
real(dp) :: x(n),fvec(m),fjac(ldfjac,n),diag(n),qtf(n), &
wa1(n),wa2(n),wa3(n),wa4(m)
interface
subroutine fcn(m,n,x,fvec,fjrow,iflag)
use mo_kind, only: dp
implicit none
integer :: m,n,iflag
real(dp) :: x(n),fvec(m),fjrow(n)
end subroutine fcn
end interface
end subroutine lmstr
subroutine lmstr1(fcn,m,n,x,fvec,fjac,ldfjac,tol,info,ipvt,wa,lwa)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,info,lwa
integer :: ipvt(n)
real(dp) :: tol
real(dp) :: x(n),fvec(m),fjac(ldfjac,n),wa(lwa)
interface
subroutine fcn(m,n,x,fvec,fjrow,iflag)
use mo_kind, only: dp
implicit none
integer m,n,iflag
real(dp) x(n),fvec(m),fjrow(n)
end subroutine fcn
end interface
end subroutine lmstr1
subroutine objfcn(n,x,f,nprob)
use mo_kind, only: dp
implicit none
integer :: n,nprob
real(dp) :: f
real(dp) :: x(n)
end subroutine objfcn
subroutine qform(m,n,q,ldq,wa)
use mo_kind, only: dp
implicit none
integer :: m,n,ldq
real(dp) :: q(ldq,m),wa(m)
end subroutine qform
subroutine qrfac(m,n,a,lda,pivot,ipvt,lipvt,rdiag,acnorm,wa)
use mo_kind, only: dp
implicit none
integer :: m,n,lda,lipvt
integer :: ipvt(lipvt)
logical :: pivot
real(dp) :: a(lda,n),rdiag(n),acnorm(n),wa(n)
end subroutine qrfac
subroutine qrsolv2(n,r,ldr,ipvt,diag,qtb,x,sdiag,wa)
use mo_kind, only: dp
implicit none
integer :: n,ldr
integer :: ipvt(n)
real(dp) :: r(ldr,n),diag(n),qtb(n),x(n),sdiag(n),wa(n)
end subroutine qrsolv2
subroutine r1mpyq(m,n,a,lda,v,w)
use mo_kind, only: dp
implicit none
integer :: m,n,lda
real(dp) :: a(lda,n),v(n),w(n)
end subroutine r1mpyq
subroutine r1updt(m,n,s,ls,u,v,w,sing)
use mo_kind, only: dp
implicit none
integer :: m,n,ls
logical :: sing
real(dp) :: s(ls),u(m),v(n),w(m)
end subroutine r1updt
subroutine rwupdt(n,r,ldr,w,b,alpha,cos,sin)
use mo_kind, only: dp
implicit none
integer :: n,ldr
real(dp) :: alpha
real(dp) :: r(ldr,n),w(n),b(n),cos(n),sin(n)
end subroutine rwupdt
subroutine ssqfcn(m,n,x,fvec,nprob)
use mo_kind, only: dp
implicit none
integer :: m,n,nprob
real(dp) :: x(n),fvec(m)
end subroutine ssqfcn
subroutine ssqjac(m,n,x,fjac,ldfjac,nprob)
use mo_kind, only: dp
implicit none
integer :: m,n,ldfjac,nprob
real(dp) :: x(n),fjac(ldfjac,n)
end subroutine ssqjac
subroutine vecfcn(n,x,fvec,nprob)
use mo_kind, only: dp
implicit none
integer :: n,nprob
real(dp) :: x(n),fvec(n)
end subroutine vecfcn
subroutine vecjac(n,x,fjac,ldfjac,nprob)
use mo_kind, only: dp
implicit none
integer :: n,ldfjac,nprob
real(dp) :: x(n),fjac(ldfjac,n)
end subroutine vecjac
end interface
contains
!----------------------------------------------------------------------
!
! add-ons
!
!----------------------------------------------------------------------
subroutine qrinv(m,n,a,a1,lda,diag)
! compute inverse matrix in least-quare sense by the use
! of the QR factorisation
! implementation is reference, no effective, test for different
! m and n are sparse
use mo_kind, only: dp
implicit none
integer :: m,n,lda
real(dp) :: a(lda,n),a1(lda,n), diag(n)
real(dp) :: r(m,n), q(m,n), qtb(m,n)
integer :: i,ipvt(n)
real(dp) :: rdiag(n),acnorm(n),wa(n),x(n),b(n),sdiag(n)
character(len=10) :: fmt = '(xxf17.7)'
if(dbg) then
write(*,*) 'qrinv:'
write(fmt(2:3),'(i2.2)') n
write(*,fmt) a
endif
! form the r matrix, r is upper trinagle (without diagonal)
! of the factorized a, diagonal is presented in rdiag
call qrfac(m,n,a,lda,.true.,ipvt,n,rdiag,acnorm,wa)
if(dbg) then
write(*,*) 'qrfac:'
write(*,fmt) a,rdiag,acnorm
end if
r = a
do i = 1, n
r(i,i) = rdiag(i)
end do
if(dbg) then
write(*,*) 'r:'
write(*,fmt) r
endif
! form the q matrix
call qform(m,n,a,lda,wa)
if(dbg) then
write(*,*) 'qform:'
write(*,fmt) a,rdiag,acnorm
write(*,*) 'ipvt:'
write(*,*) ipvt
end if
q = a
qtb = transpose(q)
do i = 1, n
b = 0
b(i) = 1.0
b = matmul(qtb,b)
call qrsolv2(n,r,m,ipvt,diag,b,x,sdiag,wa)
a1(i,:) = x
enddo
end subroutine qrinv
!---------------------------------------------------------------------
!
! Fortran 90 interfaces
!
!---------------------------------------------------------------------
! subroutine chkder_8(x,fvec,fjac,xp,fvecp,mode,err)
! integer, intent(in) :: mode
! real(dp) :: x(:),fvec(:),fjac(:,:),xp(:),fvecp(:),err(:)
! integer :: m,n,ldfjac
!
! m = size(fvec)
! n = size(x)
! ldfjac = size(fjac,1)
!
! call chkder(m,n,x,fvec,fjac,ldfjac,xp,fvecp,mode,err)
! end subroutine chkder_8
end module mo_minpack