Skip to content

Commit

Permalink
add mergeTime option for readforce postproc (#15)
Browse files Browse the repository at this point in the history
* add mergeTime option for readforce postproc

* add postProc readforce example

* coding style (black + ...)
  • Loading branch information
CyrilleBonamy committed Feb 2, 2020
1 parent 5343552 commit 14b180c
Show file tree
Hide file tree
Showing 15 changed files with 726 additions and 447 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ clean:
tests:
python -m unittest discover

black:
black -l 82 fluidfoam

tests_coverage:
mkdir -p .coverage
coverage run -p -m unittest discover
Expand Down
3 changes: 3 additions & 0 deletions examples/plot_averaged_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@
#Setting axis labels
plt.xlabel('U (m/s)')
plt.ylabel('y (m)')

# add grid
plt.grid()
38 changes: 38 additions & 0 deletions examples/plot_postproc_force.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Read and Plot a time series of OpenFoam postProcessing force
============================================================
This example reads and plots a series of postProcessing force
"""

###############################################################################
# Read the postProcessing files
# -----------------------------
#
# .. note:: In this example it reads and merges two postProcessing files
# automatically (with the 'mergeTime' option)

# import readforce function from fluidfoam package
from fluidfoam.readpostpro import readforce

sol = '../output_samples/ascii/'

force = readforce(sol, time_name = 'mergeTime')

###############################################################################
# Now plots the pressure force
# ----------------------------
#

import matplotlib.pyplot as plt

plt.figure()

plt.plot(force[:, 0], force[:, 1])

# Setting axis labels
plt.xlabel('t (s)')
plt.ylabel('p (Pa)')

# add grid
plt.grid()
3 changes: 3 additions & 0 deletions examples/plot_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@
# Setting axis labels
plt.xlabel('t (s)')
plt.ylabel('p (Pa)')

# add grid
plt.grid()
10 changes: 6 additions & 4 deletions fluidfoam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from fluidfoam.readof import typefield
from fluidfoam.processing1d import create1dprofil, read1dprofil
from fluidfoam.processing1d import create1dprofil_spe, plot1dprofil
from fluidfoam.meshdesign import getgz,getdzs
from fluidfoam.meshdesign import getgz, getdzs
from fluidfoam.readpostpro import readforce, readprobes
from fluidfoam._version import __version__

warnings.simplefilter('always', category=DeprecationWarning)
warnings.simplefilter("always", category=DeprecationWarning)


def readarray(*args, **kargs):
warnings.warn('The function readarray is deprecated. '
'Please use readvector instead.', DeprecationWarning)
warnings.warn(
"The function readarray is deprecated. " "Please use readvector instead.",
DeprecationWarning,
)
return readvector(*args, **kargs)
2 changes: 1 addition & 1 deletion fluidfoam/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
'a' or 'alpha' means alpha version (internal testing),
'b' or 'beta' means beta version (external testing).
"""
__version__ = '0.1.7'
__version__ = "0.1.7"
22 changes: 12 additions & 10 deletions fluidfoam/meshdesign.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import numpy as np


def getgz(h, dz1, N):
""" Given a domain size h, a first grid size dz1 and a number of points N
this function returns the common ratio, the grading gz to enter
Expand All @@ -26,8 +27,9 @@ def getgz(h, dz1, N):
toto = np.where(np.imag(sol) == 0)
solreal = sol[toto]
titi = np.where(np.real(solreal) > 0)
titi = np.where(np.logical_and(np.real(solreal) > 0,
np.real(solreal) < 0.9999))
titi = np.where(
np.logical_and(np.real(solreal) > 0, np.real(solreal) < 0.9999)
)
solgood = solreal[titi]
# Compute the common ratio of the sequence
try:
Expand All @@ -48,12 +50,12 @@ def getgz(h, dz1, N):

# print some output
print("grid sizes, common ratio and grading factor")
print('z[N-1]=', z[N - 1])
print('dz[0]=', dz[0])
print('dz[N-2]=', dz[N - 2])
print('common ratio: r=', r)
print('gz=', gz)
print('1/gz=', 1. / gz)
print("z[N-1]=", z[N - 1])
print("dz[0]=", dz[0])
print("dz[N-2]=", dz[N - 2])
print("common ratio: r=", r)
print("gz=", gz)
print("1/gz=", 1. / gz)
return z, dz, gz


Expand All @@ -80,7 +82,7 @@ def getdzs(h, gz, N):

# print some output
print("grid size of the first and last cells:")
print('dz[0]=', dz1)
print('dz[N-1]=', dzN)
print("dz[0]=", dz1)
print("dz[N-1]=", dzN)

return dz1, dzN
138 changes: 80 additions & 58 deletions fluidfoam/processing1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,49 @@ def create1dprofil_spe(pathw, waxis, var, varname, typevar):
"""

size1d = waxis.shape[0]
filename = ''
filename = ""
field = var
filename = ''+varname
filename = "" + varname

if typevar == 'scalar':
filename1 = pathw+'/1d_profil/'+filename+'.xy'
if typevar == "scalar":
filename1 = pathw + "/1d_profil/" + filename + ".xy"
f = open(filename1, "w")
f.write('(\n')
if field.shape==(1,):
f.write("(\n")
if field.shape == (1,):
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[0])+')\n')
f.write("(" + str(waxis[cell]) + " " + str(field[0]) + ")\n")
else:
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[cell])+')\n')
f.write(')\n')
f.write("(" + str(waxis[cell]) + " " + str(field[cell]) + ")\n")
f.write(")\n")
f.close()
elif typevar == 'vector':
elif typevar == "vector":
for i in range(3):
filename1 = pathw+'/1d_profil/'+filename+str(i)+'.xy'
filename1 = pathw + "/1d_profil/" + filename + str(i) + ".xy"
f = open(filename1, "w")
f.write('(\n')
if field.shape==(3,1):
f.write("(\n")
if field.shape == (3, 1):
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[i, 0])+')\n')
f.write(
"(" + str(waxis[cell]) + " " + str(field[i, 0]) + ")\n"
)
else:
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[i, cell])+')\n')
f.write(')\n')
f.write(
"(" + str(waxis[cell]) + " " + str(field[i, cell]) + ")\n"
)
f.write(")\n")
f.close()
else:
print('PROBLEM with input: Good input is for example :')
print('fluidfoam.create1dprofil_spe("/data/1dcompute/", Y, epsilon, "epsilon", "scalar")\n')
status = 'create 1D profiles: done'
print("PROBLEM with input: Good input is for example :")
print(
'fluidfoam.create1dprofil_spe("/data/1dcompute/", Y, epsilon, "epsilon", "scalar")\n'
)
status = "create 1D profiles: done"
return status


def create1dprofil(pathr, pathw, timename, axis ,varlist):
def create1dprofil(pathr, pathw, timename, axis, varlist):
"""
This function provides way to read 1D profiles at time timename of pathr
and write them in OpenFoam Format in the 1d_profil folder of pathw
Expand All @@ -105,53 +111,69 @@ def create1dprofil(pathr, pathw, timename, axis ,varlist):

X, Y, Z = readmesh(pathr)
size1d = Y.shape[0]
filename = ''
filename = ""
for var in varlist:
field = readfield(pathr, timename, var)
typevar = typefield(pathr, timename, var)

if axis=='X':
waxis=X
elif axis=='Y':
waxis=Y
elif axis=='Z':
waxis=Z
if axis == "X":
waxis = X
elif axis == "Y":
waxis = Y
elif axis == "Z":
waxis = Z
else:
print('axis does not exist, please check input parameters\n')
print("axis does not exist, please check input parameters\n")

filename = ''+var
filename = "" + var

if typevar == 'scalar':
filename1 = pathw+'/1d_profil/'+filename+'.xy'
if typevar == "scalar":
filename1 = pathw + "/1d_profil/" + filename + ".xy"
f = open(filename1, "w")
f.write('(\n')
if field.shape==(1,):
f.write("(\n")
if field.shape == (1,):
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[0])+')\n')
f.write("(" + str(waxis[cell]) + " " + str(field[0]) + ")\n")
else:
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[cell])+')\n')
# np.savetxt(f, np.c_[Y, field], fmt="(%s %s)")
f.write(')\n')
f.write(
"(" + str(waxis[cell]) + " " + str(field[cell]) + ")\n"
)
# np.savetxt(f, np.c_[Y, field], fmt="(%s %s)")
f.write(")\n")
f.close()
elif typevar == 'vector':
elif typevar == "vector":
for i in range(3):
filename1 = pathw+'/1d_profil/'+filename+str(i)+'.xy'
filename1 = pathw + "/1d_profil/" + filename + str(i) + ".xy"
f = open(filename1, "w")
f.write('(\n')
if field.shape==(3,1):
f.write("(\n")
if field.shape == (3, 1):
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[i, 0])+')\n')
f.write(
"("
+ str(waxis[cell])
+ " "
+ str(field[i, 0])
+ ")\n"
)
else:
for cell in range(size1d):
f.write('('+str(waxis[cell])+' '+str(field[i, cell])+')\n')
f.write(')\n')
f.write(
"("
+ str(waxis[cell])
+ " "
+ str(field[i, cell])
+ ")\n"
)
f.write(")\n")
f.close()
print('Warning for pyof users : Ua=Ua0, Va=Ua2, Wa=Ua1\n')
print("Warning for pyof users : Ua=Ua0, Va=Ua2, Wa=Ua1\n")
else:
print('PROBLEM with varlist input: Good input is for example :')
print('fluidfoam.create1dprofil("/data/1dcompute/", "/data/1dcompute/", "750", "Y",[\'omega\',\'p\'])\n')
status = 'create 1D profiles: done'
print("PROBLEM with varlist input: Good input is for example :")
print(
'fluidfoam.create1dprofil("/data/1dcompute/", "/data/1dcompute/", "750", "Y",[\'omega\',\'p\'])\n'
)
status = "create 1D profiles: done"
return status


Expand All @@ -173,17 +195,17 @@ def read1dprofil(file_name):

with open(file_name) as handle:

size1d = len(handle.readlines())-2
size1d = len(handle.readlines()) - 2
z = np.empty(size1d)
field = np.empty(size1d)
handle.seek(0)
for line_num, line in enumerate(handle):
if ((line_num != 0) & (line_num != size1d+1)):
line = line.replace(')', '')
line = line.replace('(', '')
if (line_num != 0) & (line_num != size1d + 1):
line = line.replace(")", "")
line = line.replace("(", "")
cols = line.split()
z[(line_num-1)] = cols[0]
field[(line_num-1)] = cols[1]
z[(line_num - 1)] = cols[0]
field[(line_num - 1)] = cols[1]
return z, field, size1d


Expand All @@ -201,12 +223,12 @@ def plot1dprofil(pathr, varlist):

import matplotlib.pyplot as plt

z, field, size1d = read1dprofil(pathr+"/"+varlist[0]+".xy")
z, field, size1d = read1dprofil(pathr + "/" + varlist[0] + ".xy")
fields = np.empty([len(varlist), size1d])
fields[0] = field
for i in range(len(varlist)-1):
z, field, size1d = read1dprofil(pathr+"/"+varlist[i+1]+".xy")
fields[i+1] = field
for i in range(len(varlist) - 1):
z, field, size1d = read1dprofil(pathr + "/" + varlist[i + 1] + ".xy")
fields[i + 1] = field

dummy, axarr = plt.subplots(1, len(varlist), sharey=True)
for i, dummy in enumerate(varlist):
Expand Down
Loading

0 comments on commit 14b180c

Please sign in to comment.