DNS.py 6.28 KB
Newer Older
Cristian Lalescu's avatar
Cristian 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
#######################################################################
#                                                                     #
#  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                                 #
#                                                                     #
#######################################################################



import os
import sys
import shutil
import subprocess
import argparse
import h5py
import math
import numpy as np
import warnings

import bfps
from ._code import _code

class DNS(_code):
    """This class is meant to stitch together the C++ code into a final source file,
    compile it, and handle all job launching.
    """
    def __init__(
            self,
            work_dir = './',
            simname = 'test',
            dns_type = 'NSVE',
            fluid_dtype = 'single'):
        _code.__init__(self, work_dir = work_dir, simname = simname)
        self.dns_type = dns_type
        self.name = self.dns_type + '-v' + bfps.__version__
        if fluid_dtype in [np.float32, np.float64]:
            self.fluid_dtype = fluid_dtype
        elif fluid_dtype in ['single', 'double']:
            if fluid_dtype == 'single':
                self.fluid_dtype = np.dtype(np.float32)
            elif fluid_dtype == 'double':
                self.fluid_dtype = np.dtype(np.float64)
        self.rtype = self.fluid_dtype
        if self.rtype == np.float32:
            self.ctype = np.dtype(np.complex64)
            self.C_dtype = 'float'
        elif self.rtype == np.float64:
            self.ctype = np.dtype(np.complex128)
            self.C_dtype = 'double'
        if self.dns_type == 'NSVE':
            self.parameters['dealias_type'] = 1
            self.parameters['dkx'] = 1.0
            self.parameters['dky'] = 1.0
            self.parameters['dkz'] = 1.0
            self.parameters['niter_todo'] = 8
            self.parameters['niter_part'] = 1
            self.parameters['niter_stat'] = 1
            self.parameters['niter_out'] = 1024
            self.parameters['nparticles'] = 0
            self.parameters['dt'] = 0.01
            self.parameters['nu'] = float(0.1)
            self.parameters['fmode'] = 1
            self.parameters['famplitude'] = float(0.5)
            self.parameters['fk0'] = float(2.0)
            self.parameters['fk1'] = float(4.0)
            self.parameters['forcing_type'] = 'linear'
            self.parameters['histogram_bins'] = int(256)
            self.parameters['max_velocity_estimate'] = float(1)
            self.parameters['max_vorticity_estimate'] = float(1)
            self.parameters['checkpoints_per_file'] = int(1)
        self.version_message = (
                '/***********************************************************************\n' +
                '* this code automatically generated by bfps\n' +
                '* version {0}\n'.format(bfps.__version__) +
                '***********************************************************************/\n\n\n')
        self.includes = """
                //begincpp
                #include "base.hpp"
                #include "scope_timer.hpp"
                #include "fftw_interface.hpp"
                #include "full_code/main_code.hpp"
                #include <iostream>
                #include <hdf5.h>
                #include <string>
                #include <cstring>
                #include <fftw3-mpi.h>
                #include <omp.h>
                #include <cfenv>
                #include <cstdlib>
                //endcpp
                """
        self.includes += '#include "full_code/{0}.hpp"\n'.format(self.dns_type)
        self.variables = ''
        self.definitions = ''
        self.main = """
                int main(int argc, char *argv[])
                {{
                    bool fpe = (
                        (getenv("BFPS_FPE_OFF") == nullptr) ||
                        (getenv("BFPS_FPE_OFF") != std::string("TRUE")));
                    return main_code<{0}>(argc, argv, fpe);
                }}
                """.format(self.dns_type + '<{0}>'.format(self.C_dtype))
        self.host_info = {'type'        : 'cluster',
                          'environment' : None,
                          'deltanprocs' : 1,
                          'queue'       : '',
                          'mail_address': '',
                          'mail_events' : None}
        return None
    def write_src(self):
        with open(self.name + '.cpp', 'w') as outfile:
            outfile.write(self.version_message)
            outfile.write(self.includes)
            outfile.write(self.cread_pars(
                template_class = 'NSVE<rnumber>::',
                template_prefix = 'template <typename rnumber> ',
                simname_variable = 'simname.c_str()'))
            for rnumber in ['float', 'double']:
                outfile.write(self.cread_pars(
                    template_class = 'NSVE<{0}>::'.format(rnumber),
                    template_prefix = 'template '.format(rnumber),
                    just_declaration = True))
            outfile.write(self.main)
        return None