def createReducedMatrices(U_POD,U_DEIM,PT,p,path): ''' creates 12 matrices for the reduced system with V as basis and saves them Parameters ------- U_POD : 2-D array pod base vectors U_DEIM : 2-D array deim base vectors PT : 2-D array DEIM index matrix p : array DEIM indices path : str path to directory to save the base matrices Returns ------- saves the base matrices in the path directory ''' np.save(path + "U_POD_truncated.npy",U_POD) np.save(path + "U_DEIM_truncated.npy",U_DEIM) np.save(path + "PT.npy",PT) np.save(path + "p.npy",p) P = np.dot(U_DEIM,la.inv(np.dot(PT.T,U_DEIM))) for i in range(0,12): Ai = io.read_PETSc_mat('data/TMM/2.8/Transport/Matrix5_4/1dt/Ai_'+str(i).zfill(2)+'.petsc') Ae = io.read_PETSc_mat('data/TMM/2.8/Transport/Matrix5_4/1dt/Ae_'+str(i).zfill(2)+'.petsc') #Ai = sp.sparse.block_diag((Ai,Ai))y #Ae = sp.sparse.block_diag((Ae,Ae)) Ar = U_POD.T.dot(Ai.dot(Ae.dot(U_POD))) Pr = U_POD.T.dot(Ai.dot(P)) np.save(path + 'reduced_A' +str(i).zfill(2), Ar) np.save(path + 'reduced_P' +str(i).zfill(2), Pr)
def test(self,nspinup,ntimestep): y = np.ones(52749,dtype=np.float_) * 2.17 #load high dim matrices Ae = [] Ai = [] print("load matrices") for i in range(12): print(i) Ai.append(io.read_PETSc_mat('data/TMM/2.8/Transport/Matrix5_4/1dt/Ai_'+str(i).zfill(2)+'.petsc')) Ae.append(io.read_PETSc_mat('data/TMM/2.8/Transport/Matrix5_4/1dt/Ae_'+str(i).zfill(2)+'.petsc')) #check if q is zero in fortran routine q = np.zeros(52749,dtype=np.float_) t = 0 #q_select = np.zeros(p.shape[0],dtype=np.float_) starttime =time.time() for spin in range(nspinup): for step in range(ntimestep): t = np.fmod(0 + step*self.dt, 1.0); counter = 0 for i in range(4448): self.bc[0] = self.lat[i] self.bc[1] = self.interpolation_a[step]*self.fice[i,self.interpolation_j[step]] + self.interpolation_b[step]*self.fice[i,self.interpolation_k[step]] q[self.J[i]:self.J[i+1]] = modeln.metos3dbgc(self.dt,t,y[self.J[i]:self.J[i+1]],self.u,self.bc,self.dc[self.J[i]:self.J[i+1],:])[:,0] #print("q:", q[self.J[i]:self.J[i+1]]) Aiint = self.interpolation_a[step]*Ai[self.interpolation_j[step]] + self.interpolation_b[step]*Ai[self.interpolation_k[step]] Aeint = self.interpolation_a[step]*Ae[self.interpolation_j[step]] + self.interpolation_b[step]*Ae[self.interpolation_k[step]] #Aey = io.read_PETSc_vec("simulation/compare/Aey_sp%.4dts%.4dN.petsc" % (spin,step)) #Aeq = io.read_PETSc_vec("simulation/compare/Ae+q_sp%.4dts%.4dN.petsc" % (spin,step)) #q_v = io.read_PETSc_vec("simulation/compare/q_sp%.4dts%.4dN.petsc" % (spin,step)) #Aiint_metos = io.read_PETSc_mat("simulation/compare/A%.4d.petsc" % (step)) #print("norm A interplaton: ", (Aiint-Aiint_metos)) ye = Aeint.dot(y) yeq = ye +q #io.write_PETSc_vec(yeq,"yeqts%.4dN.petsc" % step) # A_saved = io.read_PETSc_mat("Ai_interpolatedts%.4d.petsc" % step) y_j = Aiint.dot(yeq) #print("q:", np.linalg.norm(q_v-q)) #print("before Ai:", np.linalg.norm(Aeq-yeq)) #print(step,spin) if(step == 2879): v = io.read_PETSc_vec("simulation/POD_DEIM/sp%.4dts%.4dN.petsc" % (spin,step)) print("time: ", time.time() - starttime ,spin,step,np.linalg.norm(y-v)) io.write_PETSc_vec(y_j,"simulation/compare/exp01/sp%.4dts%.4dN.petsc" % (spin,step)) starttime = time.time() #io.write_PETSc_mat(Aiint,"Ai%.4dN.petsc" % step) # print(Aiint-A_saved.T) y = y_j
def Init(self): #boundary and domain condition self.lat = io.read_PETSc_vec(self.config["-Metos3DBoundaryConditionInputDirectory"][0] + self.config["-Metos3DLatitudeFileFormat"][0]) dz = io.read_PETSc_vec(self.config["-Metos3DDomainConditionInputDirectory"][0] + self.config["-Metos3DLayerHeightFileFormat"][0]) z = io.read_PETSc_vec(self.config["-Metos3DDomainConditionInputDirectory"][0] + self.config["-Metos3DLayerDepthFileFormat"][0]) self.lsm = io.read_PETSc_mat(self.config["-Metos3DProfileInputDirectory"][0] + self.config["-Metos3DProfileMaskFile"][0]) self.fice = np.zeros((self.profiles,np.int_(self.config["-Metos3DIceCoverCount"][0])),dtype=np.float_) for i in range(np.int_(self.config["-Metos3DIceCoverCount"][0])): self.fice[:,i] = io.read_PETSc_vec(self.config["-Metos3DBoundaryConditionInputDirectory"][0] + (self.config["-Metos3DIceCoverFileFormat"][0] % i)) self.bc = np.zeros(2,dtype=np.float_) self.dc = np.zeros((self.ny,2),dtype=np.float_) self.dc[:,0] = z self.dc[:,1] = dz self.u = np.array(self.config["-Metos3DParameterValue"],dtype=np.float_) self.dt = np.float_(self.config["-Metos3DTimeStep"][0]) self.nspinup = np.int_(self.config["-Metos3DSpinupCount"][0]) self.ntimestep = np.int_(self.config["-Metos3DTimeStepCount"][0]) self.matrixCount = np.int_(self.config["-Metos3DMatrixCount"][0]) self.U_PODN = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixPODFileFormat"][0]) self.U_PODDOP = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixPODFileFormat"][0]) self.U_DEIMN = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixDEIMFileFormat"][0]) self.U_DEIMDOP = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixDEIMFileFormat"][0]) self.DEIM_IndicesN = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DDEIMIndicesFileFormat"][0]) self.DEIM_IndicesDOP = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DDEIMIndicesFileFormat"][0]) self.AN = np.ndarray(shape=(self.matrixCount,self.U_PODN.shape[1],self.U_PODN.shape[1]), dtype=np.float_, order='C') self.ADOP = np.ndarray(shape=(self.matrixCount,self.U_PODDOP.shape[1],self.U_PODDOP.shape[1]), dtype=np.float_, order='C') for i in range(0,self.matrixCount): self.AN[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixReducedFileFormat"][0] % i) self.ADOP[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixReducedFileFormat"][0] % i) self.PN = np.ndarray(shape=(self.matrixCount,self.U_PODN.shape[1],self.U_DEIMN.shape[1]), dtype=np.float_, order='C') self.PDOP = np.ndarray(shape=(self.matrixCount,self.U_PODDOP.shape[1],self.U_DEIMDOP.shape[1]), dtype=np.float_, order='C') for i in range(0,self.matrixCount): self.PN[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixReducedDEINFileFormat"][0] % i) self.PDOP[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixReducedDEINFileFormat"][0] % i) #precomputin the interplaton indices for a year [self.interpolation_a,self.interpolation_b,self.interpolation_j,self.interpolation_k] = util.linearinterpolation(2880,12,0.0003472222222222) self.yN = np.ones(self.ny,dtype=np.float_) * np.float_(self.config["-Metos3DTracerInitValue"])[0] self.yDOP = np.ones(self.ny,dtype=np.float_) * np.float_(self.config["-Metos3DTracerInitValue"])[1] self.y_redN = np.dot(self.U_PODN.T,self.yN) self.y_redDOP = np.dot(self.U_PODDOP.T,self.yDOP) self.qN = np.zeros(self.DEIM_IndicesN.shape[0],dtype=np.float_) self.qDOP = np.zeros(self.DEIM_IndicesDOP.shape[0],dtype=np.float_) self.J,self.PJ = util.generateIndicesForNonlinearFunction(self.lsm,self.profiles,self.ny) self.out_pathN = self.config["-Metos3DTracerOutputDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][0] self.out_pathDOP = self.config["-Metos3DTracerOutputDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][1] self.monitor_path = self.config["-Metos3DTracerMointorDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][0]
def Init(self): #boundary and domain condition self.lat = io.read_PETSc_vec(self.config["-Metos3DBoundaryConditionInputDirectory"][0] + self.config["-Metos3DLatitudeFileFormat"][0]) dz = io.read_PETSc_vec(self.config["-Metos3DDomainConditionInputDirectory"][0] + self.config["-Metos3DLayerHeightFileFormat"][0]) z = io.read_PETSc_vec(self.config["-Metos3DDomainConditionInputDirectory"][0] + self.config["-Metos3DLayerDepthFileFormat"][0]) self.lsm = io.read_PETSc_mat(self.config["-Metos3DProfileInputDirectory"][0] + self.config["-Metos3DProfileMaskFile"][0]) self.fice = np.zeros((self.profiles,np.int_(self.config["-Metos3DIceCoverCount"][0])),dtype=np.float_) for i in range(np.int_(self.config["-Metos3DIceCoverCount"][0])): self.fice[:,i] = io.read_PETSc_vec(self.config["-Metos3DBoundaryConditionInputDirectory"][0] + (self.config["-Metos3DIceCoverFileFormat"][0] % i)) self.bc = np.zeros(2,dtype=np.float_) self.dc = np.zeros((self.ny,2),dtype=np.float_) self.dc[:,0] = z self.dc[:,1] = dz self.u = np.array(self.config["-Metos3DParameterValue"],dtype=np.float_) self.dt = np.float_(self.config["-Metos3DTimeStep"][0]) self.nspinup = np.int_(self.config["-Metos3DSpinupCount"][0]) self.ntimestep = np.int_(self.config["-Metos3DTimeStepCount"][0]) self.matrixCount = np.int_(self.config["-Metos3DMatrixCount"][0]) self.U_POD = np.load(self.config["-Metos3DMatrixInputDirectory"][0] + self.config["-Metos3DMatrixPODFileFormat"][0]) self.U_DEIM = np.load(self.config["-Metos3DMatrixInputDirectory"][0] + self.config["-Metos3DMatrixDEIMFileFormat"][0]) self.DEIM_Indices = np.load(self.config["-Metos3DMatrixInputDirectory"][0] + self.config["-Metos3DDEIMIndicesFileFormat"][0]) self.A = np.ndarray(shape=(self.matrixCount,self.U_POD.shape[1],self.U_POD.shape[1]), dtype=np.float_, order='C') for i in range(0,self.matrixCount): self.A[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] + self.config["-Metos3DMatrixReducedFileFormat"][0] % i) self.P = np.ndarray(shape=(self.matrixCount,self.U_POD.shape[1],self.U_DEIM.shape[1]), dtype=np.float_, order='C') for i in range(0,self.matrixCount): self.P[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] + self.config["-Metos3DMatrixReducedDEINFileFormat"][0] % i) #precomputin the interplaton indices for a year [self.interpolation_a,self.interpolation_b,self.interpolation_j,self.interpolation_k] = util.linearinterpolation(2880,12,0.0003472222222222) self.y = np.ones(self.ny,dtype=np.float_) * np.float_(self.config["-Metos3DTracerInitValue"])[0] self.y_red = np.dot(self.U_POD.T,self.y) self.q = np.zeros(self.DEIM_Indices.shape[0],dtype=np.float_) self.J,self.PJ = util.generateIndicesForNonlinearFunction(self.lsm,self.profiles,self.ny) self.out_path = self.config["-Metos3DTracerOutputDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][0] self.monitor_path = self.config["-Metos3DTracerMointorDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][0]