Skip to content

Instantly share code, notes, and snippets.

@Nidish96
Last active September 11, 2018 22:12
Show Gist options
  • Save Nidish96/5f8e8ce4b0a0a768574306e43a76ee13 to your computer and use it in GitHub Desktop.
Save Nidish96/5f8e8ce4b0a0a768574306e43a76ee13 to your computer and use it in GitHub Desktop.
A short bash script (with a Python here doc) to extract matrices from an abaqus mtx file. The test case for this is a substructure matrix output analysis outputting the mass, stiffness and some recovery matrix rows. It assumes the matrix storage to be in dense symmetric form. Avoided unnecessary conditionals for readability. Hope it helps someon…
#!/bin/sh
if [ $# = 1 ]
then
echo "Correct call!"
else
echo "Wrong call - quitting!"
return
fi
echo "Preprocessing mtx files"
awk 'BEGIN{start=0} ($1~/^\*M/){start=start+1;} ($1~/^\*\*/){start=0} (start==1 && $1!~/^\*/){print}' $1|awk 'BEGIN{RS=",";ORS="\n"}{print}'|awk '(NF!=0){print}' > ./STIFFNESS.mtx
awk 'BEGIN{start=0} ($1~/^\*M/){start=start+1;} ($1~/^\*\*/){start=0} (start==2 && $1!~/^\*/){print}' $1|awk 'BEGIN{RS=",";ORS="\n"}{print}'|awk '(NF!=0){print}' > ./MASS.mtx
awk 'BEGIN{start=0;op=0} ($1~/^\*M/){start=1} (start==1 && $1~/\*\*/){print $0}' $1|cut --complement -d ' ' -f1|awk 'BEGIN{FS=".";OFS="."}(NF>=2){print}'|awk 'BEGIN{RS=",\n";ORS=" "}{print}'|awk '(NF>0){print}'|awk 'BEGIN{RS=", ";ORS=" "}{print}'|awk '(NF>0){print}' > RECOV.mtx
echo "Preprocessing mtx files done"
python <<EOF
import numpy as np
import scipy.io as io
Mv = np.loadtxt('MASS.mtx');
Kv = np.loadtxt('STIFFNESS.mtx');
R = np.loadtxt('RECOV.mtx');
print("Reading mtx arrays done")
Nelm = len(Mv);
Nelk = len(Kv);
if (Nelm!=Nelk):
sys.exit("GIGO - Mass & Stiffness not of same length.");
Nel = Nelm;
Nd = np.int((np.sqrt(1+8*Nel)-1)/2); # Solution of Nd(Nd+1)/2-Nel = 0
M = np.zeros((Nd,Nd));
K = np.zeros((Nd,Nd));
(xi,yi) = np.tril_indices(Nd);
M[xi,yi] = Mv;
M[yi,xi] = Mv;
K[xi,yi] = Kv;
K[yi,xi] = Kv;
print("Matrix extraction done - writing mat file")
dict = {"M": M, "K": K, "R": R};
io.savemat("OUT.mat",dict);
print("Processing Over")
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment