matrices = op4.readOP4(fname, matrixNames=matrixNames, precision='default') print("keys = %s" % (matrices.keys())) #print "#####################################################" print("fname=%s" % (fname)) for name, (form, matrix) in sorted(matrices.items()): print("-----------------------------------") print("name = |%s|" % (name)) if isinstance(matrix, coo_matrix): print("SPARSE") #matrix = matrix.todense() #print print_annotated_matrix(matrix) else: print("DENSE") print(print_matrix(matrix)) #if 't' in fname: #f.write(op4.writeDenseMatrixAscii(name,matrix,form=form,precision='default')) if isinstance(matrix, coo_matrix): op4.writeSparseMatrixAscii(f, name, matrix, form=form, precision='default', isBigMat=isBigMat) else: f.write(op4.writeDenseMatrixAscii(name, matrix, 1, 'single')) #f.write(op4.writeDenseMatrixBinary(name,matrix,1,'single')) #print print_annotated_matrix(matrices['STRINGS'][1]-strings) print("-----------------------------")
def readMatrixBinary(self, f, floatType, matrixNames=None): """reads a matrix""" #print self.printSection(60) #print "*************************" data = f.read(4) self.n += 4 (recordLength, ) = unpack(self.endian + 'i', data) assert self.n == f.tell(), 'n=%s tell=%s' % (self.n, f.tell()) #print "RL = %s" %(recordLength) #print self.printSection(60) if recordLength == 24: data = f.read(recordLength) self.n += recordLength (ncols, nrows, form, Type, name) = unpack(self.endian + '4i8s', data) #print "nrows=%s ncols=%s form=%s Type=%s name=%s" %(nrows,ncols,form,Type,name) else: msg = recordLength + self.printBlock(data) raise NotImplementedError('recordLength=%s\n%s' % (msg)) name = name.strip() if 0: if Type == 1: print("Type = Real, Single Precision") elif Type == 2: print("Type = Real, Double Precision") elif Type == 3: print("Type = Complex, Single Precision") elif Type == 4: print("Type = Complex, Double Precision") if nrows < 0: # if less than 0, big isBigMat = True nrows = abs(nrows) elif nrows > 0: isBigMat = False else: raise RuntimeError('unknown BIGMAT. nRows=%s' % (nrows)) # jump forward to get if isSparse, then jump back nSave = self.n (_a, _icol, _irow, _nWords) = self.readStartMarker(f) f.seek(nSave) self.n = nSave (NWV, NBW, d, dType) = self.getMatrixInfo(Type) isSparse = False if _irow == 0: isSparse = True # rows = [] # cols = [] # entries = [] #else: # A = zeros((nrows,ncols),dType) #A = zeros((nrows,ncols),dType) assert self.n == f.tell(), 'n=%s tell=%s' % (self.n, f.tell()) if Type in [1, 2]: # real A = self.readRealBinary(f, nrows, ncols, Type, isSparse, isBigMat) elif Type in [3, 4]: # complex A = self.readComplexBinary(f, nrows, ncols, Type, isSparse, isBigMat) else: raise RuntimeError("Type=%s" % (Type)) try: print_matrix(A.todense()) except: pass if d in ['d', 'dd']: f.read(8) self.n += 8 elif d in ['f', 'ff']: f.read(4) self.n += 4 else: raise NotImplementedError(d) #f.read(recordLength); self.n+=recordLength #self.printSection(10) #f.read(4); self.n+=4 #if isSparse: # Initialize a real matrix # A = coo_matrix( (entries,(rows,cols)),shape=(nrows,ncols),dtype=dType) #print '------end1--------' #print self.printSection(60) #print '------end2--------' return (name, form, A)
def setupSOL101(self, model, case): # the (GridID,componentID) -> internalID (self.nidComponentToID, i) = self.buildNidComponentToID(model) (Kgg, Fg, isSPC, isMPC) = self.build_Kgg_Fg(model, case, self.nidComponentToID, i) (self.IDtoNidComponents) = reverseDict(self.nidComponentToID) print("IDtoNidComponents = ", self.IDtoNidComponents) print("Kgg =\n" + print_annotated_matrix(Kgg, self.IDtoNidComponents)) #print("Kgg = \n", Kgg) #print("iSize = ", i) #(Kaa,Fa) = self.Partition(Kgg) #sys.exit('verify Kgg') Kaa = partition_dense_symmetric(Kgg, self.iUs) print("Kaa = \n%s" % (print_matrix(Kaa))) #print("Kaa.shape = ",Kaa.shape) #sys.exit('verify Kaa') Fa = partition_dense_vector(Fg, self.iUs) #print("Kaa = \n%s" %(print_matrix(Kaa))) print("Fg = ", Fg) print("Fa = ", Fa) print("Us = ", self.Us) self.Us = array(self.Us, 'float64') self.Um = array(self.Um, 'float64') zero = array([]) MPCgg = zero Ksa = Kas = Kss = Cam = Cma = Kma = Kam = Kmm = Kms = Ksm = zero Kaa1 = Kaa2 = zero Fm = Fs = zero #Kaa = partition_dense_matrix(Kgg,iFree) Kaa0 = Kaa Fa0 = Fa if isSPC: #Fs = partition_dense_vector(Fg,self.iUs) Ksa = partition_dense_matrix(Kgg, self.iUs, iFree) Kas = Ksa.transpose() Kss = partition_dense_matrix(Kgg, self.iUs) if isMPC: Fm = partition_dense_vector(Fg, self.iUm) Cam = partition_dense_matrix(MPCgg, iFree) Cma = partition_dense_matrix(MPCgg, self.iUm) Kaa1 = Cam * Kmm * Cma Kaa2 = Cam * Kma + Kam * Cma assert Cam.transpose() == Cma Kma = partition_dense_matrix(Kgg, self.iUm, iFree) Kam = Kma.transpose() Kmm = partition_dense_matrix(Kgg, self.iUm) if isSPC: Kms = partition_dense_matrix(Kgg, self.iUm, self.iUs) Ksm = Kms.transpose() Fa = Fa0 # + Cam*Fm Kaa = Kaa0 # +Kaa1+Kaa2 Ua = self.solve(Kaa, Fa) #self.Um = Kma*Ua return(Ua, i)
def setupSOL101(self, model, case): # the (GridID,componentID) -> internalID (self.nidComponentToID, i) = self.buildNidComponentToID(model) (Kgg, Fg, isSPC, isMPC) = self.build_Kgg_Fg(model, case, self.nidComponentToID, i) (self.IDtoNidComponents) = reverseDict(self.nidComponentToID) print("IDtoNidComponents = ", self.IDtoNidComponents) print("Kgg =\n" + print_annotated_matrix(Kgg, self.IDtoNidComponents)) #print("Kgg = \n", Kgg) #print("iSize = ", i) #(Kaa,Fa) = self.Partition(Kgg) #sys.exit('verify Kgg') Kaa = partition_dense_symmetric(Kgg, self.iUs) print("Kaa = \n%s" % (print_matrix(Kaa))) #print("Kaa.shape = ",Kaa.shape) #sys.exit('verify Kaa') Fa = partition_dense_vector(Fg, self.iUs) #print("Kaa = \n%s" %(print_matrix(Kaa))) print("Fg = ", Fg) print("Fa = ", Fa) print("Us = ", self.Us) self.Us = array(self.Us, 'float64') self.Um = array(self.Um, 'float64') zero = array([]) MPCgg = zero Ksa = Kas = Kss = Cam = Cma = Kma = Kam = Kmm = Kms = Ksm = zero Kaa1 = Kaa2 = zero Fm = Fs = zero #Kaa = partition_dense_matrix(Kgg,iFree) Kaa0 = Kaa Fa0 = Fa if isSPC: #Fs = partition_dense_vector(Fg,self.iUs) Ksa = partition_dense_matrix(Kgg, self.iUs, iFree) Kas = Ksa.transpose() Kss = partition_dense_matrix(Kgg, self.iUs) if isMPC: Fm = partition_dense_vector(Fg, self.iUm) Cam = partition_dense_matrix(MPCgg, iFree) Cma = partition_dense_matrix(MPCgg, self.iUm) Kaa1 = Cam * Kmm * Cma Kaa2 = Cam * Kma + Kam * Cma assert Cam.transpose() == Cma Kma = partition_dense_matrix(Kgg, self.iUm, iFree) Kam = Kma.transpose() Kmm = partition_dense_matrix(Kgg, self.iUm) if isSPC: Kms = partition_dense_matrix(Kgg, self.iUm, self.iUs) Ksm = Kms.transpose() Fa = Fa0 # + Cam*Fm Kaa = Kaa0 # +Kaa1+Kaa2 Ua = self.solve(Kaa, Fa) #self.Um = Kma*Ua return (Ua, i)
def readMatrixBinary(self, f, floatType, matrixNames=None): """reads a matrix""" #print self.printSection(60) #print "*************************" data = f.read(4) self.n += 4 (recordLength,) = unpack(self.endian + 'i', data) assert self.n == f.tell(), 'n=%s tell=%s' % (self.n, f.tell()) #print "RL = %s" %(recordLength) #print self.printSection(60) if recordLength == 24: data = f.read(recordLength) self.n += recordLength (ncols, nrows, form, Type, name) = unpack( self.endian + '4i8s', data) #print "nrows=%s ncols=%s form=%s Type=%s name=%s" %(nrows,ncols,form,Type,name) else: msg = recordLength + self.printBlock(data) raise NotImplementedError('recordLength=%s\n%s' % (msg)) name = name.strip() if 0: if Type == 1: print("Type = Real, Single Precision") elif Type == 2: print("Type = Real, Double Precision") elif Type == 3: print("Type = Complex, Single Precision") elif Type == 4: print("Type = Complex, Double Precision") if nrows < 0: # if less than 0, big isBigMat = True nrows = abs(nrows) elif nrows > 0: isBigMat = False else: raise RuntimeError('unknown BIGMAT. nRows=%s' % (nrows)) # jump forward to get if isSparse, then jump back nSave = self.n (_a, _icol, _irow, _nWords) = self.readStartMarker(f) f.seek(nSave) self.n = nSave (NWV, NBW, d, dType) = self.getMatrixInfo(Type) isSparse = False if _irow == 0: isSparse = True # rows = [] # cols = [] # entries = [] #else: # A = zeros((nrows,ncols),dType) #A = zeros((nrows,ncols),dType) assert self.n == f.tell(), 'n=%s tell=%s' % (self.n, f.tell()) if Type in [1, 2]: # real A = self.readRealBinary(f, nrows, ncols, Type, isSparse, isBigMat) elif Type in [3, 4]: # complex A = self.readComplexBinary( f, nrows, ncols, Type, isSparse, isBigMat) else: raise RuntimeError("Type=%s" % (Type)) try: print_matrix(A.todense()) except: pass if d in ['d', 'dd']: f.read(8) self.n += 8 elif d in ['f', 'ff']: f.read(4) self.n += 4 else: raise NotImplementedError(d) #f.read(recordLength); self.n+=recordLength #self.printSection(10) #f.read(4); self.n+=4 #if isSparse: # Initialize a real matrix # A = coo_matrix( (entries,(rows,cols)),shape=(nrows,ncols),dtype=dType) #print '------end1--------' #print self.printSection(60) #print '------end2--------' return (name, form, A)
#f = open('binary.op4','wb') matrices = op4.readOP4( fname, matrixNames=matrixNames, precision='default') print("keys = %s" % (matrices.keys())) #print "#####################################################" print("fname=%s" % (fname)) for name, (form, matrix) in sorted(matrices.items()): print("-----------------------------------") print("name = |%s|" % (name)) if isinstance(matrix, coo_matrix): print("SPARSE") #matrix = matrix.todense() #print print_annotated_matrix(matrix) else: print("DENSE") print(print_matrix(matrix)) #if 't' in fname: #f.write(op4.writeDenseMatrixAscii(name,matrix,form=form,precision='default')) if isinstance(matrix, coo_matrix): op4.writeSparseMatrixAscii(f, name, matrix, form=form, precision='default', isBigMat=isBigMat) else: f.write(op4.writeDenseMatrixAscii(name, matrix, 1, 'single')) #f.write(op4.writeDenseMatrixBinary(name,matrix,1,'single')) #print print_annotated_matrix(matrices['STRINGS'][1]-strings) print("-----------------------------") print("done") print("-----------------------------")
print('Failed to get %d-th matrix' % i) else: Name = op4fh.name[i] Format, A = matrices[Name] error = 0. #print type(a),type(A) if isinstance(A, np.ndarray): pass else: # sparse a = a.todense() A = A.todense() error = abs(a - A).max() if error > 0: print "Name = |%s|" % (Name) print('%s:' % op4fh.name[i]) print "error[%s] = %s" % (Name, error) print "cOP4:" print print_matrix(a) print "pyOP4:" print print_matrix(A) print "diff:" print print_matrix(a - A) print '----------------------------' sys.exit('stopping')