Commit 9641fdd3 authored by frey_m's avatar frey_m
Browse files

AMR test case: Remove Python plotting scripts and move them to a new repository.

New repostiory: git@gitlab.psi.ch:frey_m/AMR-Tools.git
		(or https://gitlab.psi.ch/frey_m/AMR-Tools.git)

deleted:    ippl/test/AMR/amr_plot_tools/field.py
deleted:    ippl/test/AMR/amr_plot_tools/tools.py
deleted:    ippl/test/AMR/amr_plot_tools/tracking_vis.py
deleted:    ippl/test/AMR/amr_plot_tools/visualize.py
parent fce112a9
##
# @file field.py
# @author Matthias Frey
# @date 4. Jan. 2017
# @version 1.0
# @brief Plot vector and scalar fields
# @details When configuring OPAL with the flag
# -DDBG_SCALARFIELD=1 it dumps the
# scalar and vector fields of every
# time step to "data" directory of the
# simulation. These are visualized by
# this script.\n
# Call: python field.py [file] \n
# where [file] either *field*
# or *scalar*
#
from tools import match, doGridPlot
import sys
import numpy as np
# regular expression pattern, where parentheses () represent groups that can be accessed later
# group(0) is whole match
# format: i j k ( x-component , y-component , z-component )
# where i, j and k are the grid points
vector_field_pattern = r'(\d*) (\d*) (\d*) \( (-*.*\d*) \, (-*.*\d*) \, (-*.*\d*) \)'
# format: i j k value
# where i, j and k are the grid points
scalar_field_pattern = r'(\d*) (\d*) (\d*) (-*.*\d*)'
try:
f = sys.argv[1]
if 'field' in f:
data = match(f, vector_field_pattern)
# electric field in horizontal direction
mid = int(0.5 * max(data[:, 2]))
condition = (data[:, 2] == mid)
i = data[condition, 0]
j = data[condition, 1]
val = data[condition, 3]
plt = doGridPlot(i, j, val, "horizontal grid", "vertical grid", r'$E_x$ [V/m]')
plt.savefig(f + '_slice_z_Ex.png')#, bbox_inches='tight')
print ("max. Ex: ", max(val))
print ("min. Ex: ", min(val))
# electric field in vertical direction
val = np.extract(condition, data[:, 4])
plt = doGridPlot(i, j, val, "horizontal grid", "vertical grid", r'$E_y$ [V/m]')
plt.savefig(f + '_slice_z_Ey.png')#bbox_inches='tight')
print ("max. Ey: ", max(val))
print ("min. Ey: ", min(val))
# center in z
mid = int(0.5 * max(data[:, 0]))
condition = (data[:, 0] == mid)
i = data[condition, 1]
j = data[condition, 2]
val = data[condition, 5]
plt = doGridPlot(i, j, val, "vertical grid", "longitudinal grid", r'$E_z$ [V/m]')
plt.savefig(f + '_slice_x_Ez.png')# bbox_inches='tight')
print ("max. Ez: ", max(val))
print ("min. Ez: ", min(val))
elif 'scalar' in f:
data = match(f, scalar_field_pattern)
clab = ''
if 'phi' in f:
clab = r'$\phi$ [V]'
elif 'rho' in f:
clab = r'$\rho$ [C/m**3]'
# center in x
mid = int(0.5 * max(data[:, 0]))
condition = (data[:, 0] == mid)
i = data[condition, 1]
j = data[condition, 2]
val = data[condition, 3]
plt = doGridPlot(i, j, val, "vertical grid", "longitudinal grid", clab)
plt.savefig(f + '_slice_x.png')#, bbox_inches='tight')
# center in y
mid = int(0.5 * max(data[:, 1]))
condition = (data[:, 1] == mid)
i = data[condition, 0]
j = data[condition, 2]
val = data[condition, 3]
plt = doGridPlot(i, j, val, "horizontal grid", "longitudinal grid", clab)
plt.savefig(f + '_slice_y.png')#bbox_inches='tight')
# center in z
mid = int(0.5 * max(data[:, 2]))
condition = (data[:, 2] == mid)
i = data[condition, 0]
j = data[condition, 1]
val = data[condition, 3]
plt = doGridPlot(i, j, val, "horizontal grid", "vertical grid", clab)
plt.savefig(f + '_slice_z.png')# bbox_inches='tight')
print ("max. value (slice z): ", max(val))
print ("min. value (slice z): ", min(val))
else:
raise RuntimeError('This type of file is not supported.')
except Exception as e:
print (e.strerror)
\ No newline at end of file
##
# @file tools.py
# @author Matthias Frey
# @date 22. Dec. 2016
# @version 1.0
# @brief This module contains several helper functions
# for data analysis
#
import yt
import os
import re
import numpy as np
import matplotlib.pyplot as plt
##
# @param ds is the data
# @param direct is the direction 'x', 'y' or 'z' (normal)
# @param field to plot
# @param unit the data should be converted to (otherwise it
# takes the default given by the data)
# @param zfactor is the zoom factor (default: 1, i.e. no zoom)
# @param col is the color for the time stamp and scale annotation
def doSlicePlot(ds, direct, field, unit, zfactor = 1, col = 'white'):
slc = yt.SlicePlot(ds, normal=direct, fields=field)
if unit is not None:
slc.set_unit(field, unit)
slc.zoom(zfactor)
slc.annotate_grids()
slc.annotate_timestamp(corner='upper_left', redshift=False, draw_inset_box=True)
slc.annotate_scale(corner='upper_right', size_bar_args={'color':col})
slc.save()
##
# @param ds is the data
# @param direct is the direction 'x', 'y' or 'z' (normal)
# @param field to plot
# @param unit the data should be converted to (otherwise it
# takes the default given by the data)
# @param zfactor is the zoom factor (default: 1, i.e. no zoom)
# @param col is the color for the time stamp and scale annotation
def doProjectionPlot(ds, direct, field, unit, zfactor = 1, col = 'white'):
slc = yt.ProjectionPlot(ds, direct, fields=field)
if unit is not None:
slc.set_unit(field, unit)
slc.zoom(zfactor)
slc.annotate_grids()
slc.annotate_timestamp(corner='upper_left', redshift=False, draw_inset_box=True)
slc.annotate_scale(corner='upper_right', size_bar_args={'color':col})
slc.save()
##
# Take an integer and transform it
# to a string of four characters, e.g.\n
# \f$ 1 \rightarrow 0001 \f$ \n
# \f$ 12 \rightarrow 0012 \f$ \n
# \f$ 586 \rightarrow 0586 \f$ \n
# @param step is an integer
def concatenate(step):
res = str(step)
while len(res) < 4:
res = '0' + res
return res
##
# Count subdirectories
# @param parent is the path to the parent directory
# @param substr specifies the substring that should be contained
# @returns the number of subdirectories containing
# a given substring
def countSubdirs(parent, substr):
nDirs = 0
# 22. Dec. 2016
# http://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory
for filename in os.listdir(parent):
if substr in filename:
nDirs = nDirs + 1
return nDirs
##
# Read field data written by OPAL (-DDBG_SCALARFIELD=1) using
# regular expression
# @param f is the file that is read line by line
# @param pattern is a string specifying rule for matching
# @returns a matrix where each row is matched line in the file
def match(f, pattern):
regex = re.compile(pattern)
data = np.empty((0, regex.groups))
# go through file line by line and match
with open(f) as ff:
for line in ff:
res = regex.match(line)
row = []
for i in range(1, regex.groups + 1):
row = np.append(row, [float(res.group(i))])
data = np.append(data, np.array([row]), axis=0)
return data
##
def doGridPlot(i, j, val, xlab, ylab, clab):
imin = int(i[0])
imax = int(i[-1])
jmin = int(j[0])
jmax = int(j[-1])
i = np.reshape(i, (imax, jmax))
j = np.reshape(j, (imax, jmax))
val = np.reshape(val, (imax, jmax))
plt.figure()
plt.pcolor(i, j, val, cmap='YlGnBu')
plt.xlim(imin, imax)
plt.xlabel(xlab)
plt.ylim(imin, jmax)
plt.ylabel(ylab)
plt.colorbar(label=clab)
return plt
##
# @file tracking_vis.py
# @author Matthias Frey
# @date 14. October 2016, LBNL
# @version 1.1 (22. Dec. 2016)
# @brief Plot the electric self-field, density and self-field
# potential using the yt framework.
# @details It reads in the all generated BoxLib plotfiles "plt_amr_*"
# (* are four digits) of a directory and saves the content as slice
# plots. It reads in the output of testReal.cpp.
# Make sure you sourced the yt directory $YT_DIR/yt-x86_64/bin/activate.
#
import os
import sys
import yt
from tools import countSubdirs, concatenate, doSlicePlot
try:
opal = os.environ['OPAL_BUILD']
# leading name of plotfiles
substr = 'plt_amr_'
nFiles = countSubdirs(opal + '/ippl/test/AMR/', substr)
print ('Found ' + str(nFiles) + ' plotfiles')
for i in range(0, nFiles):
# do a string of four characters, e.g. 12 --> 0012
res = concatenate(i)
ds = yt.load(opal + '/ippl/test/AMR/' + substr + res, unit_system='mks')
doSlicePlot(ds, 'z', 'rho', 'C/m**3', 'gray')
doSlicePlot(ds, 'z', 'potential', 'V')
doSlicePlot(ds, 'z', 'Ex', 'V/m')
doSlicePlot(ds, 'z', 'Ey', 'V/m')
doSlicePlot(ds, 'x', 'Ez', 'V/m')
except KeyError:
print ("Please export the environment variable 'OPAL_BUILD'.")
except IOError as e:
print (e.strerror)
except TypeError:
print ("No subdirectories containing the given substring " +
"found in " + opal + "'/ippl/test/AMR/'.")
\ No newline at end of file
##
# @file visualize.py
# @author Matthias Frey
# @date 14. October 2016, LBNL
# @version 1.1 (21. Dec. 2016)
#
# @pre Environment variable OPAL_BUILD has to be set. Python >= 2.7
# @details Plot the electric self-field, density and self-field
# potential using the yt framework.
# 1. mpirun -np #cores testSolver
# 2. python visualize.py (make sure you sourced the yt directory $YT_DIR/yt-x86_64/bin/activate)
# @brief Slice plots of plotfiles generated by writePlotFile.H
import os
import yt
import argparse
from tools import doSlicePlot, doProjectionPlot
try:
parser = argparse.ArgumentParser(description='Visualize BoxLib grid data.')
parser.add_argument('--zoom',
help='zoom factor of images (default: 1)',
default=1,
type=int,
nargs=1)
args = parser.parse_args()
zoom = args.zoom
opal = os.environ['OPAL_BUILD']
ds = yt.load(opal + "ippl/test/AMR/plt0000", dataset_type='opal')
ds.print_stats()
print ("Field list:", ds.field_list)
print ("Derived field list:", ds.derived_field_list)
doSlicePlot(ds, 'z', 'rho', 'C/m**3', zoom, 'gray')
doSlicePlot(ds, 'y', 'rho', 'C/m**3', zoom, 'gray')
doSlicePlot(ds, 'x', 'rho', 'C/m**3', zoom, 'gray')
doProjectionPlot(ds, 'x', 'rho', 'C/m**2', zoom, 'gray')
doProjectionPlot(ds, 'y', 'rho', 'C/m**2', zoom, 'gray')
doProjectionPlot(ds, 'z', 'rho', 'C/m**2', zoom, 'gray')
doSlicePlot(ds, 'z', 'Ex', 'V/m', zoom)
doSlicePlot(ds, 'z', 'Ey', 'V/m', zoom)
doSlicePlot(ds, 'x', 'Ez', 'V/m', zoom)
doSlicePlot(ds, 'z', 'potential', 'V', zoom)
doSlicePlot(ds, 'y', 'potential', 'V', zoom)
doSlicePlot(ds, 'x', 'potential', 'V', zoom)
ad = ds.all_data()
print ( ad.quantities.extrema("rho").in_units('C/m**3') )
print ( ad.quantities.extrema("Ex").in_units('V/m') )
print ( ad.quantities.extrema("potential").in_units('V') )
except KeyError:
print ("Please export the environment variable 'OPAL_BUILD'.")
except IOError as e:
print (e.strerror)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment