Hello!

Extract Cell types from mesh in Paraview

It is often usefull to select cells by their type for visualization in Paraview. Visualizing boundary layer meshes for example or extracting the surface elements of a mesh can be done by using the following script in the Programmable filter of Paraview.

input = self.GetUnstructuredGridInput()
output = self.GetUnstructuredGridOutput();

# copy all curent data
output.GetPointData().PassData(input.GetPointData())
output.GetCellData().PassData(input.GetCellData())

# create an array holding the VTK cell type id of each element
cellTypes = vtk.vtkUnsignedCharArray()
cellTypes.DeepCopy(input.GetCellTypesArray())
cellTypes.SetName("Cell Types")
output.GetCellData().AddArray(cellTypes)

Even simpler you can import this file (export_cell_types.cpd) from the menu Tools>Manage Custom Filters… > Import.

[Read More]

Simple macros in ParaView

If you use ParaView often you may want to automate some of your workflows. One way to do this is writing a macro using the Python interpreter that ships with ParaView. Here, is an example of extracting all elements from a tetrahedral mesh that have a minimum dihedral angle less that 5 degrees.

from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()
# get selection
G = GetActiveSource()
# create quality Data
# set min dihedraql angle as criterion
M = MeshQuality(Input=G, TetQualityMeasure="Minimum Dihedral Angle")
# select it
SetActiveSource(M)
# Create selector
S = SelectionQuerySource(QueryString = "Quality <=5")
# Create Extractor
E = ExtractSelection(Input = M,Selection = S)
Show()
Render()

#### uncomment the following to render all views
# RenderAllViews()
# alternatively, if you want to write images, you can use SaveScreenshot(...).

Another advantage of this approach is that you can run it in a headless environment using the pvpython interpreter that ships with ParaView.

[Read More]

Experience at ATPESC

During the first half of August we took part in the Argonne Training Program on Extreme Scale Computing (ATPESC) and worked for a few days in the Argonne Leading Computing Facilities center (ALCF) just outside of Chicago, Illinois. The training consisted of lectures and presentations on software and hardware advances as well as hands-on exercises using the supercomputers of Argonne. We had the chance to meet people working on some of the most influential high-performance computing projects like core members of the MPI Forum or the OpenMP committee.

[Read More]

Type selector using template specialization

In an effort to contribute back to the great matplotlib-cpp library I came across the need of having to create a mechanism that can select between different types in C++. This was needed in order to allow passing data from C++ to python without any copies using the numpy library. The only caveat in this case in that numpy requires to know the type you are passing. Since the matplotlib-cpp library is template-based there is a nice compile-time way to do it using template specialization. In simple terms it boils down to creating a base case for all the types you do not support and specialize for the types you care. Using a simple struct to hold the type completes the whole construct. The final result is :

[Read More]

Usefull GCC compilation flags

Having the -Wall flag enabled by default can potentially save lots of time and headaches when programming in C/C++. Same other usefull flags are :

  • -Wduplicated-cond : Warn about duplicated condition in if-else-if chains.
  • -Wduplicated-branches : Warn when an if-else has identical branches.
  • -Wlogical-op : Warn about use of logical operations where a bitwise operation probably was intended.
  • -Wnull-dereference: Warn when the compiler detects paths that dereferences a null pointer.
  • -Wjump-misses-init: Warn if a goto statement or a switch statement jumps forward across the initialization of a variable, or jumps backward to a label after the variable has been initialized.

source

[Read More]

Embracing ssh-agent

ssh-agent helps with managing ssh keys, it can keep track of unlocked identity ssh keys making possible to enter the passphrase only once and use the unlocked key for all upcoming ssh connections. You can make it even more flexible by Sharing the same ssh-agent among multiple login sessions. Below is the best answer in my opinion, that I have used successfully may times.

function sshagent_findsockets {
    find /tmp -uid $(id -u) -type s -name agent.\* 2>/dev/null
}

function sshagent_testsocket {
    if [ ! -x "$(which ssh-add)" ] ; then
        echo "ssh-add is not available; agent testing aborted"
        return 1
    fi

    if [ X"$1" != X ] ; then
        export SSH_AUTH_SOCK=$1
    fi

    if [ X"$SSH_AUTH_SOCK" = X ] ; then
        return 2
    fi

    if [ -S $SSH_AUTH_SOCK ] ; then
        ssh-add -l > /dev/null
        if [ $? = 2 ] ; then
            echo "Socket $SSH_AUTH_SOCK is dead!  Deleting!"
            rm -f $SSH_AUTH_SOCK
            return 4
        else
            echo "Found ssh-agent $SSH_AUTH_SOCK"
            return 0
        fi
    else
        echo "$SSH_AUTH_SOCK is not a socket!"
        return 3
    fi
}

function sshagent_init {
    # ssh agent sockets can be attached to a ssh daemon process or an
    # ssh-agent process.

    AGENTFOUND=0

    # Attempt to find and use the ssh-agent in the current environment
    if sshagent_testsocket ; then AGENTFOUND=1 ; fi

    # If there is no agent in the environment, search /tmp for
    # possible agents to reuse before starting a fresh ssh-agent
    # process.
    if [ $AGENTFOUND = 0 ] ; then
        for agentsocket in $(sshagent_findsockets) ; do
            if [ $AGENTFOUND != 0 ] ; then break ; fi
            if sshagent_testsocket $agentsocket ; then AGENTFOUND=1 ; fi
        done
    fi

    # If at this point we still haven't located an agent, it's time to
    # start a new one
    if [ $AGENTFOUND = 0 ] ; then
        eval `ssh-agent`
    fi

    # Clean up
    unset AGENTFOUND
    unset agentsocket

    # Finally, show what keys are currently in the agent
    ssh-add -l
}

source

[Read More]
web  ssh 

Linediff : Comparing files snippet by snippet

When performing big code changes like refactoring, comparing files can become tedious and time consuming. One way to reduce the load is to compare the file function by function or even snippet by snippet since, most of the time this is what you actually need. A plugin for vim that works very closely to the default diff tool of git vimdiff is linediff.vim.

Cheetsheet

  • ^V and use movement commands to select text
  • :Linediff to put selected text into buffer
  • repeat for second snippet
  • Use diffget and diffput and rest of vimdiff commands as usual
  • Save both buffers once done

Evaluating mesh volume from surface

One of the first tests one needs to implement when generating meshes is a volume test. The main idea like in many other tests is to evaluate the same quantity in two different ways and then compare the results. If everything is correct the results should be close up to some rounding precision error.

When dealing with surface vs volume calculation a familiar method from mutivariable calculus is the Divergence theorem [1] :

[Read More]

Removing white pixels from figures

Quite often, especially when you are working with presentations, you may realize that you need to remove the white background from your figures. One way is to use any photo editing software like Gimp for example and remove the background. But if you have a lot of files and you don’t need smoothing or any other effect you can also use a simple script like this:

#!/usr/bin/env python 
#source : http://stackoverflow.com/a/765829
from PIL import Image
import sys
white = (255, 255, 255, 255)
transparent = (255, 255, 255, 0)
for infile in sys.argv[1:]:
    img = Image.open(infile)
    img = img.convert("RGBA")

    pixdata = img.load()

    for y in xrange(img.size[1]):
        for x in xrange(img.size[0]):
            if pixdata[x, y] == white:
                pixdata[x, y] = transparent

    img.save(infile + "_modified", "PNG")

source

[Read More]