Commit 741ad419 authored by Chichi Lalescu's avatar Chichi Lalescu
Browse files

put work_dir and simname in base; renew resize code

parent 653f5543
......@@ -32,7 +32,10 @@ class NavierStokes(bfps.fluid_base.fluid_particle_base):
name = 'NavierStokes',
work_dir = './',
simname = 'test'):
super(NavierStokes, self).__init__(name = name, work_dir = work_dir, simname = simname)
super(NavierStokes, self).__init__(
name = name,
work_dir = work_dir,
simname = simname)
self.fill_up_fluid_code()
self.style = {}
return None
......@@ -379,13 +382,12 @@ def launch(
c.add_particles(kcut = 'fs->kM/2')
c.finalize_code()
c.write_src()
c.write_par(simname = c.simname)
c.write_par()
if opt.run:
if opt.iteration == 0 and opt.initialize:
c.generate_initial_condition()
for nrun in range(opt.njobs):
c.run(ncpu = opt.ncpu,
simname = 'test',
iter0 = opt.iteration + nrun*opt.nsteps)
return c
......
......@@ -46,7 +46,8 @@ install_info = pickle.load(
'r'))
from .code import code
from .fluid_converter import fluid_converter
from .test_io import test_io
from .fluid_converter import fluid_converter
from .fluid_resize import fluid_resize
from .NavierStokes import NavierStokes
......@@ -23,14 +23,18 @@
import os
class base(object):
def __init__(self):
def __init__(
self,
work_dir = './',
simname = 'test'):
self.iorank = 0
### simulation parameters
self.parameters = {'nx' : 32,
'ny' : 32,
'nz' : 32}
self.string_length = 512
self.work_dir = './'
self.work_dir = work_dir
self.simname = simname
return None
def cdef_pars(self):
key = self.parameters.keys()
......@@ -96,8 +100,8 @@ class base(object):
else:
src_txt += 'DEBUG_MSG("'+ key[i] + ' = %le\\n", ' + key[i] + ');\n'
return src_txt
def write_par(self, simname = 'test'):
filename = simname + '_pars.txt'
def write_par(self):
filename = self.simname + '_pars.txt'
if not os.path.isdir(self.work_dir):
os.makedirs(self.work_dir)
ofile = open(os.path.join(self.work_dir, filename), 'w')
......@@ -110,7 +114,7 @@ class base(object):
ofile.write('{0} = {1}\n'.format(key[i], self.parameters[key[i]]))
ofile.close()
return None
def read_par(self, simname = 'test'):
def read_parameters(self):
def read_value(s):
try:
return int(s)
......@@ -119,7 +123,7 @@ class base(object):
return float(s)
except ValueError:
return str(s)
ifile = open(simname + '_pars.txt', 'r')
ifile = open(os.path.join(self.work_dir, self.simname + '_pars.txt'), 'r')
for line in ifile:
a = line.split()
if len(a)==3 and a[1] == '=':
......
......@@ -27,8 +27,11 @@ import shutil
import pickle
class code(base):
def __init__(self):
super(code, self).__init__()
def __init__(
self,
work_dir = './',
simname = 'test'):
super(code, self).__init__(work_dir = work_dir, simname = simname)
self.version_message = ('/***********************************************************************\n' +
'* this code automatically generated by bfps\n' +
'* version {0}\n'.format(bfps.__version__) +
......@@ -124,7 +127,6 @@ class code(base):
return None
def run(self,
ncpu = 2,
simname = 'test',
iter0 = 0,
out_file = 'out_file',
err_file = 'err_file',
......@@ -145,13 +147,13 @@ class code(base):
'-np',
'{0}'.format(ncpu),
'./' + self.name,
simname,
self.simname,
'{0}'.format(iter0)]
if self.host_info['type'] == 'cluster':
job_name_list = []
for j in range(njobs):
suffix = simname + '_{0}.sh'.format(iter0 + j*self.parameters['niter_todo'])
qsub_script_name = 'run_' + suffix
suffix = self.simname + '_{0}'.format(iter0 + j*self.parameters['niter_todo'])
qsub_script_name = 'run_' + suffix + '.sh'
self.write_sge_file(
file_name = os.path.join(self.work_dir, qsub_script_name),
nprocesses = ncpu,
......@@ -172,8 +174,8 @@ class code(base):
os.chdir(self.work_dir)
os.environ['LD_LIBRARY_PATH'] += ':{0}'.format(bfps.lib_dir)
subprocess.call(command_atoms,
stdout = open(out_file + '_' + simname, 'w'),
stderr = open(err_file + '_' + simname, 'w'))
stdout = open(out_file + '_' + self.simname, 'w'),
stderr = open(err_file + '_' + self.simname, 'w'))
os.chdir(current_dir)
return None
def write_sge_file(
......
......@@ -32,11 +32,11 @@ class fluid_particle_base(bfps.code):
name = 'solver',
work_dir = './',
simname = 'test'):
super(fluid_particle_base, self).__init__()
self.work_dir = work_dir
self.simname = simname
self.particle_species = 0
super(fluid_particle_base, self).__init__(
work_dir = work_dir,
simname = simname)
self.name = name
self.particle_species = 0
self.parameters['dkx'] = 1.0
self.parameters['dky'] = 1.0
self.parameters['dkz'] = 1.0
......@@ -88,19 +88,6 @@ class fluid_particle_base(bfps.code):
self.main += self.particle_end
self.main += self.fluid_end
return None
def read_parameters(
self,
simname = None,
work_dir = None):
if not type(simname) == type(None):
self.simname = simname
if not type(work_dir) == type(None):
self.work_dir = work_dir
current_dir = os.getcwd()
os.chdir(self.work_dir)
self.read_par(self.simname)
os.chdir(current_dir)
return None
def read_rfield(
self,
field = 'velocity',
......
......@@ -32,7 +32,10 @@ class fluid_converter(bfps.fluid_base.fluid_particle_base):
name = 'fluid_converter',
work_dir = './',
simname = 'test'):
super(fluid_converter, self).__init__(name = name, work_dir = work_dir, simname = simname)
super(fluid_converter, self).__init__(
name = name,
work_dir = work_dir,
simname = simname)
self.parameters['write_rvelocity'] = 1
self.parameters['write_rvorticity'] = 1
self.parameters['fluid_name'] = 'test'
......
########################################################################
#
# Copyright 2015 Max Planck Institute for Dynamics and SelfOrganization
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Contact: Cristian.Lalescu@ds.mpg.de
#
########################################################################
import bfps
import bfps.fluid_base
class fluid_resize(bfps.fluid_base.fluid_particle_base):
def __init__(
self,
name = 'fluid_converter',
work_dir = './',
simname = 'test'):
super(fluid_resize, self).__init__(
name = name,
work_dir = work_dir,
simname = simname)
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' +
'fluid_solver<float> *fs0, *fs1;\n')
self.fluid_start += """
//begincpp
char fname[512];
fs0 = new fluid_solver<float>(
simname,
nx, ny, nz,
dkx, dky, dkz);
fs1 = new fluid_solver<float>(
dst_simname,
dst_nx, dst_ny, dst_nz,
dst_dkx, dst_dky, dst_dkz);
fs0->iteration = iter0;
fs1->iteration = 0;
fs0->read('v', 'c');
double a, b;
a = 0.5*fs0->correl_vec(fs0->cvelocity, fs0->cvelocity);
b = 0.5*fs0->correl_vec(fs0->cvorticity, fs0->cvorticity);
DEBUG_MSG("old field %d %g %g\\n", fs0->iteration, a, b);
copy_complex_array(fs0->cd, fs0->cvorticity,
fs1->cd, fs1->cvorticity,
3);
fs1->write('v', 'c');
a = 0.5*fs1->correl_vec(fs1->cvelocity, fs1->cvelocity);
b = 0.5*fs1->correl_vec(fs1->cvorticity, fs1->cvorticity);
DEBUG_MSG("new field %d %g %g\\n", fs1->iteration, a, b);
niter_todo = 0;
//endcpp
"""
self.fluid_end += """
//begincpp
delete fs0;
delete fs1;
//endcpp
"""
return None
########################################################################
#
# Copyright 2015 Max Planck Institute for Dynamics and SelfOrganization
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Contact: Cristian.Lalescu@ds.mpg.de
#
########################################################################
import bfps
import bfps.code
import bfps.tools
import numpy as np
import pickle
import os
class vorticity_resize(bfps.code):
def __init__(
self,
name = 'vorticity_resize',
work_dir = './'):
super(vorticity_resize, self).__init__()
self.work_dir = work_dir
self.particle_species = 0
self.name = name
self.parameters['dkx'] = 1.0
self.parameters['dky'] = 1.0
self.parameters['dkz'] = 1.0
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_code()
return None
def fill_up_code(self):
self.includes = '#include "fluid_solver.hpp"\n'
self.variables += self.cdef_pars()
self.definitions+= self.cread_pars()
self.includes += '#include <cstring>\n'
self.includes += '#include "fftw_tools.hpp"\n'
self.variables += 'fluid_solver<float> *fs0, *fs1;\n'
self.main = """
//begincpp
char fname[512];
fs0 = new fluid_solver<float>(
simname,
nx, ny, nz,
dkx, dky, dkz);
fs1 = new fluid_solver<float>(
dst_simname,
dst_nx, dst_ny, dst_nz,
dst_dkx, dst_dky, dst_dkz);
fs0->iteration = iter0;
fs1->iteration = 0;
fs0->read('v', 'c');
fs0->dealias(fs0->cvorticity, 3);
fs0->force_divfree(fs0->cvorticity);
fs0->symmetrize(fs0->cvorticity, 3);
fs0->write('v', 'r');
fs0->write('u', 'r');
double a, b;
a = 0.5*fs0->correl_vec(fs0->cvelocity, fs0->cvelocity);
b = 0.5*fs0->correl_vec(fs0->cvorticity, fs0->cvorticity);
DEBUG_MSG("old field %d %g %g\\n", fs0->iteration, a, b);
copy_complex_array(fs0->cd, fs0->cvorticity,
fs1->cd, fs1->cvorticity,
3);
fs1->dealias(fs1->cvorticity, 3);
fs1->force_divfree(fs1->cvorticity);
fs1->symmetrize(fs1->cvorticity, 3);
fs1->write('v', 'c');
fs1->write('v', 'r');
fs1->write('u', 'r');
a = 0.5*fs1->correl_vec(fs1->cvelocity, fs1->cvelocity);
b = 0.5*fs1->correl_vec(fs1->cvorticity, fs1->cvorticity);
DEBUG_MSG("new field %d %g %g\\n", fs1->iteration, a, b);
delete fs0;
delete fs1;
//endcpp
"""
return None
def plot_vel_cut(
self,
axis,
simname = 'test',
field = 'velocity',
iteration = 0,
yval = 13,
filename = None):
axis.set_axis_off()
if type(filename) == type(None):
filename = os.path.join(self.work_dir, simname + '_' + field + '_i{0:0>5x}'.format(iteration))
Rdata0 = np.fromfile(
filename,
dtype = np.float32).reshape((-1,
self.parameters['ny'],
self.parameters['nx'], 3))
energy = np.sum(Rdata0[:, yval, :, :]**2, axis = 2)*.5
axis.imshow(energy, interpolation='none')
axis.set_title('{0} {1}'.format(field,
np.average(Rdata0[..., 0]**2 +
Rdata0[..., 1]**2 +
Rdata0[..., 2]**2)*.5))
return Rdata0
def generate_vector_field(
self,
rseed = 7547,
spectra_slope = 1.,
precision = 'single',
simname = None,
iteration = 0):
if precision == 'single':
dtype = np.complex64
elif precision == 'double':
dtype = np.complex128
np.random.seed(rseed)
Kdata00 = bfps.tools.generate_data_3D(
self.parameters['nz']/2,
self.parameters['ny']/2,
self.parameters['nx']/2,
p = spectra_slope).astype(dtype)
Kdata01 = bfps.tools.generate_data_3D(
self.parameters['nz']/2,
self.parameters['ny']/2,
self.parameters['nx']/2,
p = spectra_slope).astype(dtype)
Kdata02 = bfps.tools.generate_data_3D(
self.parameters['nz']/2,
self.parameters['ny']/2,
self.parameters['nx']/2,
p = spectra_slope).astype(dtype)
Kdata0 = np.zeros(
Kdata00.shape + (3,),
Kdata00.dtype)
Kdata0[..., 0] = Kdata00
Kdata0[..., 1] = Kdata01
Kdata0[..., 2] = Kdata02
Kdata1 = bfps.tools.padd_with_zeros(
Kdata0,
self.parameters['nz'],
self.parameters['ny'],
self.parameters['nx'])
if not (type(simname) == type(None)):
Kdata1.tofile(
os.path.join(self.work_dir,
simname + "_cvorticity_i{0:0>5x}".format(iteration)))
return Kdata1
def generate_tracer_state(
self,
rseed = 34982,
simname = None,
iteration = 0,
species = 0):
np.random.seed(rseed*self.particle_species + species)
data = np.random.random(self.parameters['nparticles']*3)*2*np.pi
if not (type(simname) == type(None)):
data.tofile(
os.path.join(
self.work_dir,
simname + "_tracers{0}_state_i{1:0>5x}".format(species, iteration)))
return data
def read_spec(
self,
simname = 'test',
field = 'velocity'):
k = np.fromfile(
os.path.join(
self.work_dir,
simname + '_kshell'),
dtype = np.float64)
spec_dtype = np.dtype([('iteration', np.int32),
('val', np.float64, k.shape[0])])
spec = np.fromfile(
os.path.join(
self.work_dir,
simname + '_' + field + '_spec'),
dtype = spec_dtype)
return k, spec
def read_stats(
self, simname = 'test'):
dtype = pickle.load(open(
os.path.join(self.work_dir, self.name + '_dtype.pickle'), 'r'))
return np.fromfile(os.path.join(self.work_dir, simname + '_stats.bin'),
dtype = dtype)
import subprocess
import matplotlib.pyplot as plt
def double(opt):
if opt.run or opt.clean:
subprocess.call(['rm {0}/test_*'.format(opt.work_dir)], shell = True)
subprocess.call(['rm {0}/*.pickle'.format(opt.work_dir)], shell = True)
subprocess.call(['rm {0}/*.elf'.format(opt.work_dir)], shell = True)
subprocess.call(['rm {0}/*version_info.txt'.format(opt.work_dir)], shell = True)
if opt.clean:
subprocess.call(['make', 'clean'])
return None
c = vorticity_resize(work_dir = opt.work_dir)
c.parameters['nx'] = opt.n
c.parameters['ny'] = opt.n
c.parameters['nz'] = opt.n
c.parameters['dst_nx'] = 2*opt.n
c.parameters['dst_ny'] = 2*opt.n
c.parameters['dst_nz'] = 2*opt.n
c.parameters['dst_simname'] = 'test2'
c.write_src()
c.write_par(simname = 'test1')
if opt.run:
#c.generate_vector_field(simname = 'test1')
c.run(ncpu = opt.ncpu,
simname = 'test1')
fig = plt.figure(figsize=(12, 12))
a = fig.add_subplot(221)
c.plot_vel_cut(
a,
simname = 'test1',
field = 'rvorticity',
iteration = 0,
yval = 13)
a = fig.add_subplot(222)
c.plot_vel_cut(
a,
simname = 'test1',
field = 'rvelocity',
iteration = 0,
yval = 13)
c.parameters['nx'] *= 2
c.parameters['ny'] *= 2
c.parameters['nz'] *= 2
a = fig.add_subplot(223)
c.plot_vel_cut(
a,
simname = 'test2',
field = 'rvorticity',
iteration = 0,
yval = 26)
a = fig.add_subplot(224)
c.plot_vel_cut(
a,
simname = 'test2',
field = 'rvelocity',
iteration = 0,
yval = 26)
fig.savefig('resize.pdf', format = 'pdf')
return None
......@@ -30,7 +30,7 @@ import pickle
import os
import bfps
from bfps.resize import vorticity_resize
from bfps import fluid_resize
def Kolmogorov_flow_test_broken(opt):
c = convergence_test(name = 'Kflow_test')
......@@ -52,9 +52,9 @@ def Kolmogorov_flow_test_broken(opt):
Kdata0[..., 1] = Kdata01
Kdata0[..., 2] = Kdata02
c.write_src()
c.write_par(simname = 'test')
c.write_par()
Kdata0.tofile("test_cvorticity_i00000")
c.run(ncpu = opt.ncpu, simname = 'test')
c.run(ncpu = opt.ncpu)
Rdata = np.fromfile(
'test_rvorticity_i00000',
dtype = np.float32).reshape(opt.n, opt.n, opt.n, 3)
......@@ -71,23 +71,19 @@ def Kolmogorov_flow_test_broken(opt):
fig = plt.figure(figsize=(12, 12))
a = fig.add_subplot(221)
c.plot_vel_cut(a,
simname = 'test',
field = 'velocity',
iteration = 0)
a = fig.add_subplot(222)
a.set_axis_off()
c.plot_vel_cut(a,
simname = 'test',
field = 'vorticity',
iteration = 0)
a = fig.add_subplot(223)
ufin = c.plot_vel_cut(a,
simname = 'test',
field = 'velocity',
iteration = stats.shape[0]-1)
a = fig.add_subplot(224)
vfin = c.plot_vel_cut(a,
simname = 'test',
field = 'vorticity',
iteration = stats.shape[0]-1)
fig.savefig('field_cut.pdf', format = 'pdf')
......@@ -134,7 +130,9 @@ def Kolmogorov_flow_test_broken(opt):
def double(opt):
old_simname = 'N{0:0>3x}'.format(opt.n)
new_simname = 'N{0:0>3x}'.format(opt.n*2)
c = vorticity_resize(work_dir = opt.work_dir + '/resize')
c = fluid_resize(
work_dir = opt.work_dir + '/resize',
simname = old_simname)
c.parameters['nx'] = opt.n
c.parameters['ny'] = opt.n
c.parameters['nz'] = opt.n
......@@ -146,13 +144,14 @@ def double(opt):
c.set_host_info({'type' : 'pc'})
if not os.path.isdir(os.path.join(opt.work_dir, 'resize')):
os.mkdir(os.path.join(opt.work_dir, 'resize'))
c.write_par(simname = old_simname)
c.write_par()
cp_command = ('cp {0}/test_cvorticity_i{1:0>5x} {2}/{3}_cvorticity_i{1:0>5x}'.format(
opt.work_dir + '/' + old_simname, opt.iteration, opt.work_dir + '/resize', old_simname))
subprocess.call([cp_command], shell = True)
c.run(ncpu = opt.ncpu,
simname = old_simname,
iter0 = opt.iteration)
iter0 = opt.iteration,
err_file = 'err_' + old_simname + '_' + new_simname,
out_file = 'out_' + old_simname + '_' + new_simname)
if not os.path.isdir(os.path.join(opt.work_dir, new_simname)):
os.mkdir(os.path.join(opt.work_dir, new_simname))
cp_command = ('cp {2}/{3}_cvorticity_i{1:0>5x} {0}/test_cvorticity_i{1:0>5x}'.format(
......@@ -192,7 +191,7 @@ def NSlaunch(
c.add_particles(integration_steps = 6)
c.finalize_code()
c.write_src()
c.write_par(simname = c.simname)
c.write_par()
c.set_host_info({'type' : 'pc'})
if opt.run:
if opt.iteration == 0 and opt.initial