FluidResize.py 6.47 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',
42
            work_dir = './',
43
            simname = 'test',
44
45
            dtype = np.float32,
            use_fftw_wisdom = False):
46
47
        _fluid_particle_base.__init__(
                self,
48
49
                name = name,
                work_dir = work_dir,
50
                simname = simname,
51
52
                dtype = dtype,
                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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        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):
        parser = argparse.ArgumentParser('bfps ' + type(self).__name__)
        self.add_parser_arguments(parser)
        opt = parser.parse_args(args)
        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
        self.pars_from_namespace(opt)
        src_file = os.path.join(
                opt.src_work_dir,
                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))
        if not os.path.exists(read_file):
            os.symlink(src_file, read_file)
        self.set_host_info(bfps.host_info)
        self.write_par(iter0 = opt.src_iteration)
        self.run(ncpu = opt.ncpu)
155
        return None
156