Ejemplo n.º 1
0
def solver_scaled(T, dt, theta):
    """
    Solve u'=-u, u(0)=1 for (0,T] with step dt and theta method.
    """
    # Is the scaled problem already solved and dimensionless
    # curve available from file?
    # See if u_scaled.dat has the right parameters.
    already_computed = False
    datafile = 'u_scaled.dat'
    if os.path.isfile(datafile):      # does u_scaled.dat exist?
        infile = open(datafile, 'r')
        infoline = infile.readline()  # read the first line
        words = infoline.split()      # split line into words
        T_, dt_, theta_ = [float(w) for w in words]
        if T_ == T and dt_ == dt and theta_ == theta:
            # The file was computed with the desired data, load
            # the solution into arrays
            data = np.loadtxt(infile)
            u_scaled = data[1,:]
            t_scaled = data[0,:]
            print 'Read scaled solution from file'
            already_computed = True
        infile.close()
    if not already_computed:
        # T, dt or theta is different from u_scaled.dat
        u_scaled, t_scaled = \
           solver_unscaled(I=1, a=1, T=T, dt=dt, theta=theta)
        outfile = open(datafile, 'w')
        outfile.write('%f %f %.1f\n' % (T, dt, theta))
        # np.savetxt saves a two-dim array (table) to file
        np.savetxt(outfile, np.array([t_scaled, u_scaled]))
        outfile.close()
        print 'Computed scaled solution'
    return u_scaled, t_scaled
Ejemplo n.º 2
0
def solver_scaled(T, dt, theta):
    """
    Solve u'=-u, u(0)=1 for (0,T] with step dt and theta method.
    """
    # Is the scaled problem already solved and dimensionless
    # curve available from file?
    # See if u_scaled.dat has the right parameters.
    already_computed = False
    datafile = 'u_scaled.dat'
    if os.path.isfile(datafile):  # does u_scaled.dat exist?
        infile = open(datafile, 'r')
        infoline = infile.readline()  # read the first line
        words = infoline.split()  # split line into words
        T_, dt_, theta_ = [float(w) for w in words]
        if T_ == T and dt_ == dt and theta_ == theta:
            # The file was computed with the desired data, load
            # the solution into arrays
            data = np.loadtxt(infile)
            u_scaled = data[1, :]
            t_scaled = data[0, :]
            print 'Read scaled solution from file'
            already_computed = True
        infile.close()
    if not already_computed:
        # T, dt or theta is different from u_scaled.dat
        u_scaled, t_scaled = \
           solver_unscaled(I=1, a=1, T=T, dt=dt, theta=theta)
        outfile = open(datafile, 'w')
        outfile.write('%f %f %.1f\n' % (T, dt, theta))
        # np.savetxt saves a two-dim array (table) to file
        np.savetxt(outfile, np.array([t_scaled, u_scaled]))
        outfile.close()
        print 'Computed scaled solution'
    return u_scaled, t_scaled
Ejemplo n.º 3
0
def solver_scaled(T, dt, theta):
    """
    Solve u'=-u, u(0)=1 for (0,T] with step dt and theta method.
    """
    print 'Computing the numerical solution'
    return solver_unscaled(I=1, a=1, T=T, dt=dt, theta=theta)