def Solve(m, xtfc): # TFC Constants: if xtfc: nC = -1 basis = 'ELMTanh' else: nC = [2, 2, 2] basis = 'CP' # Create the TFC Class: N = [n, n, n] myTfc = mtfc(N, nC, m, dim=3, basis=basis, x0=x0, xf=xf) # Create the TFC constrained expression x = myTfc.x H = myTfc.H u1 = lambda xi, *x: np.dot(H(*x), xi) - (1. - x[0]) * np.dot( H(np.zeros_like(x[0]), x[1], x[2]), xi) - x[0] * np.dot( H(np.ones_like(x[0]), x[1], x[2]), xi) u2 = lambda xi, *x: u1(xi, *x) - (1. - x[1]) * u1( xi, x[0], np.zeros_like(x[1]), x[2]) - x[1] * u1( xi, x[0], np.ones_like(x[1]), x[2]) du2dt = egrad(u2, 3) u = lambda xi, *x: u2(xi, *x) + np.sin(np.pi * x[0]) * np.sin(np.pi * x[ 1]) - u2(xi, x[0], x[1], np.zeros_like(x[2])) - x[2] * du2dt( xi, x[0], x[1], np.zeros_like(x[2])) # Create the residual uxx = egrad(egrad(u, 1), 1) uyy = egrad(egrad(u, 2), 2) utt = egrad(egrad(u, 3), 3) r = lambda xi: uxx(xi, *x) + uyy(xi, *x) - 64. * utt(xi, *x) xi = np.zeros(H(*x).shape[1]) if xtfc: LS = lambda xi: np.linalg.lstsq(jacfwd(r, 0) (xi), -r(xi), rcond=None)[0] else: LS = lambda xi: np.dot(np.linalg.pinv(jacfwd(r, 0)(xi)), -r(xi)) xi = LS(xi) # Calculate the test set error dark = np.meshgrid(np.linspace(x0[0], xf[0], nTest), np.linspace(x0[1], xf[1], nTest), np.linspace(x0[2], xf[2], nTest)) xTest = tuple([k.flatten() for k in dark]) err = np.abs(real(*xTest) - u(xi, *xTest)) return np.max(err), np.mean(err), myTfc.basisClass.numBasisFunc
xf = np.array([1.,1.]) # Allocate memory testErr = onp.zeros((len(nVec),len(mVec))) # Real analytical solution: real = lambda x,y: np.exp(-x)*(x+y**3) # Solve the problem for the various n and m values for j,n in enumerate(tqdm(nVec)): for k,m in enumerate(mVec): # Create the TFC Class: N = [n,]*2 nC = [2,2] tfc = mtfc(N,nC,m,dim=2,basis='CP',x0=x0,xf=xf) x = tfc.x if tfc.basisClass.numBasisFunc > n**2: testErr[j,k] = np.nan continue # Get the basis functions H = tfc.H # Create the constrained expression u1 = lambda xi,*x: np.dot(H(*x),xi)\ +(1.-x[0])*(x[1]**3-np.dot(H(np.zeros_like(x[0]),x[1]),xi))\ +x[0]*((1.+x[1]**3)*np.exp(-1.)-np.dot(H(np.ones_like(x[0]),x[1]),xi)) u = lambda xi,*x: u1(xi,*x)\ +(1.-x[1])*(x[0]*np.exp(-x[0])-u1(xi,x[0],np.zeros_like(x[1])))\
# Allocate memory testErr = onp.zeros((len(nVec), len(mVec))) # Real analytical solution: real = lambda x, y: np.exp(-x) * (x + y**3) # Solve the problem for the various n and m values for j, n in enumerate(tqdm(nVec)): for k, m in enumerate(mVec): # Create the TFC Class: N = [ n, ] * 2 nC = -1 tfc = mtfc(N, nC, m, dim=2, basis='ELMTanh', x0=x0, xf=xf) x = tfc.x if tfc.basisClass.numBasisFunc > n**2: testErr[j, k] = np.nan continue # Get the basis functions H = tfc.H # Create the constrained expression u1 = lambda xi,*x: np.dot(H(*x),xi)\ +(1.-x[0])*(x[1]**3-np.dot(H(np.zeros_like(x[0]),x[1]),xi))\ +x[0]*((1.+x[1]**3)*np.exp(-1.)-np.dot(H(np.ones_like(x[0]),x[1]),xi)) u = lambda xi,*x: u1(xi,*x)\ +(1.-x[1])*(x[0]*np.exp(-x[0])-u1(xi,x[0],np.zeros_like(x[1])))\
n = 10 if xTfc: m = 200 basis = 'ELMTanh' nC = -1 else: m = 10 basis = 'CP' nC = [2,2,1] maxIter = 50 # Create the TFC Class: N = [n,n,n] myTfc = mtfc(N,nC,m,dim=3,basis=basis,x0=x0,xf=xf) x = myTfc.x # Get the basis functions H = myTfc.H Hx = myTfc.Hx # Create the TFC constrained expression (here f stands as a placeholder for u and v) f1 = lambda xi,*x: np.dot(H(*x),xi)-np.dot(H(np.zeros_like(x[0]),x[1],x[2]),xi)-x[0]*np.dot(Hx(xend*np.ones_like(x[0]),x[1],x[2]),xi) f2 = lambda xi,*x: f1(xi,*x)-(Hb-2.*x[1])/(2.*Hb)*f1(xi,x[0],-Hb/2.*np.ones_like(x[1]),x[2])-(Hb+2.*x[1])/(2.*Hb)*f1(xi,x[0],Hb/2.*np.ones_like(x[1]),x[2]) f = lambda xi,*x: f2(xi,*x)-f2(xi,x[0],x[1],np.zeros_like(x[2])) fx = egrad(f,1) f2x = egrad(fx,1) fy = egrad(f,2) f2y = egrad(fy,2)
# Constants: n = [40, 40] nC = [2, [1, 2]] m = 40 r0 = 2. rf = 4. th0 = 0. thf = 2. * np.pi realSoln = lambda r, th: 4. * (-1024. + r**10) * np.sin(5. * th) / (1023. * r** 5) # Create TFC class: myTfc = mtfc(n, nC, m, x0=[r0, th0], xf=[rf, thf]) H = myTfc.H x = myTfc.x # Create constrained expression: g = lambda xi, *x: np.dot(H(*x), xi) u1 = lambda xi,*x: g(xi,*x)+\ (x[0]-rf)/(r0-rf)*(0.-g(xi,r0*np.ones_like(x[0]),x[1]))+\ (x[0]-r0)/(rf-r0)*(4.*np.sin(5.*x[1])-g(xi,rf*np.ones_like(x[0]),x[1])) u = lambda xi,*x: u1(xi,*x)+\ -x[1]/(2.*np.pi)*(u1(xi,x[0],thf*np.ones_like(x[1]))-u1(xi,x[0],th0*np.ones_like(x[1])))+\ (-x[1]**2+2.*np.pi*x[1])/(4.*np.pi)*(egrad(u1,2)(xi,x[0],thf*np.ones_like(x[1]))-egrad(u1,2)(xi,x[0],th0*np.ones_like(x[1]))) # Create the loss function: ur = egrad(u, 1) u2r = egrad(ur, 1)