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 simulate(self): starttime = time.time() timings = np.zeros(self.nspinup) bgcTime = np.zeros(self.ntimestep) #bgcStepTime = np.zeros(self.ntimestep) multTime = np.zeros(self.ntimestep) interpolationTime = np.zeros(self.ntimestep) for spin in range(self.nspinup): for step in range(self.ntimestep): if ((step % 240 == 239) & (step > 0)): matrixIndex = np.int(np.floor(step / 240)) y = np.dot(self.U_POD[(matrixIndex - 1) % 12, :], self.y_red) y_hig = io.read_PETSc_vec(self.monitor_path % (spin, step)) io.write_PETSc_vec(y, self.out_path % (spin, step)) print("time: ", time.time() - starttime, "spin: ", spin, "step: ", step, "t:", self.t, "error norm: ", np.linalg.norm(y - y_hig), "spinup norm: ", np.linalg.norm(y - self.y)) timings[spin] = time.time() - starttime starttime = time.time() self.y = y #bgcTime[step],multTime[step],interpolationTime[step] = self.__TimeStep(step) self.__TimeStep(step) #np.save(self.out_path %(spin,step) + "_bgc_timeings.npy",bgcTime) #np.save(self.out_path %(spin,step) + "_bgcStep_timeings.npy",bgcStepTime) #np.save(self.out_path %(spin,step) + "_mult_timeings.npy",multTime) #np.save(self.out_path %(spin,step) + "_interpolation_timeings.npy",interpolationTime) np.save(self.out_path % (spin, step) + "_timeings.npy", timings)
def simulate(self): starttime = time.time() timings = np.zeros(self.nspinup) bgcTime = np.zeros(self.ntimestep) #bgcStepTime = np.zeros(self.ntimestep) multTime = np.zeros(self.ntimestep) interpolationTime = np.zeros(self.ntimestep) for spin in range(self.nspinup): for step in range(self.ntimestep): if((step == 2879) & (step > 0)): y = self.U_POD.dot(self.y_red) y_hig = io.read_PETSc_vec(self.monitor_path % (spin,step)) io.write_PETSc_vec(y, self.out_path % (spin,step)) print("time: ", time.time() - starttime ,"spin: ", spin,"step: ", step,"t:", self.t, "spinup norm: ", np.linalg.norm(y-self.y), "norm: ", np.linalg.norm(y_hig -y)) timings[spin] = time.time() - starttime starttime = time.time() self.y = y bgcTime[step],multTime[step],interpolationTime[step] = self.__TimeStep(step) #self.__TimeStep(step) np.save(self.out_path %(spin,step) + "_bgc_timeings.npy",bgcTime) np.save(self.out_path %(spin,step) + "_timeings.npy",timings) np.save(self.out_path %(spin,step) + "_mult_timeings.npy",multTime) np.save(self.out_path %(spin,step) + "_interpolation_timeings.npy",interpolationTime) np.save(self.out_path %(spin,step) + "_timeings.npy",timings)
def simulate(self): starttime = time.time() timings = np.zeros(self.nspinup) bgcTime = np.zeros(self.ntimestep) #bgcStepTime = np.zeros(self.ntimestep) multTime = np.zeros(self.ntimestep) interpolationTime = np.zeros(self.ntimestep) for spin in range(self.nspinup): for step in range(self.ntimestep): if((step % 240 == 239) & (step > 0)): matrixIndex = np.int(np.floor(step/240)) y = np.dot(self.U_POD[(matrixIndex-1) % 12,:],self.y_red) y_hig = io.read_PETSc_vec(self.monitor_path % (spin,step)) io.write_PETSc_vec(y, self.out_path % (spin,step)) print("time: ", time.time() - starttime ,"spin: ", spin,"step: ", step,"t:", self.t,"error norm: ", np.linalg.norm(y-y_hig), "spinup norm: ", np.linalg.norm(y-self.y)) timings[spin] = time.time() - starttime starttime = time.time() self.y = y #bgcTime[step],multTime[step],interpolationTime[step] = self.__TimeStep(step) self.__TimeStep(step) #np.save(self.out_path %(spin,step) + "_bgc_timeings.npy",bgcTime) #np.save(self.out_path %(spin,step) + "_bgcStep_timeings.npy",bgcStepTime) #np.save(self.out_path %(spin,step) + "_mult_timeings.npy",multTime) #np.save(self.out_path %(spin,step) + "_interpolation_timeings.npy",interpolationTime) np.save(self.out_path %(spin,step) + "_timeings.npy",timings)
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]
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 constructMatrix(path,outpath,pattern,nstep): ''' returns an matix constructed out of the vectos in the given directory than macht the pattern Parameters ------- path: str path to directory outpath: str path ot output directory pattern: str pattern to match files nstep: int number of vectors ''' ny = 52749 Y = np.empty([ny,np.int_(nstep)],dtype='float_') counter = 0 for i in range(nstep): Y[:,i] = io.read_PETSc_vec(path+pattern % i) np.save(outpath,Y)