def testRValNumpyArray(): x = np.arange(0, 1., 0.2) print((sys.getrefcount(x))) a = pg.RVector(x) print(a) # should return 2 (self & counter) since the counter is not increased # while conversion print((sys.getrefcount(x))) x = np.arange(0, 1., 0.2, dtype=np.float64) a = pg.RVector(x) print(('pg.RVector(x):', a)) x = np.array(a) a = pg.RVector(x) print(('pg.RVector(array):', a)) a = pg.RVector([0.2, 0.3, 0.4, 0.5, 0.6]) print(('pg.RVector(list[float]):', a)) a = pg.RVector((0.2, 0.3, 0.4, 0.5, 0.6)) print(('pg.RVector(tuple(float,)):', a)) a = pg.RVector(np.arange(0, 1., 0.2)) print(a) a = pg.RVector(np.arange(10.)) print(a) print((pg.norm(np.arange(10.))))
def testSlices(): a = pg.RVector(np.arange(10.)) print((a[0:3:1], np.arange(10.)[0:3:1])) print((np.arange(10.)[0:3:-1])) try: print((a[0:3:-1])) except: print("oki") pass print((a[3:0:-2])) print((pg.norm(a[0:3:1])))
def testRValNumpyArray(): x = np.arange(0, 1., 0.2 ) print(sys.getrefcount(x)) a=pg.RVector(x) print(a) # should return 2 (self & counter) since the counter is not increased while conversion print(sys.getrefcount(x)) x = np.arange(0, 1., 0.2, dtype=np.float64) a=pg.RVector(x) print(a) a=pg.RVector([0.2, 0.3, 0.4, 0.5]) print(a) a=pg.RVector(np.arange(0, 1., 0.2 )) print(a) a=pg.RVector(np.arange(10.)) print(a) print(pg.norm(np.arange(10.)))
fop = DCMultiElectrodeModellingC(mesh, data, verbose=True) print(dir(fop)) print(fop.jacobian()) print(fop.jacobian().rows()) #fop = pb.DCMultiElectrodeModelling(mesh, data) fop.regionManager().region(1).setBackground(True) fop.createRefinedForwardMesh(refine=True, pRefine=False) cData = pb.getComplexData(data) mag = pg.abs(cData) phi = -pg.phase(cData) print(pg.norm(mag-data('rhoa'))) print(pg.norm(phi-data('ip')/1000)) inv = pg.Inversion(pg.cat(mag, phi), fop, verbose=True, dosave=True) dataTrans = pg.trans.TransCumulative() datRe = pg.trans.TransLog() datIm = pg.trans.Trans() dataTrans.add(datRe, data.size()) dataTrans.add(datIm, data.size()) modRe = pg.trans.TransLog() modIm = pg.trans.TransLog()
def solveStokes_NEEDNAME(mesh, velBoundary, preBoundary=[], viscosity=1, pre0=None, vel0=None, tol=1e-4, maxIter=1000, verbose=1): """ """ velocityRelaxation = 0.5 pressureRelaxation = 0.8 pressureCoeff = None preCNorm = [] divVNorm = [] velBoundaryX = [[marker, vel[0]] for marker, vel in velBoundary] velBoundaryY = [[marker, vel[1]] for marker, vel in velBoundary] print(velBoundaryX) class WS: pass wsux = WS() wsuy = WS() wsp = WS() pressure = None if pre0 is None: pressure = np.zeros(mesh.cellCount()) else: pressure = np.array(pre0) velocity = None if vel0 is None: velocity = np.zeros((mesh.cellCount(), mesh.dimension())) else: velocity = np.array(vel0) mesh.createNeighbourInfos() CtB = mesh.cellToBoundaryInterpolation() controlVolumes = CtB * mesh.cellSizes() for i in range(maxIter): pressureGrad = cellDataToCellGrad(mesh, pressure, CtB) # __d('vx', pressureGrad[:,0]) velocity[:, 0] = solveFiniteVolume(mesh, a=viscosity, f=-pressureGrad[:, 0], uBoundary=velBoundaryX, uL=velocity[:, 0], relax=velocityRelaxation, ws=wsux) # for s in wsux.S: # print(s) # __d('rhs', wsux.rhs, 1) # __d('ux', velocity[:,0]) velocity[:, 1] = solveFiniteVolume(mesh, a=viscosity, f=-pressureGrad[:, 1], uBoundary=velBoundaryY, uL=velocity[:, 1], relax=velocityRelaxation, ws=wsuy) ap = np.array(wsux.ap * mesh.cellSizes()) apF = CtB * ap uxF = CtB * velocity[:, 0] uyF = CtB * velocity[:, 1] applyBoundaryValues(uxF, mesh, velBoundaryX) applyBoundaryValues(uyF, mesh, velBoundaryY) pxF = CtB * pressureGrad[:, 0] pyF = CtB * pressureGrad[:, 1] pF2 = cellDataToBoundaryGrad(mesh, pressure, pressureGrad) velXF = uxF + controlVolumes / apF * (pxF - pF2[:, 0]) velYF = uyF + controlVolumes / apF * (pyF - pF2[:, 1]) applyBoundaryValues(velXF, mesh, velBoundaryX) applyBoundaryValues(velYF, mesh, velBoundaryY) if pressureCoeff is None: pressureCoeff = 1. / apF * mesh.boundarySizes() * \ boundaryToCellDistances(mesh) div = -divergence(mesh, np.vstack([velXF, velYF]).T) pressureCorrection = solveFiniteVolume(mesh, a=pressureCoeff, f=div, uBoundary=preBoundary, ws=wsp) pressure += pressureCorrection * pressureRelaxation pressureCorrectionGrad = cellDataToCellGrad(mesh, pressureCorrection, CtB) velocity[:, 0] -= pressureCorrectionGrad[:, 0] / ap * mesh.cellSizes() velocity[:, 1] -= pressureCorrectionGrad[:, 1] / ap * mesh.cellSizes() preCNorm.append(pg.norm(pressureCorrection)) divVNorm.append(pg.norm(div)) # __d('div', div) # if ( i == 1): # sd if verbose: # print("\rIter: " + str(i) + " div V=" + str(divVNorm[-1]) + " " + # str(preCNorm[-1]), end='') print("\r" + str(i) + " div V=" + str(divVNorm[-1])) if i == maxIter or divVNorm[-1] < tol: break if verbose: print(str(i) + ": " + str(preCNorm[-1])) return velocity, pressure, preCNorm, divVNorm
fop = DCMultiElectrodeModellingC(mesh, data, verbose=True) print(dir(fop)) print(fop.jacobian()) print(fop.jacobian().rows()) #fop = pb.DCMultiElectrodeModelling(mesh, data) fop.regionManager().region(1).setBackground(True) fop.createRefinedForwardMesh(refine=True, pRefine=False) cData = pb.getComplexData(data) mag = pg.abs(cData) phi = -pg.phase(cData) print(pg.norm(mag-data('rhoa'))) print(pg.norm(phi-data('ip')/1000)) inv = pg.RInversion(pg.cat(mag, phi), fop, verbose=True, dosave=True) dataTrans = pg.RTransCumulative() datRe = pg.RTransLog() datIm = pg.RTrans() dataTrans.add(datRe, data.size()) dataTrans.add(datIm, data.size()) modRe = pg.RTransLog() modIm = pg.RTransLog()
def __solveStokes(mesh, viscosity, velBoundary=None, preBoundary=None, pre0=None, vel0=None, tol=1e-4, maxIter=1000, verbose=1, **kwargs): """Steady Navier-Stokes solver.""" workspace = pg.solver.WorkSpace() wsux = pg.solver.WorkSpace() wsuy = pg.solver.WorkSpace() wsp = pg.solver.WorkSpace() # get cache values if given ws = kwargs.pop('ws', None) if ws: workspace = ws if hasattr(workspace, 'wsux'): wsux = workspace.wsux else: workspace.wsux = wsux if hasattr(workspace, 'wsuy'): wsuy = workspace.wsuy else: workspace.wsuy = wsuy if hasattr(workspace, 'wsp'): wsp = workspace.wsp else: workspace.wsp = wsp velocityRelaxation = kwargs.pop('vRelax', 0.5) pressureRelaxation = kwargs.pop('pRelax', 0.8) pressureCoeff = None preCNorm = [] divVNorm = [] velBoundaryX = [] velBoundaryY = [] if velBoundary is not None: for marker, vel in velBoundary: if vel is not None: if not isinstance(vel[0], str): velBoundaryX.append([marker, vel[0]]) if not isinstance(vel[1], str): velBoundaryY.append([marker, vel[1]]) pressure = None if pre0 is None: pressure = np.zeros(mesh.cellCount()) else: pressure = np.array(pre0) velocity = None if vel0 is None: velocity = np.zeros((mesh.cellCount(), mesh.dimension())) else: velocity = np.array(vel0) mesh.createNeighbourInfos() CtB = mesh.cellToBoundaryInterpolation() controlVolumes = CtB * mesh.cellSizes() density = kwargs.pop('density', 1.0) force = kwargs.pop('f', [0.0, 0.0]) density = pg.solver.parseArgToArray(density, ndof=mesh.cellCount()) forceX = pg.solver.parseArgToArray(force[0], ndof=mesh.cellCount()) forceY = pg.solver.parseArgToArray(force[1], ndof=mesh.cellCount()) for i in range(maxIter): pressureGrad = cellDataToCellGrad(mesh, pressure, CtB) # __d('vx', pressureGrad[:,0]) velocity[:, 0] = solveFiniteVolume(mesh, a=viscosity, f=-pressureGrad[:, 0] / density + forceX, uB=velBoundaryX, uL=velocity[:, 0], relax=velocityRelaxation, ws=wsux) # for s in wsux.S: # print(s) # __d('rhs', wsux.rhs, 1) # __d('ux', velocity[:,0]) velocity[:, 1] = solveFiniteVolume(mesh, a=viscosity, f=-pressureGrad[:, 1] / density + forceY, uB=velBoundaryY, uL=velocity[:, 1], relax=velocityRelaxation, ws=wsuy) ap = np.array(wsux.ap * mesh.cellSizes()) apF = CtB * ap uxF = CtB * velocity[:, 0] uyF = CtB * velocity[:, 1] applyBoundaryValues(uxF, mesh, velBoundaryX) applyBoundaryValues(uyF, mesh, velBoundaryY) pxF = CtB * pressureGrad[:, 0] pyF = CtB * pressureGrad[:, 1] pF2 = cellDataToBoundaryGrad(mesh, pressure, pressureGrad) velXF = uxF + controlVolumes / apF * (pxF - pF2[:, 0]) velYF = uyF + controlVolumes / apF * (pyF - pF2[:, 1]) applyBoundaryValues(velXF, mesh, velBoundaryX) applyBoundaryValues(velYF, mesh, velBoundaryY) if pressureCoeff is None: pressureCoeff = 1. / apF * mesh.boundarySizes() * \ _boundaryToCellDistances(mesh) div = -mesh.divergence(np.vstack([velXF, velYF]).T) # boundsDirichlet = pg.solver.parseArgToBoundaries(preBoundary, mesh) # pB = CtB * pressure # for ix, [boundary, val] in enumerate(boundsDirichlet): # boundsDirichlet[ix][1] = -(-pg.solver.generateBoundaryValue(boundary, # val) + pB[boundary.id()]) pressureCorrection = solveFiniteVolume( mesh, a=pressureCoeff, f=div, uB=preBoundary, # uB=boundsDirichlet, ws=wsp) # print(i, pg.solver.generateBoundaryValue(mesh.boundary(58), val), # pB[58], boundsDirichlet[-1][1], # (CtB*pressureCorrection)[58]) pressure += pressureCorrection * pressureRelaxation pressureCorrectionGrad = cellDataToCellGrad(mesh, pressureCorrection, CtB) velocity[:, 0] -= pressureCorrectionGrad[:, 0] / ap * mesh.cellSizes() velocity[:, 1] -= pressureCorrectionGrad[:, 1] / ap * mesh.cellSizes() preCNorm.append(pg.norm(pressureCorrection)) divVNorm.append(pg.norm(div)) if workspace: workspace.div = div # __d('div', div) # if ( i == 1): # sd convergenceTest = 100 if i > 6: convergenceTest = (divVNorm[-1] - divVNorm[-2]) + \ (divVNorm[-2] - divVNorm[-3]) + \ (divVNorm[-3] - divVNorm[-4]) + \ (divVNorm[-4] - divVNorm[-5]) + \ (divVNorm[-5] - divVNorm[-6]) convergenceTest /= 5 if verbose: print("\r" + str(i) + " div V=" + str(divVNorm[-1]) + " ddiv V=" + str(convergenceTest)) if divVNorm[-1] > 1e6: print("\r" + str(i) + " div V=" + str(divVNorm[-1]) + " ddiv V=" + str(convergenceTest)) raise BaseException("Stokes solver seams to diverging") if i > 2: if i == maxIter or divVNorm[-1] < tol or \ abs(convergenceTest * divVNorm[-1]) < tol: break if verbose: print(str(i) + ": |pC|" + str(preCNorm[-1])) return velocity, pressure, preCNorm, divVNorm
def solveStokes(mesh, viscosity, velBoundary, preBoundary=[], pre0=None, vel0=None, tol=1e-4, maxIter=1000, verbose=1, **kwargs): """ """ ws=kwargs.pop('ws', None) velocityRelaxation = kwargs.pop('vRelax', 0.5) pressureRelaxation = kwargs.pop('pRelax', 0.8) pressureCoeff = None preCNorm = [] divVNorm = [] velBoundaryX = [[marker, vel[0]] for marker, vel in velBoundary] velBoundaryY = [[marker, vel[1]] for marker, vel in velBoundary] class WS: pass wsux = WS() wsuy = WS() wsp = WS() pressure = None if pre0 is None: pressure = np.zeros(mesh.cellCount()) else: pressure = np.array(pre0) velocity = None if vel0 is None: velocity = np.zeros((mesh.cellCount(), mesh.dimension())) else: velocity = np.array(vel0) mesh.createNeighbourInfos() CtB = mesh.cellToBoundaryInterpolation() controlVolumes = CtB * mesh.cellSizes() for i in range(maxIter): pressureGrad = cellDataToCellGrad(mesh, pressure, CtB) # __d('vx', pressureGrad[:,0]) velocity[:, 0] = solveFiniteVolume(mesh, a=viscosity, f=-pressureGrad[:, 0], uB=velBoundaryX, uL=velocity[:, 0], relax=velocityRelaxation, ws=wsux) # for s in wsux.S: # print(s) # __d('rhs', wsux.rhs, 1) # __d('ux', velocity[:,0]) velocity[:, 1] = solveFiniteVolume(mesh, a=viscosity, f=-pressureGrad[:, 1], uB=velBoundaryY, uL=velocity[:, 1], relax=velocityRelaxation, ws=wsuy) ap = np.array(wsux.ap * mesh.cellSizes()) apF = CtB * ap uxF = CtB * velocity[:, 0] uyF = CtB * velocity[:, 1] applyBoundaryValues(uxF, mesh, velBoundaryX) applyBoundaryValues(uyF, mesh, velBoundaryY) pxF = CtB * pressureGrad[:, 0] pyF = CtB * pressureGrad[:, 1] pF2 = cellDataToBoundaryGrad(mesh, pressure, pressureGrad) velXF = uxF + controlVolumes / apF * (pxF - pF2[:, 0]) velYF = uyF + controlVolumes / apF * (pyF - pF2[:, 1]) applyBoundaryValues(velXF, mesh, velBoundaryX) applyBoundaryValues(velYF, mesh, velBoundaryY) if pressureCoeff is None: pressureCoeff = 1. / apF * mesh.boundarySizes() * \ boundaryToCellDistances(mesh) # div = -divergence(mesh, np.vstack([velXF, velYF]).T) div = -mesh.divergence(np.vstack([velXF, velYF]).T) pressureCorrection = solveFiniteVolume(mesh, a=pressureCoeff, f=div, uB=preBoundary, ws=wsp) pressure += pressureCorrection * pressureRelaxation pressureCorrectionGrad = cellDataToCellGrad(mesh, pressureCorrection, CtB) velocity[:, 0] -= pressureCorrectionGrad[:, 0] / ap * mesh.cellSizes() velocity[:, 1] -= pressureCorrectionGrad[:, 1] / ap * mesh.cellSizes() preCNorm.append(pg.norm(pressureCorrection)) divVNorm.append(pg.norm(div)) if ws: ws.div=div # __d('div', div) # if ( i == 1): # sd convergenceTest = 100 if i > 6: convergenceTest = (divVNorm[-1] - divVNorm[-2]) + \ (divVNorm[-2] - divVNorm[-3]) + \ (divVNorm[-3] - divVNorm[-4]) + \ (divVNorm[-4] - divVNorm[-5]) + \ (divVNorm[-5] - divVNorm[-6]) convergenceTest /= 5 if verbose: print("\r" + str(i) + " div V=" + str(divVNorm[-1]) + " ddiv V=" + str(convergenceTest)) if i == maxIter or divVNorm[-1] < tol or \ abs(convergenceTest * divVNorm[-1]) < tol: break if verbose: print(str(i) + ": " + str(preCNorm[-1])) return velocity, pressure, preCNorm, divVNorm
def __solveStokes(mesh, viscosity, velBoundary=None, preBoundary=None, pre0=None, vel0=None, tol=1e-4, maxIter=1000, verbose=1, **kwargs): """Steady Navier-Stokes solver.""" workspace = pg.solver.WorkSpace() wsux = pg.solver.WorkSpace() wsuy = pg.solver.WorkSpace() wsp = pg.solver.WorkSpace() # get cache values if given ws = kwargs.pop('ws', None) if ws: workspace = ws if hasattr(workspace, 'wsux'): wsux = workspace.wsux else: workspace.wsux = wsux if hasattr(workspace, 'wsuy'): wsuy = workspace.wsuy else: workspace.wsuy = wsuy if hasattr(workspace, 'wsp'): wsp = workspace.wsp else: workspace.wsp = wsp velocityRelaxation = kwargs.pop('vRelax', 0.5) pressureRelaxation = kwargs.pop('pRelax', 0.8) pressureCoeff = None preCNorm = [] divVNorm = [] velBoundaryX = [] velBoundaryY = [] if velBoundary is not None: for marker, vel in velBoundary: if vel is not None: if not isinstance(vel[0], str): velBoundaryX.append([marker, vel[0]]) if not isinstance(vel[1], str): velBoundaryY.append([marker, vel[1]]) pressure = None if pre0 is None: pressure = np.zeros(mesh.cellCount()) else: pressure = np.array(pre0) velocity = None if vel0 is None: velocity = np.zeros((mesh.cellCount(), mesh.dimension())) else: velocity = np.array(vel0) mesh.createNeighbourInfos() CtB = mesh.cellToBoundaryInterpolation() controlVolumes = CtB * mesh.cellSizes() density = kwargs.pop('density', 1.0) force = kwargs.pop('f', [0.0, 0.0]) density = pg.solver.parseArgToArray(density, ndof=mesh.cellCount()) forceX = pg.solver.parseArgToArray(force[0], ndof=mesh.cellCount()) forceY = pg.solver.parseArgToArray(force[1], ndof=mesh.cellCount()) for i in range(maxIter): pressureGrad = cellDataToCellGrad(mesh, pressure, CtB) # __d('vx', pressureGrad[:,0]) velocity[:, 0] = solveFiniteVolume( mesh, a=viscosity, f=-pressureGrad[:, 0] / density + forceX, uB=velBoundaryX, uL=velocity[:, 0], relax=velocityRelaxation, ws=wsux) # for s in wsux.S: # print(s) # __d('rhs', wsux.rhs, 1) # __d('ux', velocity[:,0]) velocity[:, 1] = solveFiniteVolume( mesh, a=viscosity, f=-pressureGrad[:, 1] / density + forceY, uB=velBoundaryY, uL=velocity[:, 1], relax=velocityRelaxation, ws=wsuy) ap = np.array(wsux.ap * mesh.cellSizes()) apF = CtB * ap uxF = CtB * velocity[:, 0] uyF = CtB * velocity[:, 1] applyBoundaryValues(uxF, mesh, velBoundaryX) applyBoundaryValues(uyF, mesh, velBoundaryY) pxF = CtB * pressureGrad[:, 0] pyF = CtB * pressureGrad[:, 1] pF2 = cellDataToBoundaryGrad(mesh, pressure, pressureGrad) velXF = uxF + controlVolumes / apF * (pxF - pF2[:, 0]) velYF = uyF + controlVolumes / apF * (pyF - pF2[:, 1]) applyBoundaryValues(velXF, mesh, velBoundaryX) applyBoundaryValues(velYF, mesh, velBoundaryY) if pressureCoeff is None: pressureCoeff = 1. / apF * mesh.boundarySizes() * \ _boundaryToCellDistances(mesh) div = -mesh.divergence(np.vstack([velXF, velYF]).T) # boundsDirichlet = pg.solver.parseArgToBoundaries(preBoundary, mesh) # pB = CtB * pressure # for ix, [boundary, val] in enumerate(boundsDirichlet): # boundsDirichlet[ix][1] = -(-pg.solver.generateBoundaryValue(boundary, # val) + pB[boundary.id()]) pressureCorrection = solveFiniteVolume(mesh, a=pressureCoeff, f=div, uB=preBoundary, # uB=boundsDirichlet, ws=wsp) # print(i, pg.solver.generateBoundaryValue(mesh.boundary(58), val), # pB[58], boundsDirichlet[-1][1], # (CtB*pressureCorrection)[58]) pressure += pressureCorrection * pressureRelaxation pressureCorrectionGrad = cellDataToCellGrad(mesh, pressureCorrection, CtB) velocity[:, 0] -= pressureCorrectionGrad[:, 0] / ap * mesh.cellSizes() velocity[:, 1] -= pressureCorrectionGrad[:, 1] / ap * mesh.cellSizes() preCNorm.append(pg.norm(pressureCorrection)) divVNorm.append(pg.norm(div)) if workspace: workspace.div = div # __d('div', div) # if ( i == 1): # sd convergenceTest = 100 if i > 6: convergenceTest = (divVNorm[-1] - divVNorm[-2]) + \ (divVNorm[-2] - divVNorm[-3]) + \ (divVNorm[-3] - divVNorm[-4]) + \ (divVNorm[-4] - divVNorm[-5]) + \ (divVNorm[-5] - divVNorm[-6]) convergenceTest /= 5 if verbose: print("\r" + str(i) + " div V=" + str(divVNorm[-1]) + " ddiv V=" + str(convergenceTest)) if divVNorm[-1] > 1e6: print("\r" + str(i) + " div V=" + str(divVNorm[-1]) + " ddiv V=" + str(convergenceTest)) raise BaseException("Stokes solver seams to diverging") if i > 2: if i == maxIter or divVNorm[-1] < tol or \ abs(convergenceTest * divVNorm[-1]) < tol: break if verbose: print(str(i) + ": |pC|" + str(preCNorm[-1])) return velocity, pressure, preCNorm, divVNorm