[英]Is that way of writing/reading a solution safe?
I need to store a solution of expensive FEM calculation to use it in further analysis. 我需要存储昂贵的FEM计算解决方案,以便在进一步分析中使用它。 Browsing through tutorials I have so far discovered, that I may store my results like this:
浏览我到目前为止发现的教程,我可以像这样存储我的结果:
from fenics import *
mesh = Mesh('mesh/UnitSquare8x8.xml')
V = FunctionSpace(mesh, 'P', 1)
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, u_D, boundary)
A = assemble(a)
b = assemble(L)
bc.apply(A, b)
u = Function(V)
solver = KrylovSolver("cg", "ilu")
solver.solve(A, u.vector(), b)
File('solution.xml') << u.vector()
and later load them like this: 然后像这样加载它们:
from fenics import *
mesh = Mesh('mesh/UnitSquare8x8.xml')
V = FunctionSpace(mesh, 'P', 1)
u = Function(V)
File('solution.xml') >> u.vector()
Unfortunately I hardly know what exactly I am doing here. 不幸的是,我几乎不知道我到底在做什么。 Is that a proper way of storing and loading calculated results?
这是存储和加载计算结果的正确方法吗? Is order of elements in
u.vector()
(for the same mesh file) fixed within/between different FEniCS versions, or it is just an implementation detail which may change any time? u.vector()
的元素顺序(对于相同的网格文件)是在不同的FEniCS版本内/之间修复的,还是只是一个可能随时改变的实现细节? If it is unsafe, then what is the proper way of doing so? 如果它不安全,那么这样做的正确方法是什么?
I have found another (possibly even more dangerous) solution. 我找到了另一种(可能更危险的)解决方案。 I may use
VALUES = u.vector().get_local()
and u.vector().set_local(VALUES)
methods, as VALUES
is a numpy array which I may easily store and load. 我可以使用
VALUES = u.vector().get_local()
和u.vector().set_local(VALUES)
方法,因为VALUES
是一个我可以轻松存储和加载的numpy数组。
No, according to answer to Is the order of Vector
elements preserved between runs? 不,根据回答是否在运行之间保留了
Vector
元素的顺序? the order of Vector
elements is not guarranted to be preserved. Vector
元素的顺序不保证是保留的。
It is recommended to use XDMFFile.write_checkpoint()
and XDMFFile.read_checkpoint()
methods instead. 建议使用
XDMFFile.write_checkpoint()
和XDMFFile.read_checkpoint()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.