[cig-commits] [commit] master: py: Use configparser to read settings from Par_file. (c7ef3f2)
cig_noreply at geodynamics.org
cig_noreply at geodynamics.org
Wed Jan 7 07:56:30 PST 2015
Repository : https://github.com/geodynamics/specfem1d
On branch : master
Link : https://github.com/geodynamics/specfem1d/compare/5ac1833074897dbc9cc286395ddae214ea0452e3...c63d455e0ff0075e43cb487147ab0bc101d4019c
>---------------------------------------------------------------
commit c7ef3f2db4888570ed9208334aced63c24ff8283
Author: Elliott Sales de Andrade <esalesde at physics.utoronto.ca>
Date: Tue Jan 6 17:02:36 2015 -0500
py: Use configparser to read settings from Par_file.
>---------------------------------------------------------------
c7ef3f2db4888570ed9208334aced63c24ff8283
Python_version/Par_file | 55 +++++++++
Python_version/defines.py | 185 +++++++++++++++++-------------
Python_version/functions.py | 2 +-
Python_version/main_for_Python_version.py | 4 +-
4 files changed, 163 insertions(+), 83 deletions(-)
diff --git a/Python_version/Par_file b/Python_version/Par_file
new file mode 100644
index 0000000..eec3492
--- /dev/null
+++ b/Python_version/Par_file
@@ -0,0 +1,55 @@
+AXISYM = True
+
+####################
+# Grid description #
+####################
+
+# Grid's type
+GRID_TYPE = 'homogeneous'
+# "Physical" length of the domain (in meters)
+LENGTH = 3000
+# kg/m^3
+DENSITY = 2500
+# Pa
+RIGIDITY = 30000000000
+GRID_FILE = 'grid_homogeneous.txt'
+TICKS_FILE = 'ticks_homogeneous.txt'
+
+
+######################
+# Source description #
+######################
+
+# Duration of the source in dt
+TSOURCE = 100
+# GLL point number on which the source is situated
+ISOURCE = 0
+# Maximum amplitude
+MAX_AMPL = 1e7
+# Source's type
+SOURCE_TYPE = 'ricker'
+
+DECAY_RATE = 2.628
+
+
+###################
+# Other constants #
+###################
+
+# Number of elements
+NSPEC = 250
+# Degree of the basis functions
+N = 4
+# Degree of basis functions in the first element
+# For the moment NGLJ need to be = N
+NGLJ = %(N)s
+# Number of time steps
+NTS = 20000
+# Courant CFL number
+CFL = 0.45
+
+#########
+# Plots #
+#########
+# One image is displayed each DPLOT time step
+DPLOT = 10
diff --git a/Python_version/defines.py b/Python_version/defines.py
index e9d25ff..611fcc5 100644
--- a/Python_version/defines.py
+++ b/Python_version/defines.py
@@ -12,37 +12,13 @@ is implemented.
@author: Alexis Bottero (alexis.bottero at gmail.com)
"""
-### THIS PART CAN BE MODIFIED -> ###
-AXISYM=True
-# Grid description
-GRID_TYPE='homogeneous' # Grid's type
-LENGTH=3000 # "Physical" length of the domain (in meters)
-DENSITY=2500 #kg/m^3
-RIGIDITY=30000000000 #Pa
-GRID_FILE='grid_homogeneous.txt'
-TICKS_FILE='ticks_homogeneous.txt'
-
-# Source description
-TSOURCE=100 #500. # Duration of the source in dt
-ISOURCE=0 #501 #501 #501 #501 #501 # GLL point number on which the source is situated
-MAX_AMPL=1e7 # Maximum amplitude
-SOURCE_TYPE='ricker' # Source's type
-DECAY_RATE=2.628 #10 #2.628
-
-# Other constants
-NSPEC=250 # Number of elements
-N=4 # Degree of the basis functions
-NGLJ=N # Degree of basis functions in the first element
-# For the moment NGLJ need to be = N
-NTS=20000 # Number of time steps
-CFL=0.45 # Courant CFL number
-
-# Plots
-DPLOT=10 # One image is displayed each DPLOT time step
-### <- THIS PART CAN BE MODIFIED ###
-
-### THIS PART IS NOT SUPPOSED TO BE MODIFIED -> ###
-# --- MODULES AND PACKAGES ---
+try:
+ # Python 3
+ from configparser import SafeConfigParser
+except ImportError:
+ # Python 2
+ from ConfigParser import SafeConfigParser
+
import numpy as np # NumPy (multidimensional arrays, linear algebra, ...)
import scipy as sp # SciPy (signal and image processing library)
import matplotlib as mpl # Matplotlib (2D/3D plotting library)
@@ -52,6 +28,7 @@ from pylab import * # Matplotlib's pylab interface
# --- FUNCTIONS --- #
import functions # Contains fundamental functions
+
# Gauss Lobatto Legendre points and integration weights
ksiGLL = {
4: np.array([-1, -0.6546536707, 0, 0.6546536707, 1]),
@@ -97,57 +74,110 @@ wGLJ = {
}
-try:
- ksiGLL = ksiGLL[N]
- wGLL = wGLL[N]
-except KeyError:
- print >> sys.stderr, 'ERROR: N = %d is invalid!' % (N, )
- sys.exit()
-
-try:
- ksiGLJ = ksiGLJ[NGLJ]
- wGLJ = wGLJ[NGLJ]
-except KeyError:
- print >> sys.stderr, 'ERROR: NGLJ = %d is invalid!' % (NGLJ, )
- sys.exit()
+class FakeGlobalSectionHead(object):
+ def __init__(self, fp):
+ self.fp = fp
+ self.sechead = '[global]\n'
+ def readline(self):
+ if self.sechead:
+ try:
+ return self.sechead
+ finally:
+ self.sechead = None
+ else:
+ return self.fp.readline()
-class Parameter:
+class Parameter(object):
"""Contains all the constants and parameters necessary for 1D spectral
element simulation"""
def __init__(self):
"""Init"""
- self.axisym=AXISYM # True if axisymmetry
- self.length=LENGTH # "Physical" length of the domain (in meters)
- self.nSpec=NSPEC # Number of elements
- self.N=N # Degree of the basis functions
- self.NGLJ=NGLJ # Degree of the basis functions in the first element
- self.nGLL=self.N+1 # Number of GLL points per elements
- self.nGLJ=self.NGLJ+1 # Number of GLJ in the first element
- self.nGlob=(self.nSpec-1)*self.N+self.NGLJ+1 # Number of points in the array
- self.ibool=functions.globalArray(self.nSpec,self.nGLL) # Global array TODO add GLJ
- self.nts=NTS # Number of time steps
- self.cfl=CFL # Courant CFL number
- self.dt=0 # Time step (will be updated)
- #Grid description
- self.gridType=GRID_TYPE # Grid's type
- self.meanRho=DENSITY
- self.meanMu=RIGIDITY
- # Source description :
- self.tSource=TSOURCE # Duration of the source in dt
- self.iSource=ISOURCE # GLL point number on which the source is situated
- self.maxAmpl=MAX_AMPL # Maximum amplitude
- self.sourceType=SOURCE_TYPE # Source's type
- self.decayRate=DECAY_RATE # Decay rate for the ricker
+ cp = SafeConfigParser(defaults={
+ # True if axial symmetry
+ 'axisym': True,
+ # "Physical" length of the domain (in meters)
+ 'LENGTH': 3000,
+ # Number of elements
+ 'NSPEC': 250,
+ # Degree of the basis functions
+ 'N': 4,
+ # Degree of basis functions in the first element
+ 'NGLJ': 4,
+ # Number of time steps
+ 'NTS': 2,
+ # Courant CFL number
+ 'CFL': 0.45,
+ # Grid description
+ 'GRID_TYPE': 'homogeneous',
+ 'GRID_FILE': 'grid_homogeneous.txt',
+ 'TICKS_FILE': 'ticks_homogeneous.txt',
+ # kg/m^3
+ 'DENSITY': 2500,
+ # Pa
+ 'RIGIDITY': 30000000000,
+ # Duration of the source in dt
+ 'TSOURCE': 100,
+ # GLL point number on which the source is situated
+ 'ISOURCE': 0,
+ # Maximum amplitude
+ 'MAX_AMPL': 1e7,
+ # Source's type
+ 'SOURCE_TYPE': 'ricker',
+ # Decay rate for the ricker
+ 'DECAY_RATE': 2.628,
+ # One image is displayed each DPLOT time step
+ 'DPLOT': 10,
+ })
+ with open('Par_file') as f:
+ cp.readfp(FakeGlobalSectionHead(f))
+
+ self.axisym = cp.getboolean('global', 'AXISYM')
+ self.length = cp.getfloat('global', 'LENGTH')
+ self.nSpec = cp.getint('global', 'NSPEC')
+ self.N = cp.getint('global', 'N')
+ self.NGLJ = cp.getint('global', 'NGLJ')
+ self.nts = cp.getint('global', 'NTS')
+ self.cfl = cp.getfloat('global', 'CFL')
+ self.gridType = cp.get('global', 'GRID_TYPE').strip("'\"")
+ self.gridFile = cp.get('global', 'GRID_FILE').strip("'\"")
+ self.ticksFile = cp.get('global', 'TICKS_FILE').strip("'\"")
+ self.meanRho = cp.getfloat('global', 'DENSITY')
+ self.meanMu = cp.getfloat('global', 'RIGIDITY')
+ self.tSource = cp.getfloat('global', 'TSOURCE')
+ self.iSource = cp.getint('global', 'ISOURCE')
+ self.maxAmpl = cp.getfloat('global', 'MAX_AMPL')
+ self.sourceType = cp.get('global', 'SOURCE_TYPE').strip("'\"")
+ self.decayRate = cp.getfloat('global', 'DECAY_RATE')
+ self.dplot = cp.getfloat('global', 'DPLOT')
+
+ self.nGLL = self.N + 1 # Number of GLL points per elements
+ self.nGLJ = self.NGLJ + 1 # Number of GLJ in the first element
+ self.nGlob = (self.nSpec - 1) * self.N + self.NGLJ + 1 # Number of points in the array
+ self.ibool = functions.globalArray(self.nSpec, self.nGLL) # Global array TODO add GLJ
+ self.dt = 0 # Time step (will be updated)
+
# Gauss Lobatto Legendre points and integration weights :
- self.ksiGLL=ksiGLL # Position of the GLL points in [-1,1]
- self.wGLL=wGLL # Integration weights
- self.ksiGLJ=ksiGLJ # Position of the GLJ points in [-1,1]
- self.wGLJ=wGLJ # Integration weights
+ try:
+ # Position of the GLL points in [-1,1]
+ self.ksiGLL = ksiGLL[self.N]
+ # Integration weights
+ self.wGLL = wGLL[self.N]
+ except KeyError:
+ raise ValueError('N = %d is invalid!' % (self.N, ))
+ try:
+ # Position of the GLJ points in [-1,1]
+ self.ksiGLJ = ksiGLJ[self.NGLJ]
+ # Integration weights
+ self.wGLJ = wGLJ[self.NGLJ]
+ except KeyError:
+ raise ValueError('NGLJ = %d is invalid!' % (self.NGLJ, ))
+
# Derivatives of the Lagrange polynomials at the GLL points
- self.deriv=functions.lagrangeDeriv(self.ksiGLL)
- self.derivGLJ=functions.GLJderiv(self.ksiGLJ)
+ self.deriv = functions.lagrangeDeriv(self.ksiGLL)
+ self.derivGLJ = functions.GLJderiv(self.ksiGLJ)
+
class OneDgrid:
"""Contains the grid properties"""
@@ -155,8 +185,6 @@ class OneDgrid:
def __init__(self,param):
"""Init"""
self.param=param
- self.gridFile=GRID_FILE
- self.ticksFile=TICKS_FILE
self.z=np.zeros(param.nGlob)
self.rho=np.zeros((param.nSpec,param.nGLL))
self.mu=np.zeros((param.nSpec,param.nGLL))
@@ -181,8 +209,8 @@ class OneDgrid:
print "typeOfGrid == 'miscellaneous' Has not been implemented yet"
raise
elif param.gridType == 'file':
- [self.z,self.rho,self.mu]=np.loadtxt(defines.GRID_FILE).transpose()
- self.ticks=np.loadtxt(defines.TICKS_FILE)
+ self.z, self.rho, self.mu = np.loadtxt(param.gridFile, unpack=True)
+ self.ticks = np.loadtxt(param.ticksFile)
else :
print "Unknown grid's type"
raise
@@ -244,6 +272,3 @@ class Source:
plt.title('Source(t)')
plt.grid(True)
plt.show()
-
-### <- THIS PART IS NOT SUPPOSED TO BE MODIFIED ###
-
diff --git a/Python_version/functions.py b/Python_version/functions.py
index 5645484..54e598f 100644
--- a/Python_version/functions.py
+++ b/Python_version/functions.py
@@ -49,7 +49,7 @@ def invProjection(ksi, elt_number, ticks):
def lagrangeDeriv(ksiGLL):
"""Calculates the values of the derivative of the Lagrange polynomials
at the GLL points"""
- N=defines.N
+ N = len(ksiGLL) - 1
deriv=np.zeros((N+1,N+1),dtype='d')
for i in range(N+1):
for j in range(N+1):
diff --git a/Python_version/main_for_Python_version.py b/Python_version/main_for_Python_version.py
index 84bc869..77a0eee 100644
--- a/Python_version/main_for_Python_version.py
+++ b/Python_version/main_for_Python_version.py
@@ -9,7 +9,7 @@ Main script for 1D spectral elements.
li(z): Lagrange polynomial of degree N
-> ui(t): Time dependent expansion coeffs (what we are looking for)
- The parameters of the run can be edited in the file defines.py
+ The parameters of the run can be edited in the file Par_file
@author: Alexis Bottero, CNRS Marseille, France (alexis.bottero at gmail.com)
"""
@@ -72,7 +72,7 @@ for it in np.arange(param.nts):
acc[:] /= M[:]
vel[:] += param.dt/2*acc[:]
- if it%defines.DPLOT == 0:
+ if it % param.dplot == 0:
if param.axisym:
b=np.array([i for i in reversed(u)])
c=append(b,u)
More information about the CIG-COMMITS
mailing list