File: checkpointf.F

package info (click to toggle)
sprng 2.0a-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,308 kB
  • sloc: ansic: 30,353; fortran: 1,618; makefile: 575; cpp: 58; sh: 5
file content (82 lines) | stat: -rw-r--r-- 2,854 bytes parent folder | download | duplicates (9)
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
C/* new_spring version  5/25/99 Chris S.  List of changes: 1 of 1           */
C/* adding readin in of a generator type */

C/****************************************************************************/
C/*                ____Demonstrates checkpointing____                        */
C/* In a new run, this program initializes a random number stream and prints */
C/* a few random numbers. Finally it packs the state of the stream into a    */
C/* file. In a continuation of an old run, this program reads in the state of*/
C/* a stream from a file, prints a few random numbers, and stores the final  */
C/* state of the stream into a file.                                         */
C/****************************************************************************/
C

C Uncomment the following line to get the interface with pointer checking
C #define CHECK_POINTERS

       program checkpointf
       implicit none

#include "sprng_f.h"

       integer streamnum, nstreams, seed
       SPRNG_POINTER stream
       real*8 rn
       integer i, size, junk
       character buffer1(MAX_PACKED_LENGTH), buffer2(MAX_PACKED_LENGTH)
       character outfile*7, infile*7, firstChar*1

C--- reading in a generator type
       integer gtype
#include "genf_types_menu.h"
       print *,'Type in a generator type (integers: 0,1,2,3,4,5):  '
       read *, gtype
C---

       streamnum = 0
       nstreams = 1
       seed = 985456376

C/*********************** Initialize streams *******************************/
C===========================================
C outfile  -- state.X ; infile  -- state.X
C===========================================
       print*, 'Enter file name(length 7) to store final state:'
       read(*, 111) outfile    ! 7 characters please
       print*, 'Enter file name(length 7) to read from:'
       print*, '         (enter 9 for a new run)'
       read(*, 112) infile     ! 7 characters please
       write(firstChar, 113) infile

 111   format(A7)
 112   format(A7)
 113   format(A1)

       if (firstChar .eq. '9') then  ! initialize stream the first time  
       stream = init_sprng(gtype,streamnum,nstreams,seed,SPRNG_DEFAULT)
       else                          ! read stream state from the old file
          open(30, file =infile , status = 'old', form = 'unformatted')
          read(30) size
          read(30) buffer1
          stream = unpack_sprng(buffer1)
          close(30)
       endif
       
       print *, 'Printing 5 random numbers in [0,1): '
       do 100 i = 1, 5
          rn = sprng(stream)
          write(6,150) i, rn
 100   continue

 150      format(i1, "  ", f8.6)

       size = pack_sprng(stream, buffer2)
       open(31, file = outfile, status = 'unknown', 
     &      form = 'unformatted')
       write(31) size
       write(31) buffer2
       close(31)

       junk = free_sprng(stream)

       end