我有一个派生类型,表示为:
struct BoundaryCondition
domain::MeshStructure
left::BorderValue
right::BorderValue
bottom::BorderValue
top::BorderValue
back::BorderValue
front::BorderValue
end
我想创建一个这种派生类型的数组,大小为 n_comp,在我的代码中如下:
bc = Array{BoundaryCondition}(n_comp)
for i in 1:n_comp
bc[i] = createBC(m)
bc[i].left.a[:] = 0.0
bc[i].left.b[:] = 1.0
bc[i].left.c[:] = c_left[i]
end
但我收到一个错误:
ERROR: LoadError: MethodError: no method matching (Array{BoundaryCondition, N} where N)(::Int64)
声明我的 BoundaryCondition 类型数组的正确方法是什么?
答案 0 :(得分:2)
如果你想预先分配它,你需要用一些东西填充你的数组。例如,您可以使用 undef
(用您的问题替换 Bool
类型和大小):
julia> Vector{Bool}(undef, 3)
3-element Vector{Bool}:
1
0
0
您也可以使用 fill
(相同,将 false
替换为您问题的默认“填充”值):
julia> fill(false, 3)
3-element Vector{Bool}:
0
0
0
如果不需要预分配,一个好的解决方案是理解。制作用于创建边界条件的索引的函数,例如:
function my_BC(i)
out = createBC(m)
out.left.a[:] = 0.0
out.left.b[:] = 1.0
out.left.c[:] = c_left[i]
return out
end
然后简单地做
[my_BC(i) for i in 1:n_comp]