V3FIT
vmec_history.f
1 !*******************************************************************************
2 ! File vmec_history.f
3 ! Contains module vmec_history
4 
5 !*******************************************************************************
6 ! MODULE vmec_history
7 ! (history of a vmec run)
8 ! SECTION I. Variable Declarations
9 ! SECTION II. Subroutines
10 ! SECTION III. Comments - version history
11 !*******************************************************************************
12 
13  MODULE vmec_history
14 
15 !*******************************************************************************
16 ! SECTION I. Variable Declarations
17 !*******************************************************************************
18 
19 !-------------------------------------------------------------------------------
20 ! Type declarations - lengths of reals, integers, and complexes.
21 ! Frequently used mathematical constants, lots of extra precision.
22 ! Make type declarations and constants Private, so there are no conflicts.
23 !-------------------------------------------------------------------------------
24 
25  USE stel_kinds, only : rprec
26  USE safe_open_mod
27  USE xstuff
28 
29  IMPLICIT NONE
30 
31  PRIVATE rprec
32 
33 !-------------------------------------------------------------------------------
34 ! Module Variables - Scalars
35 !-------------------------------------------------------------------------------
36 ! vmh_dim Length of History Arrays
37 ! vmh_index index to the history arrays, and call counter
38 ! vmh_save_i1 Integer stored value. Value set with subroutine
39 ! vmec_history_set. Used by V3FIT for reconstruction
40 ! iteration number
41 ! vmh_save_i2 Integer stored value. Value set with subroutine
42 ! vmec_history_set. Used by V3FIT for jacobian
43 ! calculation loop (reconstruction parameter number)
44 ! vmh_print_flag Logical to control printing
45 ! Print Flag Usage - Now (2010-08-12)
46 ! 1) Initialized to .False.
47 ! 2) vmec_history_print not called from VMEC
48 ! 3) Flag changed to .TRUE. in V3FIT
49 ! 4) vmec_history_print called at end of V3FIT run
50 ! Print Flag Usage - Future - If want VMEC run alone to also call vmec_history_print
51 ! 1) Initialized to .TRUE.
52 ! 2) vmec_history_print from VMEC
53 ! 3) When run from V3FIT
54 ! a) have V3FIT change flag to .False. at start
55 ! Will need to fix up eq_interface
56 ! b) change flag to .TRUE. at end, and call vmec_history_print
57 ! vmh_time_zero Real variable to store the initial result of the second0 call
58 
59  INTEGER, PARAMETER :: vmh_dim = 100000
60  INTEGER :: vmh_index = 0
61  INTEGER :: vmh_save_i1 = - 1
62  INTEGER :: vmh_save_i2 = - 1
63 ! LOGICAL :: vmh_print_flag = .TRUE.
64  LOGICAL :: vmh_print_flag = .false.
65  REAL(rprec) :: vmh_time_zero = 0
66  PRIVATE vmh_dim, vmh_index, vmh_save_i1, vmh_save_i2, &
67  & vmh_print_flag
68 
69 !-------------------------------------------------------------------------------
70 ! Module Variables - Integer arrays
71 !-------------------------------------------------------------------------------
72 ! vmh_iterc VMEC's iterc
73 ! vmh_iter2m1 VMEC's iter2 - iter1
74 ! vmh_ns VMEC's ns
75 ! vmh_nvacskip VMEC's nvacskip
76 ! vmh_ivac VMEC's ivac
77 ! vmh_ictrl_prec2d VMEC's ictrl_prec2d
78 ! vmh_i1 V3FIT, reconstruction iteration number
79 ! vmh_i2 V3FIT, jacobian calculation, reconstruction parameter #
80 
81  INTEGER, DIMENSION(vmh_dim) :: vmh_iterc, vmh_iter2m1, vmh_ns, &
82  & vmh_nvacskip, vmh_ivac, vmh_ictrl_prec2d, vmh_i1, vmh_i2
83 
84 !-------------------------------------------------------------------------------
85 ! Module Variables - Real arrays
86 !-------------------------------------------------------------------------------
87 ! vmh_fsq- Convergence diagnostics
88 ! vmh_time_step VMEC delt0 values
89 ! vmh_time SYSTEM_TIME, via LIBSTELL's second0 function
90 
91  REAL(rprec), DIMENSION(vmh_dim) :: vmh_time_step, vmh_fsqr, &
92  & vmh_fsqz, vmh_fsql, vmh_fedge, vmh_time
93 
94 !*******************************************************************************
95 ! SECTION II. Subroutines
96 !*******************************************************************************
97  CONTAINS
98 
99 !*******************************************************************************
100 !*******************************************************************************
101 ! vmec_history_store
102 ! subroutine to store local vmec and variables into the history arrays.
103 ! Should be called ONLY from subroutine eqsolve, right after iterc
104 ! is incremented.
105 !-------------------------------------------------------------------------------
106 
107  SUBROUTINE vmec_history_store(time_step)
108 
109 !-------------------------------------------------------------------------------
110 ! USE statements
111 !-------------------------------------------------------------------------------
112 
113  USE vmec_main, ONLY: iter1, iter2, iterc, fsqr, fsqz, fsql, &
114  & fedge, ivac
115  USE vmec_dim, ONLY: ns
116  USE vmec_input, ONLY: nvacskip
117  USE precon2d, ONLY: ictrl_prec2d
118 
119 !-------------------------------------------------------------------------------
120 ! ARGUMENT declaration
121 !-------------------------------------------------------------------------------
122  REAL(rprec), INTENT(in) :: time_step
123 ! delt0 is a local variable in runvmec
124 ! runvmec: CALL eqsolve(.,delt0,...)
125 ! dummy argument of eqsolve is called delt0
126 ! eqsolve: CALL vmec_history_store(delt0)
127 ! eqsolve: CALL evolve(delt0,...)
128 ! dummy argument of evolve is time_step
129 
130 !-------------------------------------------------------------------------------
131 ! Local Variables
132 !-------------------------------------------------------------------------------
133  REAL(rprec) :: time_now
134 
135 !-------------------------------------------------------------------------------
136 ! Start of executable code
137 !-------------------------------------------------------------------------------
138  IF (vmh_index .eq. 0) THEN
139  CALL second0(vmh_time_zero)
140  ENDIF
141  vmh_index = vmh_index + 1
142  IF (vmh_index .le. vmh_dim) THEN
143  vmh_iterc(vmh_index) = iterc
144  vmh_iter2m1(vmh_index) = iter2 - iter1
145  vmh_ns(vmh_index) = ns
146  vmh_nvacskip(vmh_index) = nvacskip
147  vmh_ivac(vmh_index) = ivac
148  vmh_ictrl_prec2d(vmh_index) = ictrl_prec2d
149  vmh_i1(vmh_index) = vmh_save_i1
150  vmh_i2(vmh_index) = vmh_save_i2
151  vmh_time_step(vmh_index) = time_step
152  vmh_fsqr(vmh_index) = fsqr
153  vmh_fsqz(vmh_index) = fsqz
154  vmh_fsql(vmh_index) = fsql
155  vmh_fedge(vmh_index) = fedge
156  CALL second0(time_now)
157  vmh_time(vmh_index) = time_now - vmh_time_zero
158  ENDIF
159  RETURN
160  END SUBROUTINE vmec_history_store
161 
162 !*******************************************************************************
163 !*******************************************************************************
164 ! vmec_history_print
165 ! subroutine to print out the accumulated history
166 !-------------------------------------------------------------------------------
167 
168  SUBROUTINE vmec_history_print
169 
170 !-------------------------------------------------------------------------------
171 ! USE statements
172 !-------------------------------------------------------------------------------
173  USE vmec_input, ONLY: input_extension
174 
175 !-------------------------------------------------------------------------------
176 ! Local Variables
177 !-------------------------------------------------------------------------------
178  INTEGER :: vmh_iou = 73
179  INTEGER :: istat, i
180  CHARACTER(LEN=120) :: vmh_history_file_name
181  CHARACTER(LEN=80) :: vmh_format2 =
182  & '(3(i5,1x),i4,1x,i3,1x,i5,1x,3(i3,1x),7(2x,es9.2))'
183  CHARACTER(LEN=150) :: vmh_header
184 
185 !-------------------------------------------------------------------------------
186 ! Start of executable code
187 !-------------------------------------------------------------------------------
188  IF (.NOT. vmh_print_flag) RETURN
189 
190  vmh_history_file_name = trim('vmec_history.' // input_extension)
191  CALL safe_open(vmh_iou,istat,trim(vmh_history_file_name), &
192  & 'replace','formatted',delim_in='none',record_in=150)
193  IF (istat .ne. 0) THEN
194  WRITE(*,*) 'In subroutine vmec_history_print: Error from'
195  WRITE(*,*) 'call to safe_open. istat = ', istat
196  stop ' (source file vmec_history.f)'
197  ENDIF
198 
199  WRITE(vmh_iou,*) 'History arrays are dimensioned ',vmh_dim
200  WRITE(vmh_iou,*) 'Subroutine vmec_history_store was called ', &
201  & vmh_index, ' times'
202  WRITE(vmh_iou,*)
203 
204  vmh_header = ' i iterc 2m1 ns nvac ivac ictrl_ i1 i2' // &
205  & ' time_step fsqr fsqz fsql max(fsq)' // &
206  & ' fedge sys-time'
207  WRITE(vmh_iou,*) trim(vmh_header)
208  WRITE(vmh_iou,*) ' skip prec2d'
209 
210  DO i = 1,min(vmh_index,vmh_dim)
211  WRITE(vmh_iou,vmh_format2) &
212  & i, vmh_iterc(i), vmh_iter2m1(i), vmh_ns(i), &
213  & vmh_nvacskip(i), vmh_ivac(i), &
214  & vmh_ictrl_prec2d(i), vmh_i1(i), vmh_i2(i), &
215  & vmh_time_step(i), vmh_fsqr(i), vmh_fsqz(i), vmh_fsql(i), &
216  & max(vmh_fsqr(i),vmh_fsqz(i),vmh_fsql(i)), &
217  & vmh_fedge(i), vmh_time(i)
218  END DO
219 
220  RETURN
221  END SUBROUTINE vmec_history_print
222 
223 !*******************************************************************************
224 !*******************************************************************************
225 ! vmec_history_set
226 ! Subroutine to set values of vmh_save_i1 and/or vmh_save_i2
227 !-------------------------------------------------------------------------------
228 
229  SUBROUTINE vmec_history_set(i1,i2)
230 
231 ! Declare Arguments
232  INTEGER, OPTIONAL :: i1, i2
233 
234 ! Start of executable code
235 
236  IF (PRESENT(i1)) vmh_save_i1 = i1
237  IF (PRESENT(i2)) vmh_save_i2 = i2
238 
239  RETURN
240  END SUBROUTINE vmec_history_set
241 
242 !*******************************************************************************
243 !*******************************************************************************
244 ! vmec_history_get
245 ! Subroutine to get values of vmh_save_i1 and/or vmh_save_i2
246 !-------------------------------------------------------------------------------
247 
248  SUBROUTINE vmec_history_get(i1,i2)
249 
250 ! Declare Arguments
251  INTEGER :: i1, i2
252 
253 ! Start of executable code
254 
255  i1 = vmh_save_i1
256  i2 = vmh_save_i2
257 
258  RETURN
259  END SUBROUTINE vmec_history_get
260 
261 !*******************************************************************************
262 !*******************************************************************************
263 ! vmec_history_print_flag_off
264 ! Subroutine to turn on the print flag.
265 !-------------------------------------------------------------------------------
266 
267  SUBROUTINE vmec_history_print_flag_off
268  vmh_print_flag = .false.
269  RETURN
270  END SUBROUTINE vmec_history_print_flag_off
271 
272 !*******************************************************************************
273 !*******************************************************************************
274 ! vmec_history_print_flag_on
275 ! Subroutine to turn off the print flag.
276 !-------------------------------------------------------------------------------
277 
278  SUBROUTINE vmec_history_print_flag_on
279  vmh_print_flag = .true.
280  RETURN
281  END SUBROUTINE vmec_history_print_flag_on
282 
283  END MODULE vmec_history
284 
285 !*******************************************************************************
286 ! SECTION III. Comments - version history
287 !*******************************************************************************
288 !
289 ! JDH 07-12-2006. First version.
290 ! Module to store history information about a vmec run
291 !
292 ! 07-24-2006 JDH
293 ! Changed so that iter_ha is index of last assigned value in history arrays.
294 ! So, iter_ha increment happens BEFORE assignments in vmec_history_store.
295 !
296 ! 2010-08-03 JDH
297 ! Significant Revisions, added more variables to store. Still needs V3F interface
298 ! Use variable prefix vmh_
299 ! 2010-08-05 JDH Add mechanism to turn off and on the printing
300 ! 2010-08-10 JDH Add time_step
301 ! 2010-08-12 JDH Add vmec_history_get, clean up headings, comments.
302 ! 2011-02-08 JDH Added vmh_fsq and vmh_fedge
303 ! 2011-02-15 JDH Removed vmh_fsq
304 ! 2011-02-18 JDH Added vmh_time - system time via LIBSTELL's second0
305 ! 2011-02-19 JDH Fixed up issues with vmh_time
306 ! 2012-06-20 JDH Changed iter2 to iterc for cumulative counter
307 
308 
309