I made a function to read OpenFOAM time series data and sets data (spatial distribution data output by the sample command) in Python, and simplified the plotting with matplotlib.
As a premise, the time series data of OpenFOAM is saved in a new directory every time it is restarted, but it is assumed that they are attached with the cat command to form a single file.
def readHistory(case, positions, variables):
import numpy as np
"""
case: case name (string data)
positions: position name (list data)
variables: physical quantities (list data)
"""
data ={}
for position in positions:
for variable in variables:
key = variable+"_"+position
value = np.genfromtxt(case+"/"+position+"/"+variable, unpack=True)
data[key] = value
return data
Argument description
def readSets(case, file, variables):
import numpy as np
import pandas as pd
import glob
import os
"""
case: case name (string data)
file: sets data file name (string data)
variables: physical quantities (list data)
"""
# list of timing
list1 = glob.glob(case + "/sets/*")
variable_names = file.split("_")
# remove ".xy"
variable_names[-1] = variable_names[-1].split(".")[0]
position, variable_names[0] = variable_names[0], "x"
#print(variable_names)
data = {}
for variable in variables:
key = variable+"_"+position
data[key] = pd.DataFrame()
for i in list1:
path = i+"/" + file
df = pd.read_table(path, header = None, names = variable_names)
data[key] = pd.concat([data[key], df[variable]], axis=1,)
list_index=[]
for i in list1:
list_index.append(str(float(os.path.basename(i))))
data[key].columns = list_index
data[key] = pd.concat([df[variable_names[0]], data[key].sort_index(axis=1)], axis=1)
# return dictionary of dataframe
return data
Plot the data with matplotlib.
Suppose the data layout is as follows. test01 is the case name.
test01/position-A/p test01/position-A/U test01/position-B/p test01/position-B/U test01/sets/0/center_p_k.xy test01/sets/10/center_p_k.xy
p and U are time series data of position-A and B center_p_k.xy is the distribution data at time 0 and 10. is.
There are multiple cases, and it is designed assuming that you can easily create a plot with only the case name changed.
import matplotlib.pyplot as plt
%matplotlib inline
case = "test01"
history_position = ["position-A", "position-B"]
history_variables = ["p", "U"]
sets_variables = ["p", "k"]
sets_position = "center"
dataSet_test01= [readHistory(case, history_position, history_variables),
readSets(case, "center_p_k.xy", sets_variables)]
#Plot of a set of time series data
for i in history_position:
for j in history_variables:
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.plot(dataSet_test01[0][j+"_"+i][0], dataSet_test01[0][j+"_"+i][1], label=j)
ax.legend(bbox_to_anchor=(1.01, 1., 0., 0), loc='upper left', borderaxespad=0.,)
#Spatial distribution plot
for sets in sets_variables:
for column in dataSet_test01[1][sets+"_"+sets_position]:
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.plot(dataSet_test01[1][sets+"_"+sets_position]["x"],
dataSet_test01[1][sets+"_"+sets_position][column],
label=sets+"_"+column)
ax.legend(bbox_to_anchor=(1.01, 1., 0., 0), loc='upper left', borderaxespad=0.,)
Recommended Posts