Exemplo n.º 1
0
def CalculateSolution(Pe):

    # Create the TFC Class:
    N = 200
    m = 190
    nC = 2
    tfc = utfc(N, nC, m, basis='LeP', x0=0., xf=1.)
    x = tfc.x

    # Get the Chebyshev polynomials
    H = tfc.H
    H0 = H(np.array([0.]))
    Hf = H(np.array([1.]))

    # Create the constraint expression and its derivatives
    y = lambda x, xi: np.dot(H(x), xi) + (1. - x) * (1. - np.dot(H0, xi)
                                                     ) - x * np.dot(Hf, xi)
    yd = egrad(y)
    ydd = egrad(yd)

    L = lambda xi: ydd(x, xi) - Pe * yd(x, xi)

    # Calculate the solution
    zXi = np.zeros(H(x).shape[1])
    xi, it = NLLS(zXi, L)

    # Create the test set:
    N = 1000
    xTest = np.linspace(0., 1., N)
    err = np.abs(y(xTest, xi) - soln(xTest, Pe))
    return np.max(err), np.mean(err)
Exemplo n.º 2
0
def BVP_tfc(N, m, basis, iterMax, tol):
    ## Unpack Paramters: *********************************************************
    x0 = 0.
    xf = np.pi

    ## Initial Conditions: *******************************************************
    y0 = 0.
    yf = 0.
    nC = 2  # number of constraints

    ## Determine call tfc class needs to be 1 for ELMs
    if basis == 'CP' or basis == 'LeP':
        c = 2. / (xf - x0)
    elif basis == 'FS':
        c = 2. * np.pi / (xf - x0)
    else:
        c = 1. / (xf - x0)

    ## Compute true solution
    ytrue = lambda x: np.exp(-x) * np.sin(x)

    err = onp.ones_like(m) * np.nan
    res = onp.ones_like(m) * np.nan

    ## GET CHEBYSHEV VALUES: *********************************************

    tfc = utfc(N, nC, int(m), basis=basis, x0=x0, xf=xf)
    x = tfc.x

    H = tfc.H
    H0 = H(tfc.x[0])
    Hf = H(tfc.x[-1])

    ## DEFINE THE ASSUMED SOLUTION: *************************************
    phi1 = lambda x: (np.pi - x) / np.pi
    phi2 = lambda x: x / np.pi

    f = lambda x: np.exp(-2. * x) * np.sin(x) * (np.cos(x) - np.sin(
        x)) - 2. * np.exp(-x) * np.cos(x)

    y = lambda x, xi: np.dot(H(x), xi) + phi1(x) * (y0 - np.dot(
        H0, xi)) + phi2(x) * (yf - np.dot(Hf, xi))
    yp = egrad(y)
    ypp = egrad(yp)

    ## DEFINE LOSS AND JACOB ********************************************
    L = lambda xi: ypp(x, xi) + y(x, xi) * yp(x, xi) - f(x)

    ## SOLVE THE SYSTEM *************************************************
    xi = onp.zeros(H(x).shape[1])

    xi, _, time = NLLS(xi, L, timer=True, maxIter=iterMax)

    ## COMPUTE ERROR AND RESIDUAL ***************************************
    err = onp.linalg.norm(y(x, xi) - ytrue(x))
    res = onp.linalg.norm(L(xi))

    return err, res, time
Exemplo n.º 3
0
def IVP2BVP(N, m, gamma, basis, iterMax, tol):
    ## Unpack Paramters: *********************************************************
    x0 = -1.
    xf =  1.

    ## Initial Conditions: *******************************************************
    y0  = -2.
    y0p = -2.
    yf  =  2.

    nC  =  2 # number of constraints

    ## Determine call tfc class needs to be 1 for ELMs
    if basis == 'CP' or basis == 'LeP':
        c = 2./ (xf - x0)
    elif basis == 'FS':
        c = 2. * np.pi / (xf - x0)
    else:
        c = 1./ (xf - x0)

    ## GET CHEBYSHEV VALUES: *********************************************

    tfc = utfc(N,nC,m,basis = basis, x0=-1., xf=1.)
    x = tfc.x

    H = tfc.H
    dH = tfc.dH
    H0 = H(tfc.z[0])
    Hf = H(tfc.z[-1])
    H0p = dH(tfc.z[0])

    ## DEFINE THE ASSUMED SOLUTION: *************************************
    phi1 = lambda a: 1./(1. + 4.*gamma - gamma**2) * ( (1. + gamma) - 2.*gamma*a )
    phi2 = lambda a: 1./(1. + 4.*gamma - gamma**2) * ( (1. - gamma)**2 + (1. - gamma**2)*a)
    phi3 = lambda a: 1./(1. + 4.*gamma - gamma**2) * ( -gamma*(gamma-3.) + 2.*gamma*a )

    y = lambda x, xi: np.dot(H(x),xi) + phi1(x)*(y0  - np.dot(H0,xi)) \
                                      + phi2(x)*(y0p - np.dot(H0p,xi)) \
                                      + phi3(x)*(yf  - np.dot(Hf,xi))
    yp = egrad(y,0)
    ypp = egrad(yp,0)


    ## DEFINE LOSS AND JACOB ********************************************
    L = lambda xi: ypp(x,xi) + (np.cos(3.*x**2) -3.*x + 1.)*yp(x,xi) \
                             + (6.*np.sin(4.*x**2) - np.exp(np.cos(3.*x)))*y(x,xi) \
                             - 2. * (1.-np.sin(3.*x))*(3.*x-np.pi)/(4.-x)



    ## SOLVE THE SYSTEM *************************************************
    xi   = onp.zeros(H(x).shape[1])


    xi,_,_ = NLLS(xi,L,timer=True)

    return y(x,xi), L(xi), x
Exemplo n.º 4
0
if basis == 'CP' or 'LeP':
    nC = 2
elif basis == 'FS':
    nC = 1
else:
    nC = 0
# number of constraints

# length of time for one TFC step
xstep = (xspan[1] - xspan[0]) / Nstep
# !!! since this differential equation is not a explicit function of position 'x', I can get
#     away with contructing the tfc class such that x = [0, xstep] an imposing a constant step so
#     that the mapping parameter c = (zf-z0)/(xf-x0) is also constant

## construct univariate tfc class: *****************************************************************
tfc = utfc(N + 1, nC, int(m + 1), basis=basis, x0=0, xf=xstep)
x = tfc.x
# !!! notice I am using N+1 for the number of points. this is because I will be using the last point
#     of a segment 'n' for the initial conditons of the 'n+1' segment

H = tfc.H
dH = tfc.dH
H0 = H(x[0:1])
H0p = dH(x[0:1])

## define tfc constrained expression and derivatives: **********************************************
# switching function
phi1 = lambda x: np.ones_like(x)
phi2 = lambda x: x

# tfc constrained expression
Exemplo n.º 5
0
import jax.numpy as np
from tfc import utfc
from tfc.utils import egrad, MakePlot, NLLS

# Constants:
n = 100
nC = 2
m = 60
th0 = 0.
thf = 1.3
r0 = 0.
rf = 5.

# Create TFC class:
myTfc = utfc(n, nC, m, x0=th0, xf=thf)
th = myTfc.x
H = myTfc.H

# Create constrained expression:
g = lambda th, xi: np.dot(H(th), xi)
r = lambda th,xi: g(th,xi)+\
                  (th-thf)/(th0-thf)*(r0-g(th0*np.ones_like(th),xi))+\
                  (th-th0)/(thf-th0)*(rf-g(thf*np.ones_like(th),xi))

# Create loss function:
dr = egrad(r)
d2r = egrad(dr)
L = lambda xi: -r(th,xi)**2*(dr(th,xi)*np.tan(th)+2.*d2r(th,xi))+\
               -np.tan(th)*dr(th,xi)**3+3.*r(th,xi)*dr(th,xi)**2+r(th,xi)**3

# Solve the problem:
Exemplo n.º 6
0
## initial guess values: ***************************************************************************
r_init = [0.79, 0.]
v_init = [0., 0.42]
T_init = 21.4
# !!! These initial guesses comes from the Richardson's third-order analytical method for Halo-type
#     periodic orbits. See: http://articles.adsabs.harvard.edu/full/1980CeMec..22..241R

## user defined parameters: ************************************************************************
N = 140  # number of discretization points
m = 130  # number of basis function terms
basis = 'CP'  # basis function type
# !!! Here we can see that a large number of basis terms is used. This is needed for this specific
#     problem to obtain the desirable accuracy

## construct univariate tfc class: *****************************************************************
tfc = utfc(N, 4, m, basis=basis, x0=-1., xf=1.)
# !!! Note that here I didn't explicitly define nC = 4, but I had inluded the number of constraints
#     in my call to the utfc class

H = tfc.H
dH = tfc.dH

H0 = H(tfc.z[0:1])
Hf = H(tfc.z[-1:])

Hp0 = dH(tfc.z[0:1])
Hpf = dH(tfc.z[-1:])

## defined the constrained expressions: ************************************************************
z = tfc.z
z0 = z[0]
Exemplo n.º 7
0
import numpy as np
#####################################################################
nLines = 20
N = 1000
basis = 'CP'
x0 = -1.
xf = 1.

# Point constraints
y1 = 4. * np.random.rand() - 2.
y2 = 4. * np.random.rand() - 2.
y3 = 4. * np.random.rand() - 2.

## DEFINE UPPER/LOWER BOUNDS: ******************************************************************
mBnd = 7
bnd = utfc(N, 0, mBnd, basis=basis, x0=x0, xf=xf)
x = bnd.x

fu = lambda xi: np.dot(bnd.H(x), xi) + 5.
fl = lambda xi: np.dot(bnd.H(x), xi) - 5.

## DEFINE CONSTRAINED EXPRESSION: ******************************************************************
nC = 3
m = 15

tfc = utfc(N, nC, m, basis=basis, x0=x0, xf=xf)
x = tfc.x

x1 = tfc.x[200]
x2 = tfc.x[500]
x3 = tfc.x[-200]
Exemplo n.º 8
0
def ytrue(a):
    val = onp.zeros_like(a)
    for i in range(0, len(a)):
        if a[i] <= np.pi / 2.:
            val[i] = - 1./5. * np.exp(np.pi - 2.*a[i]) \
                     + 1./2. * np.exp(np.pi/2. - a[i]) \
                     + (9.*np.cos(a[i]) + 7.*np.sin(a[i])) / 10.
        else:
            val[i] = np.exp(np.pi / 2. - a[i])
    return val


## GET CHEBYSHEV VALUES: *********************************************

# First segment
tfc1 = utfc(N, nC, m, basis=basis, x0=x0, xf=x1)
xs1 = tfc1.x

Hs1 = tfc1.H
dHs1 = tfc1.dH

H0s1 = Hs1(tfc1.x[0])
Hfs1 = Hs1(tfc1.x[-1])

Hfps1 = dHs1(tfc1.x[-1])

# Second segment
tfc2 = utfc(N, nC, m, basis=basis, x0=x1, xf=xf)
xs2 = tfc2.x

Hs2 = tfc2.H
Exemplo n.º 9
0
import numpy as onp
import jax.numpy as np
from jax import jit

from tfc import utfc
from tfc.utils import MakePlot, step

# Constants:
N = 101
m = 8
nC = -1

nMC = 100

# Create the TFC class:
myTfc = utfc(N, nC, m, x0=-2., xf=2., basis='LeP')
x = np.linspace(-2., 2., N)
ind = np.argmin(np.abs(x))
H = myTfc.H

m = H(x).shape[1]

# Create the constrained expression:
K = np.array([1., 2., 3.])
g = lambda x, xi: np.dot(H(x), xi)
uslow = lambda x, n, xi: g(x, xi) + K[np.int64(n % 3)] - g(np.array([0.]), xi)
u = jit(uslow)

# Run the monte carlo test
p = MakePlot(r'$x$', r'$y(x,n,g(x))$')
Exemplo n.º 10
0
    return q


# Generate the constraints in u,v,w:
uvwi = q2u(qi)
ui = uvwi[0]
vi = uvwi[1]
wi = uvwi[2]

uvwf = q2u(qf)
uf = uvwf[0]
vf = uvwf[1]
wf = uvwf[2]

# Create the TFC class:
myTfc = utfc(N, -1, m, x0=0., xf=2., basis='FS')
H = myTfc.H
t = myTfc.x

# Create the constrained expressions:
uhat = lambda t,g: g(t)\
                   +(2.-t)/2.*(ui-g(np.zeros_like(t)))\
                   +t/2.*(uf-g(2.*np.ones_like(t)))
vhat = lambda t,g: g(t)\
                   +(2.-t)/2.*(vi-g(np.zeros_like(t)))\
                   +t/2.*(vf-g(2.*np.ones_like(t)))
u = lambda t,g: uhat(t,g)\
                +(np.pi-uhat(t,g))*step(uhat(t,g)-np.pi)\
                -uhat(t,g)*step(-uhat(t,g))
v = lambda t,g: vhat(t,g)\
                +(np.pi-vhat(t,g))*step(vhat(t,g)-np.pi)\
Exemplo n.º 11
0
def laneEmden_tfc(N, m, type, xspan, basis, iterMax, tol):
    ## Unpack Paramters: *********************************************************
    x0 = xspan[0]
    xf = xspan[1]

    ## Initial Conditions: *******************************************************
    y0 = 1.
    y0p = 0.
    nC = 2  # number of constraints

    ## Determine call tfc class needs to be 1 for ELMs
    if basis == 'CP' or basis == 'LeP':
        c = 2. / (xf - x0)
    elif basis == 'FS':
        c = 2. * np.pi / (xf - x0)
    else:
        c = 1. / (xf - x0)

    ## Compute true solution
    if type == 0:
        maxIter = 1

        def ytrue(x):
            val = onp.zeros_like(x)
            val[0] = 1.
            val[1:] = 1. - 1. / 6. * x[1:]**2
            return val
    elif type == 1:
        maxIter = 1

        def ytrue(x):
            val = onp.zeros_like(x)
            val[0] = 1.
            val[1:] = np.sin(x[1:]) / x[1:]
            return val

    elif type == 5:
        maxIter = iterMax

        def ytrue(x):
            val = onp.zeros_like(x)
            val[0] = 1.
            val[1:] = (1. + x[1:]**2 / 3)**(-1 / 2)
            return val
    else:

        def ytrue(x):
            return np.nan * np.ones_like(x)

    err = np.ones_like(m) * np.nan
    res = np.ones_like(m) * np.nan

    ## GET CHEBYSHEV VALUES: *********************************************

    tfc = utfc(N, nC, int(m), basis=basis, x0=x0, xf=xf)
    x = tfc.x

    H = tfc.H
    dH = tfc.dH
    H0 = H(x[0])
    H0p = dH(x[0])

    ## DEFINE THE ASSUMED SOLUTION: *************************************
    phi1 = lambda x: np.ones_like(x)
    phi2 = lambda x: x

    y = lambda x,xi: np.dot(H(x),xi) \
                    + phi1(x)*(y0  - np.dot(H0,xi)) \
                    + phi2(x)*(y0p - np.dot(H0p,xi))
    yp = egrad(y)
    ypp = egrad(yp)

    ## DEFINE LOSS AND JACOB ********************************************
    L = jit(lambda xi: x * ypp(x, xi) + 2. * yp(x, xi) + x * y(x, xi)**type)

    ## SOLVE THE SYSTEM *************************************************

    # Solve the problem
    xi = np.zeros(H(x).shape[1])

    xi, _, time = NLLS(xi, L, timer=True, maxIter=maxIter)

    ## COMPUTE ERROR AND RESIDUAL ***************************************
    err = np.linalg.norm(y(x, xi) - ytrue(x))
    res = np.linalg.norm(L(xi))

    return err, res, time
Exemplo n.º 12
0
def CalculateSolutionSplit(Pe):

    if Pe > 1e3:
        xpBoundL = 0. + 1.e-3
        xpBoundU = 1. - 1e-3
    else:
        xpBoundL = 0. + 1.e-1
        xpBoundU = 1. - 1e-1

    # Create the ToC Class:
    N = 200
    m = 190
    nC = 3
    tfc = utfc(N, nC, m, basis='LeP', x0=-1., xf=1.)

    # Get the Chebyshev polynomials
    H = tfc.H
    dH = tfc.dH
    H0 = H(np.array([-1.]))
    Hf = H(np.array([1.]))
    Hd0 = dH(np.array([-1.]))
    Hdf = dH(np.array([1.]))

    # Create the constraint expression and its derivatives
    z = tfc.z

    xp = lambda xi: xi['xpHat'] + (xpBoundU - xi['xpHat']) * step(xi[
        'xpHat'] - xpBoundU) + (xpBoundL - xi['xpHat']) * step(xpBoundL - xi[
            'xpHat'])

    c1 = lambda xi: 2. / (xp(xi))
    c2 = lambda xi: 2. / (1. - xp(xi))

    x1 = lambda z, xi: (z + 1.) / c1(xi)
    x2 = lambda z, xi: (z + 1.) / c2(xi) + xp(xi)

    y1 = lambda z,xi: np.dot(H(z),xi['xi1'])+(1.-2.*z+z**2)/4.*(1.-np.dot(H0,xi['xi1']))\
                                   +(3.+2.*z-z**2)/4.*(xi['y']-np.dot(Hf,xi['xi1']))\
                                   +(-1.+z**2)/2.*(xi['yd']/c1(xi)-np.dot(Hdf,xi['xi1']))
    ydz1 = egrad(y1, 0)
    yddz1 = egrad(ydz1, 0)
    yd1 = lambda z, xi: ydz1(z, xi) * c1(xi)
    ydd1 = lambda z, xi: yddz1(z, xi) * c1(xi)**2

    y2 = lambda z,xi: np.dot(H(z),xi['xi2'])+(3.-2.*z-z**2)/4.*(xi['y']-np.dot(H0,xi['xi2']))\
                                     +(1.-z**2)/2.*(xi['yd']/c2(xi)-np.dot(Hd0,xi['xi2']))\
                                     +(1.+2.*z+z**2)/4.*(0.-np.dot(Hf,xi['xi2']))
    ydz2 = egrad(y2, 0)
    yddz2 = egrad(ydz2, 0)
    yd2 = lambda z, xi: ydz2(z, xi) * c2(xi)
    ydd2 = lambda z, xi: yddz2(z, xi) * c2(xi)**2

    # Solve the problem
    xi = TFCDict({
        'xi1': onp.zeros(H(z).shape[1]),
        'xi2': onp.zeros(H(z).shape[1]),
        'xpHat': onp.array([0.99]),
        'y': onp.array([0.]),
        'yd': onp.array([0.])
    })

    L1 = lambda xi: ydd1(z, xi) - Pe * yd1(z, xi)
    L2 = lambda xi: ydd2(z, xi) - Pe * yd2(z, xi)
    L = lambda xi: np.hstack([L1(xi), L2(xi)])

    xi, it = NLLS(xi, L)

    # Create the test set:
    N = 1000
    z = np.linspace(-1., 1., N)

    # Calculate the error and return the results
    X = np.hstack([x1(z, xi), x2(z, xi)])
    Y = np.hstack([y1(z, xi), y2(z, xi)])
    err = np.abs(Y - soln(X, Pe))
    return np.max(err), np.mean(err)
Exemplo n.º 13
0
mw_gas = 4.00e-3
mw_atm = 4.34e-2

# Calcualted constants:
As = 4.*np.pi*Rs**2
Vs = 4./3*np.pi*Rs**3

# TFC constants:
N = 100
m = 80

tol = 1.e-13
maxIter = 15

# Create the TFC Classes:
tfc = utfc(N,2,m,basis='CP',x0=-1.,xf=1.)
tfc1 = utfc(N,1,m,basis='CP',x0=-1.,xf=1.)
x = tfc.x

# Get the Chebyshev polynomials
H = tfc.H
H0 = H(np.array([-1.]))
Hf = H(np.array([1.]))

H1 = tfc1.H
H10 = H1(np.array([-1.]))

# Boundary conditions
s0 = lambda xi: Rs*xi['beta']
z0 = lambda xi: Rs*(1.-np.cos(xi['beta']))
r0 = lambda xi: Rs*np.sin(xi['beta'])
Exemplo n.º 14
0
nC = 2  # number of constraints

## problem initial conditions: *********************************************************************
tspan = [0., 1.] # time range of problem

initial = np.array([-1.0, -1.0])
final  = np.array([ 1.0,  1.0])

Nlines = 20

## keep-out parameters: ****************************************************************************
xbound = np.array([-0.5, 0.5])
ybound = np.array([-0.5, 0.5])

## construct univariate tfc class: *****************************************************************
tfc = utfc(N, nC, m, basis = basis, x0=tspan[0], xf=tspan[-1])

t = tfc.x
H = tfc.H
H0 = H(t[0])
Hf = H(t[-1])

## define tfc constrained expression: **************************************************************
# switching function
phi1 = lambda t: (t[-1] - t) / (t[-1] - t[0])
phi2 = lambda t: (t - t[0])  / (t[-1] - t[0])

# tfc constrained expression (without inequality constraints)
xhat = lambda xi: np.dot(H(t),xi)  + phi1(t)*(initial[0] - np.dot(H0,xi)) \
                                   + phi2(t)*(final[0]   - np.dot(Hf,xi))
yhat = lambda xi: np.dot(H(t),xi)  + phi1(t)*(initial[1] - np.dot(H0,xi)) \
Exemplo n.º 15
0
alfa = 1.
beta = 1.

# Number of points to use
N = 35

# Number of basis functions to use
m = 30

# Number of constraints
nCx = 2
nCu = 0

## GET CHEBYSHEV VALUES: *********************************************
xtfc = utfc(N, nCx, m, basis='CP', x0=-1, xf=1.)
utfc = utfc(N, nCu, m, basis='CP', x0=-1, xf=1.)

Hx = xtfc.H
Hx0 = Hx(xtfc.z[0])
Hxf = Hx(xtfc.z[-1])

Hu = utfc.H

## DEFINE THE ASSUMED SOLUTION: *************************************
z = xtfc.z
z0 = z[0]
zf = z[-1]

phi1 = lambda a: (zf - a) / (zf - z0)
phi2 = lambda a: (a - z0) / (zf - z0)
Exemplo n.º 16
0
# Constants used in the differential equation:
Pe = 10**6
tol = 1e-13

xI = 0.
xf = 1.
yi = 1.
yf = 0.
xpBound = 1. - 1. * 10**-6

# Create the ToC Class:
N = 200
c = 1.
m = 190
nC = 3
tfc = utfc(N, nC, m, basis='CP', x0=-1, xf=1.)

# Get the Chebyshev polynomials
H = tfc.H
dH = tfc.dH
H0 = H(tfc.z[0])
Hf = H(tfc.z[-1])
Hd0 = dH(tfc.z[0])
Hdf = dH(tfc.z[-1])

# Create the constraint expression and its derivatives
z = tfc.z
c1 = lambda xp: 2. / (xp - xI)
c2 = lambda xp: 2. / (xf - xp)

x1 = lambda z, xp: (z + 1.) / c1(xp) + xI
Exemplo n.º 17
0
def runLaneEmden(N, m, basis, k, xf):
    ## user defined parameters: ************************************************************************
    # N      - number of discretization points
    # m      - number of basis function terms
    # basis  - basis function type
    # k      - specific problem type, k >=0 (analytical solution known for k = 0, 1, and 5)

    ## problem initial conditions: *****************************************************************
    xspan = [0., xf]  # problem domain range [x0, xf], where x₀ > 0
    y0 = 1.  # y(x0)  = 1
    y0p = 0.  # y'(x0) = 0
    nC = 2  # number of constraints

    ## construct univariate tfc class: *************************************************************
    tfc = utfc(N, nC, int(m), basis=basis, x0=xspan[0], xf=xspan[1])
    x = tfc.x

    H = tfc.H
    dH = tfc.dH
    H0 = H(x[0:1])
    H0p = dH(x[0:1])

    ## define tfc constrained expression and derivatives: ******************************************
    # switching function
    phi1 = lambda x: np.ones_like(x)
    phi2 = lambda x: x

    # tfc constrained expression
    y = lambda x, xi: np.dot(H(x), xi) + phi1(x) * (y0 - np.dot(
        H0, xi)) + phi2(x) * (y0p - np.dot(H0p, xi))
    yp = egrad(y)
    ypp = egrad(yp)

    ## define the loss function: *******************************************************************
    L = lambda xi: x * ypp(x, xi) + 2. * yp(x, xi) + x * y(x, xi)**k

    ## solve the problem via nonlinear least-squares ***********************************************
    xi = np.zeros(H(x).shape[1])

    # if k==0 or k==1, the problem is linear
    if k == 0 or k == 1:
        xi, time = LS(xi, L, timer=True)
        iter = 1

    else:
        xi, iter, time = NLLS(xi, L, timer=True)

    ## compute the error (if k = 0, 1, or 5): ******************************************************
    if k == 0:
        ytrue = 1. - 1. / 6. * x**2
    elif k == 1:
        ytrue = onp.ones_like(x)
        ytrue[1:] = np.sin(x[1:]) / x[1:]
    elif k == 5:
        ytrue = (1. + x**2 / 3)**(-1 / 2)
    else:
        ytrue = np.empty_like(x)

    err = np.abs(y(x, xi) - ytrue)

    ## compute the residual of the loss vector: ****************************************************
    res = np.abs(L(xi))

    return x, y(x, xi), err, res
Exemplo n.º 18
0
aEll = 1.
bEll = 0.75
cEll = 2.

n = 100
nC = 2
m = 3
x0 = 0.
xf = 3.

nCEs = 8

colors = qual.Plotly

# Constrained expressions:
myTfc = utfc(n,nC,m,x0=x0,xf=xf)
t = myTfc.x
H = myTfc.H

g = lambda xi,t: np.dot(H(t),xi['xi'])
xslow = lambda xi,t: g(xi,t)\
                 +(3.-t)/3.*(aEll*np.sin(xi['phi'])*np.cos(xi['th'])-g(xi,np.array([0.])))\
                 +t/3.*(3.+aHyp*np.sinh(xi['v'])*np.cos(xi['psi'])-g(xi,np.array([3.])))
yslow = lambda xi,t: g(xi,t)\
                 +(3.-t)/3.*(bEll*np.sin(xi['phi'])*np.sin(xi['th'])-g(xi,np.array([0.])))\
                 +t/3.*(bHyp*np.sinh(xi['v'])*np.sin(xi['psi'])-g(xi,np.array([3.])))
zslow = lambda xi,t: g(xi,t)\
                 +(3.-t)/3.*(cEll*np.cos(xi['phi'])-g(xi,np.array([0.])))\
                 +t/3.*((-1.)**step(xi['n'])*cHyp*np.cosh(xi['v'])-g(xi,np.array([3.])))

x = jit(xslow); y = jit(yslow); z = jit(zslow)
Exemplo n.º 19
0
from tfc import utfc
from tfc.utils import MakePlot, step

onp.random.seed(0)

# Constants
numFuncs = 20
m = 5
n = 250

# Boundaries
ub = lambda x: x / 30. * np.sin(2. * x) + 0.2
lb = lambda x: x**2 / np.exp(x) * np.cos(x) - 0.3

# Get CP
myTfc = utfc(n, -1, m, basis='CP', x0=0., xf=10.)
H = myTfc.H
x = np.linspace(0., 8., n)
m = H(x).shape[1]

# Create inequality only plot
g = lambda x, xi: np.dot(H(x), xi)
u = jit(lambda x, xi: g(x, xi) + (ub(x) - g(x, xi)) * step(g(x, xi) - ub(x)) +
        (lb(x) - g(x, xi)) * step(lb(x) - g(x, xi)))


def antiu(x, xi):
    dark = onp.array(g(x, xi))
    ind = onp.where(np.logical_and(dark > lb(x), dark < ub(x)))[0]
    dark[ind] = np.nan
    return dark
Exemplo n.º 20
0
maxIter = 50

## CONSTANTS: *********************************************************
# Number of points to use
N = 100

# Number of basis functions to use
ms = 30
mc = 1

# Number of constraints
nCx = 4
nCy = 0

## GET CHEBYSHEV VALUES **********************************************
stfc = utfc(N, nCx, ms, basis='CP', x0=-1, xf=1.)
ctfc = utfc(N, nCy, mc, basis='CP', x0=-1, xf=1.)

Hs = stfc.H
pHs = stfc.dH

Hs0 = Hs(stfc.z[0:1])
Hsf = Hs(stfc.z[-1:])

pHs0 = pHs(stfc.z[0:1])
pHsf = pHs(stfc.z[-1:])

Hc = ctfc.H

## DEFINE THE ASSUMED SOLUTION **************************************
z = stfc.z
Exemplo n.º 21
0
import numpy as onp
####################################################################################################

## user defined parameters: ************************************************************************
N = 100  # number of discretization points
m = 60  # number of basis function terms
basis = 'CP'  # basis function type

## problem boundary conditions: ********************************************************************
xspan = [0., np.pi]
y0 = 0.  # y(0)  = 0
yf = 0.  # y(pi) = 0
nC = 2  # number of constraints

## construct univariate tfc class: *****************************************************************
tfc = utfc(N, nC, int(m), basis=basis, x0=xspan[0], xf=xspan[1])
x = tfc.x

H = tfc.H
H0 = H(tfc.x[0:1])
Hf = H(tfc.x[-1:])

## define tfc constrained expression and derivatives: **********************************************
# switching functions
phi1 = lambda x: (np.pi - x) / np.pi
phi2 = lambda x: x / np.pi

# forcing term
f = lambda x: np.exp(-2. * x) * np.sin(x) * (np.cos(x) - np.sin(x)
                                             ) - 2. * np.exp(-x) * np.cos(x)