Skip to content

Commit

Permalink
add particleshapedemo.py
Browse files Browse the repository at this point in the history
this file is also included in the tests.
  • Loading branch information
skuschel committed Mar 11, 2015
1 parent 157cf25 commit 554ec10
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
78 changes: 78 additions & 0 deletions examples/particleshapedemo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python2
#
# This file is part of postpic.
#
# postpic 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.
#
# postpic 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 postpic. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright Stephan Kuschel 2015
#

'''
This is a demonstration file to show the differences between various particles
shapes used.
'''

import numpy as np
import postpic as pp

# postpic will use matplotlib for plotting. Changing matplotlibs backend
# to "Agg" makes it possible to save plots without a display attached.
# This is necessary to run this example within the "run-tests" script
# on travis-ci.
import matplotlib; matplotlib.use('Agg')


# choose the dummy reader. This reader will create fake data for testing.
pp.chooseCode('dummy')

# Create a dummy reader with 300 particles, not initialized with a seed and use
# uniform distribution
dr = pp.readDump(300, seed=None, randfunc=np.random.random)
# set and create directory for pictures.
savedir = '_examplepictures/'
import os
if not os.path.exists(savedir):
os.mkdir(savedir)

# initialze the plotter object.
# project name will be prepended to all output names
plotter = pp.plotting.plottercls(dr, outdir=savedir, autosave=True, project='particleshapedemo')

# we will need a refrence to the ParticleAnalyzer quite often
from postpic import ParticleAnalyzer as PA

# create ParticleAnalyzer for every particle species that exists.
pas = [PA(dr, s) for s in dr.listSpecies()]


# --- 1D ---
if True:
pa = pas[0]
plotargs = {'ylim': (0,1600), 'log10plot': False}

# 1 particle per cell
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 300, 'order': 0}, title='1ppc_order0', rangex=(0,1)), **plotargs)
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 300, 'order': 1}, title='1ppc_order1', rangex=(0,1)), **plotargs)

# 3 particles per cell
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 100, 'order': 0}, title='3ppc_order0', rangex=(0,1)), **plotargs)
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 100, 'order': 1}, title='3ppc_order1', rangex=(0,1)), **plotargs)

# 10 particles per cell
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 30, 'order': 0}, title='10ppc_order0', rangex=(0,1)), **plotargs)
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 30, 'order': 1}, title='10ppc_order1', rangex=(0,1)), **plotargs)




22 changes: 18 additions & 4 deletions examples/simpleexample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
#!/usr/bin/env python2
#
# This file is part of postpic.
#
# postpic 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.
#
# postpic 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 postpic. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright Stephan Kuschel 2015
#

import numpy as np
import postpic as pp
Expand Down Expand Up @@ -83,8 +101,4 @@ def r(pa):
# use the plotter with the particle scalars defined above.
plotter.plotField(pa.createField(r, p_r, optargsh={'bins':[400,400]})) # plot 12

# show what particle shapes can do
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 100, 'order': 0}), ylim=(3e0, 2e5))
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 100, 'order': 1}), ylim=(3e0, 2e5))


11 changes: 6 additions & 5 deletions postpic/datareader/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ class Dummyreader(Dumpreader_ifc):
will pretend to have dumpid many particles).
'''

def __init__(self, dumpid, dimensions=2, **kwargs):
def __init__(self, dumpid, dimensions=2, randfunc=np.random.normal, seed=0, **kwargs):
super(self.__class__, self).__init__(dumpid, **kwargs)
self._dimensions = dimensions
# initialize fake data
np.random.seed(0)
self._xdata = np.random.normal(size=dumpid)
if seed is not None:
np.random.seed(seed)
self._xdata = randfunc(size=dumpid)
if dimensions > 1:
self._ydata = np.random.normal(size=dumpid)
self._ydata = randfunc(size=dumpid)
if dimensions > 2:
self._zdata = np.random.normal(size=dumpid)
self._zdata = randfunc(size=dumpid)

def keys(self):
pass
Expand Down
3 changes: 2 additions & 1 deletion run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def main():

cmds = ['pep8 postpic --statistics --count --show-source '
'--ignore=W391,E123,E226,E24 --max-line-length=99',
os.path.join('examples', 'simpleexample.py')]
os.path.join('examples', 'simpleexample.py'),
os.path.join('examples', 'particleshapedemo.py')]
for cmd in cmds:
print('===== running next command =====')
print(cmd)
Expand Down

0 comments on commit 554ec10

Please sign in to comment.