/
model.f90
1324 lines (1172 loc) · 47.3 KB
/
model.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Copyright (C) 2003-2018 John Young, Matthew Worsley
!
! This file is part of mfit.
!
! Mfit is free software: you can redistribute it and/or modify it
! under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! This program is distributed in the hope that it will be useful, but
! WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
! General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program. If not, see http://www.gnu.org/licenses/ .
module Model
use Maths
use Search
implicit none
include 'fftw3.f'
private
!public subroutines contained
!
!read_model
!model_valid
!model_nvar
!model_getvar
!print_model
!write_model
!free_model
public :: read_model, model_valid, model_nvar, model_getvar
public :: print_model, write_model, free_model
!public module variables contained:
public :: model_spec, model_pos_relto, model_wldep, nwave, model_wb
public :: model_param, model_prior, model_limits, max_order
public :: model_desc, model_desc_len, model_name, symm_model
public :: nxsiz, clv_mdiam, clv_mvis, clv_mbase
!! Specifies nature of model cpts -
!! holds 3 items per component: 1 name, 2 shape and 3 LD type
character(len=128), allocatable :: model_spec(:,:)
!! If model_pos_relto(icpt) is a valid model component, r & theta for
!! component icpt are treated as multipliers for r & theta for the specified
!! (other) component
integer, allocatable :: model_pos_relto(:)
!! model_wldep(i) is unity if position, flux, shape_param,
!! ld_param respectively is fn(waveband), else zero
integer :: model_wldep(4)
!! Model has nwave values for each wavelength-dependent parameter
integer :: nwave
!! Wavebands for wavelength-dependent model parameters
!! (if supplied to read_model) and CLV
real, allocatable :: model_wb(:, :)
!! Initial model parameters (both variable and non-variable)
!! for each cpt (1st axis): 2nd axis is
!! LD order
!! {r, theta(t0), t0, d(theta)/dt} possibly repeated for each waveband
!! {brightness} possibly repeated for each waveband
!! {major axis, phi, epsilon} possibly repeated for each waveband
!! {LD coeffs 1-max_order} possibly repeated for each waveband
!! EXCEPT for 'ld_type two-layer', which has
!! {LD coeffs 1-5} coeff 5 possibly repeated for each waveband
double precision, allocatable :: model_param(:,:)
integer max_order !at least one component has this many LD coeffs
!! Gaussian prior widths for parameters; prior centres are in model_param
double precision, allocatable :: model_prior(:,:)
!! Limits on legal values for parameters (both variable and non-variable)
double precision, allocatable :: model_limits(:,:,:)
integer, parameter :: model_desc_len = 55
!! Descriptions of model parameters
character(len=model_desc_len), allocatable :: model_desc(:, :)
!! Name from model file (2 words max)
character(len=128) :: model_name
!! Centrosymmetric model?
logical :: symm_model
!numerical CLV data
real, allocatable :: clv_rad(:)
real, allocatable :: clv_inten(:, :)
integer :: nclv, clvcomp
!model visibilities calculated from numerical CLV
integer, parameter :: nxsiz = 4096, modelsize = 120
real :: clv_mbase(nxsiz+1)
real, allocatable :: clv_mvis(:, :)
real :: clv_mdiam
contains
!============================================================================
!! Read consecutive lines which are blank or start with specified character
!! Backspace on reading first non-blank, non-comment line
function skip_lines(iunit, mark, eof)
!subroutine arguments
integer, intent(in) :: iunit
character (len=1), intent(in) :: mark
logical, intent(out) :: eof
integer skip_lines
!local variables
!!character(len=256) :: line
character(len=80) :: line
skip_lines = 0
eof = .false.
do
read(iunit,'(a)', end=1, err=2) line
if(len_trim(line) > 0 .and. line(1:1) /= mark) then
backspace(iunit)
return
end if
skip_lines = skip_lines + 1
print *, line
end do
1 eof = .true.
return
! I/O error, rely on calling routine to re-detect
2 return
end function skip_lines
!============================================================================
!! Read model files
!! (Re-)Allocates and assigns to module variables
subroutine read_model(info, file_name, wavebands)
!subroutine arguments
character(len=*), intent(out) :: info !! Error message
character(len=*), intent(in) :: file_name !! Filename to read
!! Wavebands being used in data
double precision, intent(in), optional :: wavebands(:,:)
!local variables
logical eof
character(len=1) :: comment
character(len=2) :: numbers(10)
character(len=32) :: keyw, cpt
character(len=32), allocatable :: wb(:)
character(len=128) :: clv_filename, shape_type, ld_type
character(len=256) :: line
integer :: i, j, k, n, line_no, iwave, loc, relto
integer :: comps, order, pos, npar, ipar, nread, nn
!check for zero length filename
if (file_name == '') then
info = 'blank filename'
return
end if
!read and count number of components and number of lines
!also determine which parameters are wavelength-dependent (model_wldep),
!how many wavebands values are supplied for (nwave),
!and the maximum ld_order (max_order)
open (unit=11, err=91, status='old', action='read', file=file_name)
comment = '!'
comps = 0
model_wldep = 0
nwave = -1
max_order = 0
line_no = 1
pass1: do
line_no = line_no + skip_lines(11, comment, eof)
if(eof) exit pass1
read (11, '(a)', err=92) line
line_no = line_no + 1
if (len_trim(line) == 0) cycle !blank line
read (line, *) keyw !read keyword & qualifiers
if (keyw == 'component') comps = comps+1
if (keyw(:8) == 'position' .and. index(keyw, '#fofwave') /= 0) then
model_wldep(1) = 1
! qualifiers could be any combination of #fofwave, #rotate, #reltoN
if (index(keyw, '#rotate') /= 0) then
!{r, theta(t0), t0, d(theta)/dt} poss. repeated for each waveband
nn = (countsym(line)-1)/4
else
!file contains {r, theta} poss. repeated for each waveband
nn = (countsym(line)-1)/2
end if
if (nwave == -1) then
nwave = nn
else if (nn /= nwave) then
write(info, *) 'line ', line_no, &
': expecting values for ', nwave, ' wavebands, got ', nn
close(11)
return
end if
else if (keyw == 'flux#fofwave') then
model_wldep(2) = 1
nn = countsym(line)-1
if (nwave == -1) then
nwave = nn
else if (nn /= nwave) then
write(info, *) 'line ', line_no, &
': expecting values for ', nwave, ' wavebands, got ', nn
close(11)
return
end if
else if (keyw == 'shape_type') then
read (line, *, end=95) keyw, shape_type
else if (keyw == 'shape_param#fofwave') then
select case (shape_type)
case ('point')
print *, 'cannot have wavelength-dependent point: no shape parameters'
case ('disc')
model_wldep(3) = 1
nn = countsym(line)-1
case ('ellipse')
model_wldep(3) = 1
nn = (countsym(line)-1)/3
case default
info = 'invalid shape type'
close (11)
return
end select
if (nwave == -1) then
nwave = nn
else if (nn /= nwave) then
write(info, *) 'line ', line_no, &
': expecting values for ', nwave, ' wavebands, got ', nn
close(11)
return
end if
else if (keyw == 'ld_type') then
read (line, *, end=95) keyw, ld_type
!Read LD order (preset for some LD type cases)
!Non-integer will cause error in read statement
select case (trim(ld_type))
case ('uniform','gaussian','thin-ring')
read (11,*,err=95,end=95) keyw
order = 0
case ('square-root')
read (11,*,err=95,end=95) keyw
order = 2
case ('hestroffer')
read (11,*,err=95,end=95) keyw
order = 1
case ('taylor','gauss-hermite')
read (11,*,err=95,end=95) keyw, order
case ('two-layer')
read (11,*,err=95,end=95) keyw
order = 5
case default
if (ld_type(1:1) == '<') then
read (11,*,err=95,end=95) keyw
order = 0
else
!not <filename>
info = 'invalid limb darkening type'
close (11)
return
end if
end select
line_no = line_no + 1
if (order > max_order) max_order = order
else if (keyw == 'ld_param#fofwave') then
model_wldep(4) = 1
if (trim(ld_type) == 'two-layer') then
nn = countsym(line)-5
else
nn = (countsym(line)-1)/order
end if
if (nwave == -1) then
nwave = nn
else if (nn /= nwave) then
write(info, *) 'line ', line_no, &
': expecting values for ', nwave, ' wavebands, got ', nn
close(11)
return
end if
end if
end do pass1
close(11)
!check if legal number of components
if ((comps<1).or.(comps>10)) then
info = 'illegal number of components present'
return
end if
!check wavebands
if (nwave /= -1) then
!wavelength-dependent model
if (present(wavebands)) then
if (nwave < size(wavebands, 1)) then
write(info, '(i3, a, i3, a)') size(wavebands, 1), &
' waveband(s) in data, but only ', nwave, &
' in wavelength-dependent model'
return
else if (nwave > size(wavebands, 1)) then
nwave = size(wavebands, 1)
print *, 'Reading first ', nwave, ' waveband(s) from model'
endif
!wavebands not present if read_model called from modelplot
end if
else
!could still have wavelength-dependent CLV
if (present(wavebands)) then
nwave = size(wavebands, 1)
else
nwave = 1
end if
end if
!allocate model data arrays
!each of position, flux, shape_param, ld_param can be either
!wavelength-independent (default) or wavelength-dependent, FOR ALL
!components
!shape_type, ld_type, ld_order of a component are the same for all wavebands
call free_model()
allocate(model_spec(comps,3))
if (present(wavebands)) then
allocate(model_wb(size(wavebands, 1), 2))
model_wb = wavebands
end if
npar = 1 !LD order
npar = npar + 4*(1 + model_wldep(1)*(nwave-1)) !r, theta(t0), t0, d(theta)/dt
npar = npar + 1*(1 + model_wldep(2)*(nwave-1)) !flux
npar = npar + 3*(1 + model_wldep(3)*(nwave-1)) !major axis, phi, epsilon
npar = npar + max_order*(1 + model_wldep(4)*(nwave-1)) !LD coeffs
allocate(model_param(comps,npar))
allocate(model_prior(comps,npar))
allocate(model_limits(comps,npar,2))
allocate(model_desc(comps,npar))
allocate(model_pos_relto(comps))
model_param = 0D0
model_prior = 0D0
clvcomp = 0 !no model component yet has numerical CLV
!assign strings describing model wavebands
allocate(wb(nwave))
do iwave = 1, nwave
if (allocated(model_wb)) then
write(wb(iwave), '(a, f7.1, a, f6.1, a)') ' [', model_wb(iwave,1), &
'/', model_wb(iwave,2), ']'
else
write(wb(iwave), '(a, i3, a)') '[', iwave, ']'
end if
end do
!Limits array stores the lower and upper acceptable limits
!on the parameter values, it is passed into the minimising routines
!later to ensure parameter values stay legal. Model values not within
!these limits trigger error on reading the model
!Remainder filled in as we read model file
numbers = (/' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9','10'/)
do j = 1, comps
model_limits(j,1,1:2) = dble((/0,10/))
cpt = 'cpt '// trim(adjustl(numbers(j))) // ', '
model_desc(j, 1) = trim(cpt) // ' LD order'
end do
!read spec and param info for components
open (unit=11, action='read', file=file_name)
j = 0 !component counter
model_name = 'model' !default if "source" keyword missing
pass2: do
read (11,*,err=94,end=12) keyw
if (keyw == 'source') then
backspace(11)
read (11,'(a)',err=95,end=95) line
line = line(index(line, 'source')+6:) !strip 'source'
pos = verify(line, ' '//achar(9))
model_name = line(pos:) !strip leading spaces and tabs
end if
if (keyw == 'component') then
j = j+1
cpt = 'cpt '// trim(adjustl(numbers(j))) // ', '
!read component name
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, model_spec(j,1)
if (keyw /= 'name') goto 96
!read shape type
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, model_spec(j,2)
if (keyw /= 'shape_type') goto 96
!read LD type (set as uniform for point case)
select case (trim(model_spec(j,2)))
case ('point')
read (11,*,err=95) keyw
model_spec(j,3) = 'uniform'
case default
n = skip_lines(11, comment, eof)
read (11,'(a)',err=95,end=95) line
read (line,*,err=95,end=95) keyw
!from last non-trailing space or tab character:
pos = scan(trim(line), ' '//achar(9), back=.true.) + 1
model_spec(j,3) = line(pos:)
end select
if (keyw /= 'ld_type') goto 96
!Read LD order (preset for some LD type cases)
!Non-integer will cause error in read statement
n = skip_lines(11, comment, eof)
select case (trim(model_spec(j,3)))
case ('uniform','gaussian','thin-ring')
read (11,*,err=95,end=95) keyw
order = 0
case ('square-root')
read (11,*,err=95,end=95) keyw
order = 2
case ('hestroffer')
read (11,*,err=95,end=95) keyw
order = 1
case ('taylor','gauss-hermite')
read (11,*,err=95,end=95) keyw, order
case ('two-layer')
read (11,*,err=95,end=95) keyw
order = 5
case default
if (model_spec(j,3)(1:1) == '<') then
if (clvcomp /= 0) then
info = 'only one numerical CLV component allowed'
close (11)
return
end if
clvcomp = j
clv_filename = model_spec(j,3)(2:(len_trim(model_spec(j,3))-1))
if (allocated(model_wb)) then
call read_clv(info, clv_filename, model_wb)
else
call read_clv(info, clv_filename)
end if
if (info /= '') then
close (11)
return
end if
read (11,*,err=95,end=95) keyw
order = 0
else
!not <filename>
info = 'invalid limb darkening type'
close (11)
return
end if
end select
if (keyw /= 'ld_order') goto 96
model_param(j,1) = dble(order)
!read position r and theta etc.
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw
if (keyw(:8) /= 'position') goto 96
loc = index(keyw, '#relto')
if (loc /= 0) then
read (keyw(loc+6:), *) model_pos_relto(j)
else
model_pos_relto(j) = 0
end if
relto = model_pos_relto(j)
!loop over wavebands, assign parameter limits and descriptions
do k = 1, 1+model_wldep(1)*(nwave-1)
model_limits(j,2+(k-1)*4,1:2) = dble((/0, 500/))
model_limits(j,3+(k-1)*4,1:2) = dble((/-720, 720/))
model_limits(j,4+(k-1)*4,1:2) = dble((/10000, 100000/))
model_limits(j,5+(k-1)*4,1:2) = dble((/-1000, 1000/))
if (relto >= 1 .and. relto <= comps) then
!allow negative radius multipliers
model_limits(j,2+(k-1)*4,1:2) = dble((/-500, 500/))
end if
!defaults for t0 and d(theta)/dt
model_param(j, 4+(k-1)*4) = 10000D0
model_param(j, 5+(k-1)*4) = 0D0
!descriptions
if (model_wldep(1) == 1) then
if (relto >= 1 .and. relto <= comps) then
model_desc(j, 2+(k-1)*4) = trim(cpt) // trim(wb(k)) &
// ' position radius as multiple of cpt ' &
// trim(adjustl(numbers(relto))) // ' radius'
model_desc(j, 3+(k-1)*4) = trim(cpt) // trim(wb(k)) &
// ' position angle(t0) as multiple of cpt ' &
// trim(adjustl(numbers(relto))) // ' angle'
else
model_desc(j, 2+(k-1)*4) = trim(cpt) // trim(wb(k)) &
// ' position radius (mas)'
model_desc(j, 3+(k-1)*4) = trim(cpt) // trim(wb(k)) &
// ' position angle(t0) (deg)'
end if
model_desc(j, 4+(k-1)*4) = trim(cpt) // trim(wb(k)) &
// ' t0 (MJD)'
model_desc(j, 5+(k-1)*4) = trim(cpt) // trim(wb(k)) &
// ' d(PA)/dt (deg/day)'
else
if (relto >= 1 .and. relto <= comps) then
model_desc(j, 2+(k-1)*4) = trim(cpt) &
// ' position radius as multiple of cpt ' &
// trim(adjustl(numbers(relto))) // ' radius'
model_desc(j, 3+(k-1)*4) = trim(cpt) &
// ' position angle(t0) as multiple of cpt ' &
// trim(adjustl(numbers(relto))) // ' angle'
else
model_desc(j, 2+(k-1)*4) = trim(cpt) // ' position radius (mas)'
model_desc(j, 3+(k-1)*4) = trim(cpt) // ' position angle(t0) (deg)'
end if
model_desc(j, 4+(k-1)*4) = trim(cpt) // ' t0 (MJD)'
model_desc(j, 5+(k-1)*4) = trim(cpt) // ' d(PA)/dt (deg/day)'
end if
end do
! qualifiers could be any combination of #fofwave, #rotate, #reltoN
if (index(keyw, '#rotate') /= 0) then
!{r, theta(t0), t0, d(theta)/dt} poss. repeated for each waveband
backspace(11)
nread = 4*(1 + model_wldep(1)*(nwave-1))
read (11,*,err=95,end=95) keyw, model_param(j, 2:1+nread)
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, model_prior(j, 2:1+nread)
if (keyw /= 'position_prior') goto 96
else
!file contains {r, theta} poss. repeated for each waveband
backspace(11)
read (11,*,err=95,end=95) keyw, &
(model_param(j, 2+(k-1)*4:3+(k-1)*4), &
k=1,(1+model_wldep(1)*(nwave-1)))
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, &
(model_prior(j, 2+(k-1)*4:3+(k-1)*4), &
k=1,(1+model_wldep(1)*(nwave-1)))
if (keyw /= 'position_prior') goto 96
end if
!read flux
ipar = 6+4*model_wldep(1)*(nwave-1)
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, &
model_param(j, ipar:ipar+model_wldep(2)*(nwave-1))
if (keyw(:4) /= 'flux') goto 96
do k = 1, 1+model_wldep(2)*(nwave-1)
model_limits(j,ipar+(k-1),1:2) = dble((/-100, 100/))
if (model_wldep(2) == 1) then
model_desc(j, ipar+(k-1)) = trim(cpt) // trim(wb(k)) // ' flux (arb units)'
else
model_desc(j, ipar+(k-1)) = trim(cpt) // ' flux (arb units)'
end if
end do
!read flux prior, must be non-negative
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, &
model_prior(j, ipar:ipar+model_wldep(2)*(nwave-1))
if (keyw /= 'flux_prior') goto 96
!Read shape parameters a, phi, epsilon depending on
!shape type. Also read priors
!Shape a and epsilon must be non-negative
!All priors must be non-negative
ipar = 7 + (4*model_wldep(1) + model_wldep(2))*(nwave-1)
do k=1, 1+model_wldep(3)*(nwave-1)
model_param(j,ipar+(k-1)*3) = 0D0
model_param(j,ipar+(k-1)*3+1) = 0D0
model_param(j,ipar+(k-1)*3+2) = 1D0
model_limits(j,ipar+(k-1)*3,1:2) = dble((/0, 1000/))
model_limits(j,ipar+(k-1)*3+1,1:2) = dble((/-720, 720/))
model_limits(j,ipar+(k-1)*3+2,1:2) = dble((/0, 10/))
if (model_wldep(3) == 1) then
model_desc(j,ipar+(k-1)*3) = trim(cpt) // trim(wb(k)) // ' major axis (mas)'
model_desc(j,ipar+(k-1)*3+1) = trim(cpt) // trim(wb(k)) // ' orientation (deg)'
model_desc(j,ipar+(k-1)*3+2) = trim(cpt) // trim(wb(k)) // ' ellipticity'
else
model_desc(j,ipar+(k-1)*3) = trim(cpt) // ' major axis (mas)'
model_desc(j,ipar+(k-1)*3+1) = trim(cpt) // ' orientation (deg)'
model_desc(j,ipar+(k-1)*3+2) = trim(cpt) // ' ellipticity'
end if
end do
n = skip_lines(11, comment, eof)
select case (trim(model_spec(j,2)))
case ('point')
read (11,*,err=95,end=95) keyw
if (keyw(:11) /= 'shape_param') goto 96
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw
case ('disc')
read (11,*,err=95,end=95) keyw, (model_param(j,ipar+(k-1)*3), &
k=1,(1+model_wldep(3)*(nwave-1)))
if (keyw(:11) /= 'shape_param') goto 96
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, (model_prior(j,ipar+(k-1)*3), &
k=1,(1+model_wldep(3)*(nwave-1)))
case ('ellipse')
nread = 3*(1 + model_wldep(3)*(nwave-1))
read (11,*,err=95,end=95) keyw, model_param(j,ipar:ipar+nread-1)
if (keyw(:11) /= 'shape_param') goto 96
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, model_prior(j,ipar:ipar+nread-1)
case default
info = 'invalid shape type'
close (11)
return
end select
if (keyw /= 'shape_param_prior') goto 96
!read LD parameters
n = skip_lines(11, comment, eof)
ipar = 10 + (4*model_wldep(1) + model_wldep(2) &
+ 3*model_wldep(3))*(nwave-1)
select case (order)
case (0)
read (11,*,err=95,end=95) keyw
if (keyw(:8) /= 'ld_param') goto 96
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw
case default
if (trim(model_spec(j,3)) == 'two-layer') then
nread = 5 + model_wldep(4)*(nwave-1)
else
nread = order*(1 + model_wldep(4)*(nwave-1))
end if
read (11,*,err=95,end=95) keyw, &
model_param(j, ipar:ipar+nread-1)
if (keyw(:8) /= 'ld_param') goto 96
n = skip_lines(11, comment, eof)
read (11,*,err=95,end=95) keyw, &
model_prior(j, ipar:ipar+nread-1)
end select
if (trim(model_spec(j,3)) == 'two-layer') then
!different limits for LD parameters if 2-layer component
model_limits(j,ipar:ipar+2,1) = 0D0
model_limits(j,ipar:ipar+2,2) = 1D5
model_limits(j,ipar+3,1) = 1D0
model_limits(j,ipar+3,2) = 100D0
do k = 1, 1+model_wldep(4)*(nwave-1)
model_limits(j,ipar+3+k,1) = 0D0
model_limits(j,ipar+3+k,2) = 100D0
end do
else
model_limits(j, ipar: &
ipar+max_order*(1 + model_wldep(4)*(nwave-1))-1,1) = -100D0
model_limits(j, ipar: &
ipar+max_order*(1 + model_wldep(4)*(nwave-1))-1,2) = 100D0
end if
!LD param descriptions
if (trim(model_spec(j,3)) == 'two-layer') then
model_desc(j,ipar) = trim(cpt) // ' NStep '
model_desc(j,ipar+1) = trim(cpt) // ' Temp1 /K '
model_desc(j,ipar+2) = trim(cpt) // ' Temp2 /K '
model_desc(j,ipar+3) = trim(cpt) // ' R2/R1 '
if (model_wldep(4) == 1) then
do iwave = 1, nwave
model_desc(j,ipar+3+iwave) = trim(cpt) &
// trim(wb(iwave)) // ' Optical depth '
end do
else
model_desc(j,ipar+4) = trim(cpt) // ' Optical depth '
end if
else
do k = 1, int(model_param(j, 1)) !order for this cpt
if (model_wldep(4) == 1) then
do iwave = 1, nwave
model_desc(j, ipar+max_order*(iwave-1)+k-1) = trim(cpt) &
// trim(wb(iwave)) // ' LD parameter ' // adjustl(numbers(k))
end do
else
model_desc(j, ipar+k-1) = trim(cpt) // ' LD parameter ' &
// adjustl(numbers(k))
end if
end do
end if
!amend model_limits:
!alpha(1) in hestroffer model must be > 0
if (trim(model_spec(j,3)) == 'hestroffer') then
if (model_wldep(4) == 1) then
do iwave = 1, nwave
k = 10+(4*model_wldep(1)+model_wldep(2) &
+3*model_wldep(3))*(nwave-1) &
+ max_order*model_wldep(4)*(iwave-1)
model_limits(j,k,1) = 0D0
end do
else
k = 10+(4*model_wldep(1)+model_wldep(2) &
+3*model_wldep(3))*(nwave-1)
model_limits(j,k,1) = 0D0
end if
end if
!check to ensure everything inside limits
do k=1, size(model_limits,2)
if (model_param(j,k) < model_limits(j,k,1)) goto 100
if (model_param(j,k) > model_limits(j,k,2)) goto 100
end do
!if numerical CLV, calculate visibilities for initial guess diameter
!wavelength-dep CLV with wavelength-dep diameter doesn't make sense
!- this is trapped in minimiser()
if (model_spec(j,3)(1:1) == '<') &
call calcvis_clv(model_param(j, &
7+(4*model_wldep(1)+model_wldep(2))*(nwave-1)))
if (keyw /= 'ld_param_prior') goto 96
end if
end do pass2
12 continue
close (11)
!is this a centrosymmetric model?
symm_model = .true.
do i = 1, size(model_param, 1)
!need all epsilon fixed at unity
ipar = 9 + (4*model_wldep(1) + model_wldep(2))*(nwave-1)
do j = 1, 1+model_wldep(3)*(nwave-1)
if (.not.((model_param(i,ipar+(j-1)*3)==1D0) &
.and.(model_prior(i,ipar+(j-1)*3)==0D0))) symm_model = .false.
end do
!and all r fixed at zero
do j = 1, 1+model_wldep(1)*(nwave-1)
if (.not.((model_param(i,2+(j-1)*4)==0D0) &
.and.(model_prior(i,2+(j-1)*4)==0D0))) symm_model = .false.
end do
end do
deallocate(wb) !free local storage
return
!error trapping
91 info = 'cannot open file '//trim(file_name)
return
92 info = 'cannot read from file '//trim(file_name)
close(11)
return
94 info = 'unknown read problem - possible invalid file format'
close (11)
return
95 info = 'unknown read problem whilst reading component data'
close (11)
return
96 info = 'invalid keyword or keyword order'
close (11)
return
100 info = 'illegal parameter value detected'
close (11)
return
end subroutine read_model
!============================================================================
!! Are variable parameters sensible?
function model_valid(info, force_symm)
logical :: model_valid
!function arguments
character(len=*), intent(out) :: info !! Error message if model invalid
logical, intent(in) :: force_symm !! True if expecting a symmetric model
!local variables
integer :: nvar, i, iwave, ipar
info = ''
model_valid = .true.
!must have at least 1 freedom to minimise with
call model_nvar(nvar)
if (nvar == 0) model_valid = .false.
!for single component model cannot vary r/theta (change in position
!only constant phase offset) or B (flux is normalised anyway)
if (size(model_param,1) == 1) then
do iwave = 1, nwave
if (model_prior(1, 2+4*model_wldep(1)*(iwave-1)) /= 0D0) &
model_valid = .false.
if (model_prior(1, 3+4*model_wldep(1)*(iwave-1)) /= 0D0) &
model_valid = .false.
if (model_prior(1, &
6+4*model_wldep(1)*(nwave-1)+model_wldep(2)*(iwave-1)) /= 0D0) &
model_valid = .false.
end do
end if
!for any component cannot vary theta if r is zero and not free to vary
!(position angle has no effect if position radius fixed at zero)
do i = 1, size(model_param,1)
do iwave = 1, nwave
ipar = 2+4*model_wldep(1)*(iwave-1)
if ((model_param(i,ipar)==0D0) .and. (model_prior(i,ipar)==0D0) &
.and. (model_prior(i,ipar+1)/=0D0)) model_valid = .false.
end do
end do
!for any component cannot vary phi if epsilon is unity and not free to vary
!(orientation is meaningless if ellipse reduced to circular disc)
do i = 1, size(model_param,1)
do iwave = 1, nwave
ipar = 7 + (4*model_wldep(1)+model_wldep(2))*(nwave-1) &
+ model_wldep(3)*(iwave-1)
if ((model_param(i,ipar+2)==1D0) .and. (model_prior(i,ipar+2)==0D0) &
.and. (model_prior(i,ipar+1)/=0D0)) model_valid = .false.
end do
end do
!doesn't make sense to have wavelength-dependent numerical CLV with
!wavelength-dependent diameter
if (allocated(clv_mvis)) then
if (size(clv_mvis, 2) > 1 .and. model_wldep(3) == 1) &
model_valid = .false.
end if
if (.not. model_valid) &
info = 'illegal freedom(s) in model'
!if centrosymmetric model is forced then must have
!eccentricity epsilon fixed to be unity and position radius fixed at zero
if (force_symm .and. .not. symm_model) then
model_valid = .false.
info = 'for vis/nvis data must have guaranteed symmetric model'
end if
end function model_valid
!============================================================================
!! Get number of variable parameters in model
subroutine model_nvar(nvar)
!subroutine arguments
integer, intent(out) :: nvar
!local variables
integer :: i, j
nvar = 0
do i = 1, size(model_param,1)
do j = 1, size(model_param,2)
if (model_prior(i,j) /= 0D0) nvar = nvar + 1
end do
end do
end subroutine model_nvar
!============================================================================
!! Extract locations and descriptions for variable model parameters
subroutine model_getvar(nvar, pos, desc)
!subroutine arguments
!! Number of variables, e.g. from model_nvar
integer, intent(in) :: nvar
!! pos(i,:) gives indices of i'th variable parameter in model_param
!! and model_prior arrays
integer, intent(out) :: pos(nvar,2)
!! Variable descriptions
character(len=model_desc_len), intent(out) :: desc(nvar)
!local variables
integer :: i, j, ivar
ivar = 0
do i = 1, size(model_param,1)
do j = 1, size(model_param,2)
if (model_prior(i,j) /= 0D0) then
ivar = ivar + 1
pos(ivar,1) = i
pos(ivar,2) = j
desc(ivar) = model_desc(i,j)
end if
end do
end do
end subroutine model_getvar
!============================================================================
!! Print model details
subroutine print_model()
!local variables
integer :: i, ipar, jpar, iwave, order
character(len=32), allocatable :: wb(:)
character(len=512) :: line1, line2, ch1, ch2
allocate(wb(nwave))
do iwave = 1, nwave
if (allocated(model_wb)) then
write(wb(iwave), '(a, f7.1, a, f6.1, a)') ' [', model_wb(iwave,1), &
'/', model_wb(iwave,2), ']'
else
write(wb(iwave), '(a, i3, a)') '[', iwave, ']'
end if
end do
do i = 1, size(model_param,1)
print *,' '
print *,'component:',i
print *,'name : ',trim(model_spec(i,1))
order = int(model_param(i,1))
if (model_spec(i,3)(1:1) == '<') then
print *,'LD type : ',trim(model_spec(i,3)), &
' :',nclv,'points'
else
print *,'LD type : ',trim(model_spec(i,3)), &
' of order',order
end if
print *,'shape : ',trim(model_spec(i,2))
!position
ipar = 6 + 4*model_wldep(1)*(nwave-1)
jpar = 7 + (4*model_wldep(1) + model_wldep(2))*(nwave-1)
print 10,'position :',real(model_param(i,2:ipar-1))
print 10,' prior:',real(model_prior(i,2:ipar-1))
!flux
line1 = 'flux :'
line2 = ' prior:'
do iwave = 1, nwave
if (model_wldep(2) == 1) then
line1 = trim(line1) // trim(wb(iwave)) // ':'
line2 = trim(line2) // trim(wb(iwave)) // ':'
end if
write (ch1, '(f10.3)') real(model_param(i,ipar+iwave-1))
write (ch2, '(f10.3)') real(model_prior(i,ipar+iwave-1))
line1 = trim(line1) // trim(ch1)
line2 = trim(line2) // trim(ch2)
if (model_wldep(2) == 0) exit
end do
print '(1x, a)', trim(line1)
print '(1x, a)', trim(line2)
!shape
ipar = jpar
line1 = 'shape par:'
line2 = ' prior:'
do iwave = 1, nwave
if (model_wldep(3) == 1) then
line1 = trim(line1) // trim(wb(iwave)) // ':'
line2 = trim(line2) // trim(wb(iwave)) // ':'
end if
write (ch1, '(3f10.3)') &
real(model_param(i,ipar+3*(iwave-1):ipar+3*(iwave-1)+2))
write (ch2, '(3f10.3)') &
real(model_prior(i,ipar+3*(iwave-1):ipar+3*(iwave-1)+2))
line1 = trim(line1) // trim(ch1)
line2 = trim(line2) // trim(ch2)
if (model_wldep(3) == 0) exit
end do
print '(1x, a)', trim(line1)
print '(1x, a)', trim(line2)
!ld params
if (order >= 1) then
if (trim(model_spec(i,3)) == 'two-layer') then
ipar = 10 + (4*model_wldep(1) + model_wldep(2) &
+ 3*model_wldep(3))*(nwave-1)
write (line1, '(a, 4f9.3)') 'LD params:', &
real(model_param(i,ipar:ipar+3))
write (line2, '(a, 4f9.3)') ' prior:', &
real(model_prior(i,ipar:ipar+3))
do iwave = 1, nwave
if (model_wldep(4) == 1) then
line1 = trim(line1) // trim(wb(iwave)) // ':'
line2 = trim(line2) // trim(wb(iwave)) // ':'
end if
write (ch1, '(f7.3)') real(model_param(i,ipar+3+iwave))
write (ch2, '(f7.3)') real(model_prior(i,ipar+3+iwave))
line1 = trim(line1) // trim(ch1)
line2 = trim(line2) // trim(ch2)
if (model_wldep(4) == 0) exit
end do
else
ipar = 10 + (4*model_wldep(1) + model_wldep(2) &
+ 3*model_wldep(3))*(nwave-1)
line1 = 'LD params:'
line2 = ' prior:'
do iwave = 1, nwave
if (model_wldep(4) == 1) then
line1 = trim(line1) // trim(wb(iwave)) // ':'
line2 = trim(line2) // trim(wb(iwave)) // ':'
end if
write (ch1, '(10f7.3)') real(model_param(i,ipar+max_order*(iwave-1):ipar+max_order*(iwave-1)+order-1))
write (ch2, '(10f7.3)') real(model_prior(i,ipar+max_order*(iwave-1):ipar+max_order*(iwave-1)+order-1))
line1 = trim(line1) // trim(ch1)
line2 = trim(line2) // trim(ch2)
if (model_wldep(4) == 0) exit
end do
end if
print '(1x, a)', trim(line1)
print '(1x, a)', trim(line2)