FEniCS 中 xdmf 文件的写入与读取

2021-12-16  本文已影响0人  马鹏飞_47c5

要完整地有限元空间中一个函数的完整信息,我们需要知道这个函数所在的函数空间和基函数的系数(通常称为dofs)。XDMFFILE的write函数只保存网格节点上的函数值,对应的也就是CG-1函数空间。这种做法虽然高效,但是FEniCS不能直接读取用write函数保存的数据,因为它丢失了原函数的函数空间的相关信息。如果要完整地保存和读取函数的信息,那么就需要使用write_checkpoint和read_checkpoint两个函数,他们能够保存并还原一个函数,以下是一个简单的范例。

from dolfin import *
nx = ny = 8 
mesh = UnitSquareMesh(nx, ny) 
V = FunctionSpace(mesh, 'P', 1) 
    
alpha = 3; beta = 1.2 
f = Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t', degree=2, alpha=alpha, beta=beta, t=0) 
 
f_out = XDMFFile("test.xdmf") 
f_out.write_checkpoint(project(f, V), "f", 0, XDMFFile.Encoding.HDF5, False)  # Not appending to file
for j in range(1,5): 
    t = float(j/5) 
    f.t = t 
f_out.write_checkpoint(project(f,V), "f",t, XDMFFile.Encoding.HDF5, True)  #appending to file
    
f_out.close() 
     
f1 = Function(V) 
f_in =  XDMFFile("test.xdmf") 
   
f_in.read_checkpoint(f1,"f",0) 
f_in.read_checkpoint(f1,"f",1)

write_checkpoint可以带有时刻信息,并且相同时刻的函数多次存储不会发生冲突。xdmf存储相同函数的时候有一个计数器,用来唯一地标识每次存储的数据。用read_checkpoint函数读取数据的时候只能根据计数读取,无法根据时刻读取。

最后,XMDF 也可以读取和写入网格,fenics中的网格边界标注 Meshfunction<typename T> 也能通过XDMFFile中write和read函数读取。

from fenics import *
mesh = UnitSquareMesh(64, 64)
mesh_file = XDMFFile("mesh.xdmf")
mesh_file.write(mesh)
mesh_file.close()
from fenics import *
mesh_file = XDMFFile("mesh.xdmf")
mesh = Mesh()
mesh_file.read(mesh)
mesh.close()

参考:

类的定义在文件XDMFFile.h中,以下是一些来自fenics论坛上的一些讨论:

https://fenicsproject.discourse.group/t/xdmf-write-checkpoint-questions/2385

https://fenicsproject.discourse.group/t/loading-xdmf-data-back-in/1925

https://fenicsproject.discourse.group/t/access-function-in-time-dependent-xdmffile/359

上一篇下一篇

猜你喜欢

热点阅读