run.py 5.29 KB
Newer Older
Aurel Neic's avatar
Aurel Neic committed
1
#!/usr/bin/env python3
2
3
4
5

"""
Demonstration and testing of checkpointing and restarting CARP.
"""
6
import os
7
8
9

EXAMPLE_DESCRIPTIVE_NAME = 'Checkpoint and Restart'
EXAMPLE_AUTHOR = 'Gernot Plank <gernot.plank@medunigraz.at>'
10
EXAMPLE_DIR = os.path.dirname(__file__)
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

from datetime import date

from carputils import settings
from carputils import tools
from carputils import mesh
from carputils import testing

def parser():
    parser = tools.standard_parser()

    parser.add_argument('--experiment',
                        default='checkpoint',
                        choices=['checkpoint',
                                 'restart' ],
                        help='define experiment type')
    parser.add_argument('--np_restart',
                        type=int,
                        help='number of processors when restarting (default: '
                            +'same as checkpoint simulation)')
    return parser

def jobID(args):
    """
    Generate name of top level output directory.
    """
    today = date.today()
    jobID = '{}_{}_{}_np{}'.format(today.isoformat(), args.experiment,
                                   args.flv, args.np)
    if args.experiment == 'restart' and args.np_restart is not None:
        jobID += '_nprst{}'.format(args.np_restart)
    return jobID

@tools.carpexample(parser, jobID)
def run(args, job):

    # Restart np defaults to checkpoint np
    if args.np_restart is None:
        args.np_restart = args.np

    # Generate mesh
    # Block which is thin in z direction
    geom = mesh.Block(size=(5, 5, 0.1))
    # Set fibre angle to 0, sheet angle to 0
    geom.set_fibres(0, 0, 90, 90)

    # Define cylinder region
    reg = mesh.CylinderRegion(start=(0,0,-0.05), axis=(0,0,0.1), 
                              radius=2.5, tag=2)
    geom.add_region(reg)

    # Generate and return base name
    meshname = mesh.generate(geom)

    # define default view settings
66
    view = os.path.join(EXAMPLE_DIR, 'checkpoint.mshz')
67
68
69
70
71
72
73
74
75
 
    # Simulation output directories
    chkID = job.ID
    # Restarting? Put in subdirectories
    if args.experiment == 'restart':
        chkID = os.path.join(job.ID, 'checkpoint')
        rstID = os.path.join(job.ID, 'restart')

    # Add all the non-general arguments
76
    cmd  = tools.carp_cmd(os.path.join(EXAMPLE_DIR, 'checkpoint.par'))
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
    cmd += ['-meshname', meshname]

    if args.visualize:
        cmd += ['-gridout_i', 3,
                '-gridout_e', 3]
   
    # Run checkpoint simulation
    chk_cmd = cmd + ['-simID', chkID]
    job.carp(chk_cmd)

    start_statef = []
    if args.experiment == 'restart':
        start_statef = ['-start_statef',
                        os.path.join(chkID, 'state.chkpt.5.00')]

    if args.experiment == 'restart':
        # Overwrite nprocs
        args.np = args.np_restart
        # Run same command, but with restarting from checkpoint
        rst_cmd = cmd + ['-simID',        rstID,
                         '-start_statef', os.path.join(chkID, 'state.chkpt.5.00'),
                         '-num_tsav',     0]
        job.carp(rst_cmd)

    # Do visualization
    if args.visualize and not settings.platform.BATCH:

        geom = os.path.join(chkID, os.path.basename(meshname)+'_i')
        data = os.path.join(chkID, 'vm.igb')
106
        view = os.path.join(EXAMPLE_DIR, 'checkpoint.mshz')
107
108
109
110
111
112
113
        
        job.gunzip(data)
        job.meshalyzer(geom, data, view)

        if args.experiment == 'restart':
            geom = os.path.join(rstID, os.path.basename(meshname)+'_i')
            data = os.path.join(rstID, 'vm.igb')
114
            view = os.path.join(EXAMPLE_DIR, 'restart.mshz')
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
        
            job.gunzip(data)
            job.meshalyzer(geom, data, view)


# ---SERIAL---------------------------------------------------------------------
test_restart = testing.Test('restart', 
                            run, ['--experiment', 'restart' ],
                            tags=[testing.tag.FAST, testing.tag.SERIAL])
test_restart.add_filecmp_check('restart/vm.igb.gz', testing.max_error, 0.001)
# ---
test_checkpoint = testing.Test('checkpoint', run, 
                              ['--experiment', 'checkpoint'],
                              tags=[testing.tag.FAST, testing.tag.SERIAL])
test_checkpoint.add_filecmp_check('vm.igb.gz', testing.max_error, 0.001)
# ---PARALLEL-------------------------------------------------------------------
test_restart_parallel = testing.Test('restart_parallel', run, 
                                    ['--experiment', 'restart', '--np', '2'],
                                     refdir='restart', 
                                     tags=[testing.tag.SHORT, testing.tag.PARALLEL])
test_restart_parallel.add_filecmp_check('restart/vm.igb.gz', testing.max_error, 0.001)
test_restart_parallel.disable_reference_generation()
# ---
test_checkpoint_parallel = testing.Test('checkpoint_parallel', run, 
                                       ['--experiment', 'checkpoint', '--np', '2'],
                                        refdir='checkpoint', 
                                        tags=[testing.tag.SHORT, testing.tag.PARALLEL])
test_checkpoint_parallel.add_filecmp_check('vm.igb.gz', testing.max_error, 0.001)
test_checkpoint_parallel.disable_reference_generation()

__tests__ = [test_restart, test_restart_parallel, 
             test_checkpoint, test_checkpoint_parallel]

if __name__ == '__main__':
    run()