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.

Hi, when i run this function with ParaView-5.8.0 it works perfectly, but when i run it with the latest version, \ParaView-5.11.0, does not work.
Do you know if i have to add any command or something similar?
Thank you.

Hi Rafael,

It seems that the method has changed for how extract blocks are defined.

You can directly select the names with the Selectors function:
extractBlock1.Selectors = [‘/Root/boundary/inletOutlet’,‘/Root/internalMesh’]

Note: there is a bit of an issue with parts containing a ‘-’, it just gets removed so part-name would become partname

I have many multiblock datasets where “Selectors” list is unknown a priori.

I’d like to loop through a given multiblock and integrate variables per block and output a table of blockname1, integrated_var1, integrated_var2,…, but I don’t see documentation on where to find the list of blocks within a multiBlock object.

How does one get the list of available Selectors, using python, in the new method?