import scipy.io as sio

from scipy.sparse import lil_matrix
from scipy.sparse import csr_matrix
from scipy.sparse import identity


#import sys
#if len(sys.argv) != 2:
#    print ("Usage: python ", sys.argv[0], " Calculixbasename")
#    quit()

#c_filename = sys.argv[1] + "_C.mtx"
#m_filename = sys.argv[1] + "_M.mtx"

m_filename = "FEMMeshNetgen" + "_M.mtx"
c_filename = "FEMMeshNetgen" + "_C.mtx"
dof_calculix_filname = "FEMMeshNetgen" + ".dof"

def calculix_dof2python_dof(dof_data, node, dof):
    query = '(Node == ' + str(node) + ' ) and (DOF == ' + str(dof) + ')'
    return(dof_data.query(query).index.item())
 

import pandas as pd
dof_calculix = pd.read_csv(dof_calculix_filname,sep = '\.', engine='python',header=None, names=['Node', 'DOF'])



C_mat=csr_matrix(sio.mmread(c_filename))
M_mat=csr_matrix(sio.mmread(m_filename))


mat_dim = C_mat.get_shape()[1]




import numpy as np

force_vec = np.zeros(mat_dim)
force_vec[calculix_dof2python_dof(dof_calculix,944,3)] = -100.


from scipy.sparse.linalg import spsolve

u = spsolve(C_mat, force_vec)

print(u[calculix_dof2python_dof(dof_calculix,3,3)])

