将术语存储在fipy中作为数组而不是fipy对象

2024-06-26 14:11:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我是菲比的新手,所以如果这是一个愚蠢的问题,我道歉(而且this似乎对我没有帮助)。 但是,除了上面的问题之外,有没有其他方法可以以人类可读(或python可读)的形式存储fipy对象呢?这只适用于单元格变量。 如果我想做一些比默认fipy查看器中更奇特/定制的绘图,我该怎么做?你知道吗

以一个简单的一维扩散为例:

from fipy import *
# USER-DEFINED PARAMETERS
nx = 100
dx = 0.1
D = 1.0
bound1 = 30
bound2 = 70

# PREPARED FOR SOLUTION
mesh = Grid1D(nx=nx, dx=dx)
print "mesh", mesh

# define some parameters specific to this solution
T0 = bound2
Tinf = bound1

hour = 3600
day = hour*24
ndays = 1
duration = ndays*day

T = CellVariable(name="Temperature", mesh=mesh, value=bound1)
# Constant temperature boundary condition
T.constrain(T0, mesh.facesLeft)
T.constrain(Tinf, mesh.facesRight)
# SOLUTION
eq = (TransientTerm() == DiffusionTerm(coeff=D))
timeStepDuration = 0.5*hour
steps = int(duration/timeStepDuration)
for step in range(steps):
    eqCirc.solve(var=T,dt=timeStepDuration)

例如,我可以存储网格?或者我可以在每个步骤中存储DiffusionTerm而不是CellVariable的值吗?你知道吗

在我的例子中,我想用每一个时间步的距离来绘制热梯度(所以从扩散项中提取它)。 我能做吗?怎样?你知道吗


Tags: thisdurationsolutionnxmeshdayhourdx
1条回答
网友
1楼 · 发布于 2024-06-26 14:11:58

But is there a way to store fipy objects in human-readable (or python-readable) form, other than suggested in the question above?

有很多选择。任何FiPy对象都可以使用fipy.dump进行pickle,它将在并行运行时收集数据。例如

import fipy
mesh = fipy.Grid2D(nx=3, ny=3)
var = fipy.CellVariable(mesh=mesh)
var[:] = mesh.x * mesh.y
fipy.dump.write(var, 'dump.gz')

然后可以在另一个Python会话中用

var = fipy.dump.read('dump.gz')

然而,Pickle对于长期存储不是很好,因为它依赖于使用相同版本的代码来读回数据。另一种方法是使用

np.save('dump.npy', var)

然后和我一起读

var_array = np.load('dump.npy')
var = fipy.CellVariable(mesh=mesh, value=var_array)

If I want to do some more fancy/customized plotting than what is in the default fipy viewer, how can I do it? If I want to do some more fancy/customized plotting than what is in the default fipy viewer, how can I do it?

要以可读的形式保存数据,并将位置和值数据保存到另一个包中,可以尝试使用pandas

import pandas
df = pandas.DataFrame({'x' : mesh.x, 'y': mesh.y, 'value': var})
df.to_csv('dump.csv')

But could I, for example, store the mesh as an array?

当然,您可以Pickle任何Python对象,但是对于长期存储来说,使用实际对象的知识更好。只有cd2和cd5才需要。{cd6}方法为对象提供了一个pickling需求。所有需要存储的内容就是这个方法返回的内容。你知道吗

Or could I store the value of the DiffusionTerm instead of the CellVariable in each step?

DiffusionTerm除了它的系数之外,实际上不存储任何东西。方程存储其矩阵和b向量。你知道吗

相关问题 更多 >