Skip to content

Instantly share code, notes, and snippets.

@reox
reox / pfilter.py
Created May 3, 2022 09:49
Paraview: Programmable Filter to display the CellType of unstructured grids
# This programmable filter can be used on an unstructured grid to display the
# numerical value of the cell type (i.e., tet, hex, wedge, ...)
output.CellData.append(inputs[0].CellTypes, 'Cell_type')
@reox
reox / stiffness.py
Last active March 22, 2022 13:50
Fun with stiffness matrices: Voigt-Reuss-Hill averages and plotting Young's modulus
"""
Fun with stiffness matrices!
The plotting routine is inspired by https://github.com/coudertlab/elate
The Voigt-Reuss-Hill Averaging was directly copied from there.
Note the following relationships of stresses and strains:
sigma_ij ... stress tensor
epsilon_ij ... strain tensor
@reox
reox / savefig_tiff_lzw.py
Created March 17, 2022 10:40
Save a figure as tiff with lzw compression
import matplotlib.pyplot as plt
# do plotting:
#plt.dosomething(...)
# pil_kwargs can be used to pass things to PIL.
# the save function has **params which then passes the kwargs to the file format save function, for example:
# See https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#saving-tiff-images
# LZW compression is called tiff_lzw in pil...
plt.savefig('foobaz.tiff', pad_inches=0.05, bbox_inches='tight', pil_kwargs=dict(compression='tiff_lzw'))
@reox
reox / faces_of_array.py
Created May 20, 2021 12:41
Query the faces of a cubical image in numpy
import numpy as np
# Numpy array is xyz
img = np.zeros((10,20,30))
a = slice(None) # all items, same as ':'
faces = {
'top': (-1, a, a),
'bottom': (0, a, a),
@reox
reox / eigen.py
Created August 5, 2020 08:34
Eigenvalues and -vectors of matrix and getting matrix back in numpy
import numpy as np
A = np.array([[2,1,2.5], [1,3,1], [2.5,1,4]])
# Eigenvalues and -vectors have the property such that: Ax = lx
# x ... Eigenvectors
# l ... Eigenvalues
l, x = np.linalg.eig(A)
# it follows:
[
{
"ColorSpace": "RGB",
"DefaultMap": true,
"Name": "Turbo",
"RGBPoints": [
0.0,
0.18823529411764706,
0.07058823529411765,
0.23137254901960785,
@reox
reox / divide by value.py
Created November 29, 2017 16:34
divide by a value, decide with a flag
# What you would normaly do:
def some_divison():
flag = True
val = 23
divisor = 42
if flag:
return val / divisor
@reox
reox / freecad_daily_build.sh
Last active March 19, 2019 01:41
Build a debian package from FreeCAD yourself.
#!/bin/bash
set -e
#################################
# NOTE:
# This assumes you have a user `builder` and a folder `/home/builder/packages`.
# it will clone FreeCAD there and start pbuilder using pdebuild.
# For me this works using debian sid. (stretch/buster will probably not work!)
#
# After the build has finished, it will import the packes into a reprepro
# which is setup at /srv/repo
@reox
reox / ReplaceNonNullPlacements.FCMacro
Last active November 2, 2017 21:17
FreeCAD Macro to replace all placements with Nullplacements. This is important when using Assembly2 Workbench, to allow correct placement there.
print("Checking replacing all placements with default.")
print("===============================================")
for b in App.activeDocument().findObjects("PartDesign::Body"):
if not b.Placement.isNull():
print("!!! Body {} has a non null placement --> resetting!".format(b.Label))
b.Placement.Base = FreeCAD.Base.Vector()
b.Placement.Rotation = FreeCAD.Base.Rotation()
else:
print("Body {} looks fine...".format(b.Label))
@reox
reox / linear.py
Created July 11, 2017 07:37
Linear Algebra with scipy
import numpy as np
from itertools import combinations
import scipy.linalg
x = [1.2, 1.3, 1.6, 2.5, 2.3, 2.8]
y = [167.0, 180.3, 177.8, 160.4, 179.6, 154.3]
z = [-0.3, -0.8, -0.75, -1.21, -1.65, -0.68]
f = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]).transpose()
G = np.c_[x, y, z]