In [3]:
import logging
import numpy as np
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s. %(levelname)s in %(funcName)s: %(message)s')
In [13]:
pwd
Out[13]:
In [17]:
import paco.pacoUtils as pu
In [48]:
def readVectFF(f):
"""Reads a vector in the form output by FreeFem++.
First line contains the number of entries: nbcoef.
Next lines contain the entries, with 5 entries per line,
separated by tab.
Entries are read left to right, then line by line.
"""
nbcoef = int(f.next().strip())
logging.info("attempt to read %s values", nbcoef)
arr = np.loadtxt(f)
arr = arr.flatten(order='C')
if len(arr) != nbcoef:
errMsg = "readVectFF: Inconsistent dimension, expected %d, got %d" %(nbcoef, len(arr))
logging.error(errMsg)
raise RuntimeError(errMsg)
logging.info("successfully read %s values", nbcoef)
return arr
In [49]:
with open('..\generate\u.txt', 'r') as f:
x = readVectFF(f)
In [50]:
x
Out[50]:
In [51]:
len(x)
Out[51]:
In [55]:
def readSparseFF(f):
"""Reads a sparse matrix in the form (Morse) output by FreeFem++.
First line contains: n m (is symmetic) nbcoef
Next lines contain, for each nonzero coefficient: i j a_ij where (i,j) \in {1,...,n}x{1,...,m}
"""
# strip comment header
row = f.next().strip()
while row[0] == '#':
row = f.next().strip()
# read shape
n, m, issym, nbcoef = map(int, row.split())
logging.info("sparse (%s x %s), sym? %s, attempt to read %s values", n, m, issym, nbcoef)
# read nzeros
ijv = np.loadtxt(f)
nrows, ncols = ijv.shape
# check dims
if nrows != nbcoef:
errMsg = "Inconsistent dimensions, expected %d, got %d" %(nbcoef, nrows)
logging.error(errMsg)
raise RuntimeError(errMsg)
if ncols != 3:
errMsg = "Inconsistent dimensions, expected %d, got %d" %(3, ncols)
logging.error(errMsg)
raise RuntimeError(errMsg)
# populate matrix
res = np.zeros((n,m))
for ifloat,jfloat,v in ijv:
i, j = int(ifloat) - 1, int(jfloat) - 1 # python is 0-based
res[i,j] = v
if issym != 0:
res[j,i] = v
logging.info("successfully read %s values", nbcoef)
return res
In [56]:
with open('..\generate\stiff.txt', 'r') as f:
s = readSparseFF(f)
In [57]:
s.shape
Out[57]:
In [1]:
pwd()
Out[1]:
In [19]:
Aref = pu.read_csr_matrix(open("runs\\bay\\bayREF_s2_A.txt", "r"))
Arun = pu.read_csr_matrix(open("runs\\bay\\bayrun_s2_A.txt", "r"))
In [27]:
Aref[0:6,0:6]
Out[27]:
In [28]:
Arun[0:6,0:6]
Out[28]:
In [24]:
stiffref = pu.readSparseFF(open("runs\\bay\\bayREF_s1_stiff.txt", "r"))
stiffrun = pu.readSparseFF(open("runs\\bay\\bayrun_s1_stiff.txt", "r"))
In [25]:
np.array_equal(stiffref, stiffrun)
Out[25]:
In [29]:
Aref[0:8,0:8]-Arun[0:8,0:8]
Out[29]:
In [32]:
np.isclose(Aref[0:8,0:8],Arun[0:8,0:8])
Out[32]:
In [38]:
massref = pu.readSparseFF(open("runs\\bay\\bayREF_s1_mass.txt", "r"))
massrun = pu.readSparseFF(open("runs\\bay\\bayrun_s1_mass.txt", "r"))
massgen = pu.readSparseFF(open("generate\\mass.txt", "r"))
print(np.allclose(massref, massrun))
print(np.allclose(massref, massgen))
print(np.allclose(massrun, massgen))
In [39]:
stiffref = pu.readSparseFF(open("runs\\bay\\bayREF_s1_stiff.txt", "r"))
stiffrun = pu.readSparseFF(open("runs\\bay\\bayrun_s1_stiff.txt", "r"))
stiffgen = pu.readSparseFF(open("generate\\stiff.txt", "r"))
print(np.allclose(stiffref, stiffrun))
print(np.allclose(stiffref, stiffgen))
print(np.allclose(stiffrun, stiffgen))
In [41]:
uref = pu.readVectFF(open("runs\\bay\\bayREF_s1_u.txt", "r"))
urun = pu.readVectFF(open("runs\\bay\\bayrun_s1_u.txt", "r"))
ugen = pu.readVectFF(open("generate\\u.txt", "r"))
print(np.allclose(uref, urun))
print(np.allclose(uref, ugen))
print(np.allclose(urun, ugen))
In [42]:
vref = pu.readVectFF(open("runs\\bay\\bayREF_s1_v.txt", "r"))
vrun = pu.readVectFF(open("runs\\bay\\bayrun_s1_v.txt", "r"))
vgen = pu.readVectFF(open("generate\\v.txt", "r"))
print(np.allclose(vref, vrun))
print(np.allclose(vref, vgen))
print(np.allclose(vrun, vgen))
In [ ]: