Get block name and ID from multiblock dataset

I’m trying to post-process an openFOAM simulation and I need to perform the analysis on one of the blocks of the solution (the one referring to the wall patch called ‘plate’). I need to apply an extractBlock filter on the solution and get this patch. The point is that I saw that the block index can vary from case to case and I would like to select the block to extract by its name (‘plate’). I there a way to do this?
This is what my spript currently looks like:

from paraview.simple import *
import os
import vtk.numpy_interface.dataset_adapter as dsa
import numpy as np
casefoam = OpenFOAMReader(FileName=‘case.foam’)
casefoam.MeshRegions = [‘inlet’, ‘internalMesh’, ‘outlet’, ‘plate’, ‘side1’, ‘side2’, ‘upperWall’]
animationScene1 = GetAnimationScene()
animationScene1.GoToLast()
extractBlock1 = ExtractBlock(Input=casefoam)
extractBlock1.BlockIndices = [5]

I’d like to find a way to know what index is assigned to every mesh region name.

Thanks in advance!

1 Like

Hello Lorenzo,

I´m trying to extract the Blockindices of my multiblock-dataset aswell. Do you have a solution for this task by now?

Tank you in advance,
Paul

Hi everyone,

A while ago I faced the same problem and took the time to write a small function that returns a dictionnary in which the key is the block name, and the value is the block index. It looks like this:

def getBlockIndices(compositeDataInformation, index=0):

    localDict = {}

    if compositeDataInformation.GetDataIsMultiPiece():
        index += compositeDataInformation.GetNumberOfChildren()
    else:
        if compositeDataInformation.GetDataIsComposite():
            for i in range(compositeDataInformation.GetNumberOfChildren()):
                index += 1
                _blockName = compositeDataInformation.GetName(i)
                localDict[_blockName] = index
                leafDict, index = getBlockIndices(compositeDataInformation.GetDataInformation(i).GetCompositeDataInformation(), index)
                localDict.update(leafDict)

    return localDict, index

#Usage:
getBlockIndices(yourMultiBlockSourceorFilter.GetDataInformation().DataInformation.GetCompositeDataInformation())

Hope this helps.

1 Like

Thank you so much for your help!

I´m still struggeling to run this srcipt (sorry, I´m really new to ParaView/Python).
What exactly do I have to insert in “yourMultiBlockSourceorFilter.” ?
I already tried the filepath of my input file but it´s not working…
Again, thank you for your support!

It should be the object in which you read your file, not the filename directly.

so in the example for the first post, that should be “casefoam”

1 Like

Okay. I´m still not able to extract the Indices. The file format of my input file in Paraview is .plt or .e. Could this be a part of the problem? I would like to extract the Indices form the Blocks “FuselageBugSection_01, FuselageBugSection_02,…” (picture). I´ve managed to get the names of the Element Blocks with “.ElementBlocks.Available” but this contains no information about the Blockindices.Paraview

can you share the python code you use to process the data ?
The function is meant to be used in a script, not from the GUI

I use this code to get the names of the ElementBlocks. In this case I opened the exodusfile to get the names.

from paraview.simple import *

reader = ExodusIIReader(FileName=['C:\\Users\\paul\\........\\exodusfile.e'])
name=reader.ElementBlocks.Available

print(name)

To get the Indices with youre function, I tried this:

from paraview.simple import *

reader = ExodusIIReader(FileName=['C:\\Users\\paul\\.........\\exodusfile.e'])


def getBlockIndices(compositeDataInformation, index=0):

    localDict = {}

    if compositeDataInformation.GetDataIsMultiPiece():
        index += compositeDataInformation.GetNumberOfChildren()
    else:
        if compositeDataInformation.GetDataIsComposite():
            for i in range(compositeDataInformation.GetNumberOfChildren()):
                index += 1
                _blockName = compositeDataInformation.GetName(i)
                localDict[_blockName] = index
                leafDict, index = getBlockIndices(compositeDataInformation.GetDataInformation(i).GetCompositeDataInformation(), index)
                localDict.update(leafDict)

    return localDict, index


Indices=getBlockIndices(reader.GetDataInformation().DataInformation.GetCompositeDataInformation())
print(Indices)

But it only prints out “({}, 0)”.

Thank you again for your kind support!

That’s indeed the way to use it.
I’ve never worked with exodus format.

Have you tried with the tecplot (.plt) file ? that should work. the print(Indices) should return some content

1 Like

I´ve tried to use the following code but it prints out the same results. Is “OpenDataFile” the right way to read the .plt file?

from paraview.simple import *

reader = OpenDataFile('C:\\Users\\paul\\.............\\techplotfile.plt')


def getBlockIndices(compositeDataInformation, index=0):

    localDict = {}

    if compositeDataInformation.GetDataIsMultiPiece():
        index += compositeDataInformation.GetNumberOfChildren()
    else:
        if compositeDataInformation.GetDataIsComposite():
            for i in range(compositeDataInformation.GetNumberOfChildren()):
                index += 1
                _blockName = compositeDataInformation.GetName(i)
                localDict[_blockName] = index
                leafDict, index = getBlockIndices(compositeDataInformation.GetDataInformation(i).GetCompositeDataInformation(), index)
                localDict.update(leafDict)

    return localDict, index


Indices=getBlockIndices(reader.GetDataInformation().DataInformation.GetCompositeDataInformation())
print(Indices)

can you eventually share your plt file ?

I found that this only works once all the parts are imported, otherwise it returns that there is nothing in the tree. If you select BlockIndices=[0] it imports everything, so I did something like this:

extractBlock1 = ExtractBlock(registrationName=‘ExtractBlock1’, Input=‘case.foam’)
extractBlock1.BlockIndices = [0]
renderView1 = GetActiveViewOrCreate(‘RenderView’)
extractBlock1Display = Show(extractBlock1, renderView1, ‘GeometryRepresentation’)

Then I could use the above code to create the full dictionary, then select individual blocks by name, convert it to a list and set extractBlock1.BlockIndices using the new list.