FluidResize.py 6.48 KB
Newer Older
Chichi Lalescu's avatar
Chichi Lalescu committed
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
#######################################################################
#                                                                     #
#  Copyright 2015 Max Planck Institute                                #
#                 for Dynamics and Self-Organization                  #
#                                                                     #
#  This file is part of bfps.                                         #
#                                                                     #
#  bfps 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.                             #
#                                                                     #
#  bfps 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 bfps.  If not, see <http://www.gnu.org/licenses/>       #
#                                                                     #
# Contact: Cristian.Lalescu@ds.mpg.de                                 #
#                                                                     #
#######################################################################


26

27
28
import os
import argparse
29
30
import numpy as np

31
32
33
import bfps
from ._fluid_base import _fluid_particle_base

34
class FluidResize(_fluid_particle_base):
35
36
37
38
    """This class is meant to resize snapshots of DNS states to new grids.
    Typical stuff for DNS of turbulence.
    It will become superfluous when HDF5 is used for field I/O.
    """
39
40
    def __init__(
            self,
41
            name = 'FluidResize-v' + bfps.__version__,
42
            work_dir = './',
43
            simname = 'test',
Cristian Lalescu's avatar
Cristian Lalescu committed
44
            fluid_precision = 'single',
45
            use_fftw_wisdom = False):
46
47
        _fluid_particle_base.__init__(
                self,
48
                name = name + '-' + fluid_precision,
49
                work_dir = work_dir,
50
                simname = simname,
Cristian Lalescu's avatar
Cristian Lalescu committed
51
                dtype = fluid_precision,
52
                use_fftw_wisdom = use_fftw_wisdom)
53
        self.parameters['src_simname'] = 'test'
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        self.parameters['dst_iter'] = 0
        self.parameters['dst_nx'] = 32
        self.parameters['dst_ny'] = 32
        self.parameters['dst_nz'] = 32
        self.parameters['dst_simname'] = 'new_test'
        self.parameters['dst_dkx'] = 1.0
        self.parameters['dst_dky'] = 1.0
        self.parameters['dst_dkz'] = 1.0
        self.fill_up_fluid_code()
        self.finalize_code()
        return None
    def fill_up_fluid_code(self):
        self.fluid_includes += '#include <cstring>\n'
        self.fluid_includes += '#include "fftw_tools.hpp"\n'
        self.fluid_variables += ('double t;\n' +
Chichi Lalescu's avatar
Chichi Lalescu committed
69
                                 'fluid_solver<' + self.C_dtype + '> *fs0, *fs1;\n')
70
71
72
        self.fluid_start += """
                //begincpp
                char fname[512];
73
                fs0 = new fluid_solver<{0}>(
74
                        src_simname,
75
76
                        nx, ny, nz,
                        dkx, dky, dkz);
77
                fs1 = new fluid_solver<{0}>(
78
79
80
                        dst_simname,
                        dst_nx, dst_ny, dst_nz,
                        dst_dkx, dst_dky, dst_dkz);
Chichi Lalescu's avatar
Chichi Lalescu committed
81
                fs0->iteration = iteration;
82
                fs1->iteration = 0;
83
                DEBUG_MSG("about to read field\\n");
84
                fs0->read('v', 'c');
85
                DEBUG_MSG("field read, about to copy data\\n");
86
                double a, b;
87
                fs0->compute_velocity(fs0->cvorticity);
88
89
                a = 0.5*fs0->autocorrel(fs0->cvelocity);
                b = 0.5*fs0->autocorrel(fs0->cvorticity);
90
                DEBUG_MSG("old field %d %g %g\\n", fs0->iteration, a, b);
91
92
93
                copy_complex_array<{0}>(fs0->cd, fs0->cvorticity,
                                        fs1->cd, fs1->cvorticity,
                                        3);
94
                DEBUG_MSG("data copied, about to write new field\\n");
95
                fs1->write('v', 'c');
96
97
                DEBUG_MSG("finished writing\\n");
                fs1->compute_velocity(fs1->cvorticity);
98
99
                a = 0.5*fs1->autocorrel(fs1->cvelocity);
                b = 0.5*fs1->autocorrel(fs1->cvorticity);
100
101
                DEBUG_MSG("new field %d %g %g\\n", fs1->iteration, a, b);
                //endcpp
Chichi Lalescu's avatar
Chichi Lalescu committed
102
                """.format(self.C_dtype)
103
104
105
106
107
108
109
        self.fluid_end += """
                //begincpp
                delete fs0;
                delete fs1;
                //endcpp
                """
        return None
110
    def specific_parser_arguments(
111
112
            self,
            parser):
113
        _fluid_particle_base.specific_parser_arguments(self, parser)
114
        parser.add_argument(
115
                '-m',
116
                type = int,
117
                dest = 'm',
118
                default = 32,
119
120
                metavar = 'M',
                help = 'resize from N to M')
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        parser.add_argument(
                '--src_wd',
                type = str,
                dest = 'src_work_dir',
                required = True)
        parser.add_argument(
                '--src_iteration',
                type = int,
                dest = 'src_iteration',
                required = True)
        return None
    def launch(
            self,
            args = [],
            **kwargs):
136
        opt = self.prepare_launch(args)
137
138
139
140
        cmd_line_pars = vars(opt)
        for k in ['dst_nx', 'dst_ny', 'dst_nz']:
            if type(cmd_line_pars[k]) == type(None):
                cmd_line_pars[k] = opt.m
141
        self.parameters['niter_todo'] = 0
142
143
        self.pars_from_namespace(opt)
        src_file = os.path.join(
144
                os.path.realpath(opt.src_work_dir),
145
146
147
148
149
150
                opt.src_simname + '_cvorticity_i{0:0>5x}'.format(opt.src_iteration))
        read_file = os.path.join(
                self.work_dir,
                opt.src_simname + '_cvorticity_i{0:0>5x}'.format(opt.src_iteration))
        self.set_host_info(bfps.host_info)
        self.write_par(iter0 = opt.src_iteration)
Cristian Lalescu's avatar
Cristian Lalescu committed
151
152
        if not os.path.exists(read_file):
            os.symlink(src_file, read_file)
153
        self.run(ncpu = opt.ncpu)
154
        return None
155