Skip to content

Generating PVD files when the Triangulation is not present on all Ranks #171

@shreyas02

Description

@shreyas02

With the current implementation, we can not generate the paraview files when the triangulations are not present on all MPI ranks. In issue #70, this has been written as a #TODO task for taking nothing.

Since I needed this functionality for my use case, I tried to fix it myself, but I had to use functions from Mpi.jl as well. Below are the changes I made to make it work.

struct DistributedPvd{T<:AbstractArray}
  pvds::T
  main_rank::Int
end

function Visualization.createpvd(trian::DistributedTriangulation,parts::AbstractArray,args...;kwargs...)
  main_rank = -1
  map(local_views(trian), parts) do LocTrian, part
    flag = part * !(num_cells(LocTrian) == 0)
    global_flag = Vector{Int}(undef, MPI.Comm_size(MPI.COMM_WORLD))
    MPI.Allgather!([flag], global_flag, MPI.COMM_WORLD)
    main_rank = maximum(global_flag)
  end
  pvds = map_main(parts,main = main_rank) do part
    p = paraview_collection(args...;kwargs...)
  end
  DistributedPvd(pvds,main_rank)
end

function Visualization.createpvd(f,trian::DistributedTriangulation,parts::AbstractArray,args...;kwargs...)
  pvd = createpvd(trian,parts,args...;kwargs...)
  try
    f(pvd)
  finally
    savepvd(pvd)
  end
end

function Visualization.createpvd(parts::AbstractArray,args...;kwargs...)
  pvds = map_main(parts, main = 1) do part
    p = paraview_collection(args...;kwargs...)
  end
  DistributedPvd(pvds,1)
end

function Visualization.createpvd(f,parts::AbstractArray,args...;kwargs...)
  pvd = createpvd(parts,args...;kwargs...)
  try
    f(pvd)
  finally
    savepvd(pvd)
  end
end

function Visualization.savepvd(pvd::DistributedPvd)
  map_main(pvd.pvds, main = pvd.main_rank) do pvd
    vtk_save(pvd)
  end
end

function Base.setindex!(pvd::DistributedPvd,pvtk::AbstractArray,time::Real)

  map(pvtk) do pvtk
    if !isnothing(pvtk)
      vtk_save(pvtk)
    end  
  end

  map_main(pvtk,pvd.pvds,main = pvd.main_rank) do pvtk,pvd
    pvd[time] = pvtk
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions