Ejemplo n.º 1
0
    private static void simple_rkf45_run()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SIMPLE_RKF45_RUN runs the simple three body ODE system.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    13 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int          i;
        const int    neqn = 12;
        int          step;
        const int    step_num   = 630;
        const string t_filename = "simple_rkf45_t.txt";
        const string y_filename = "simple_rkf45_y.txt";

        RungeKuttaFehlberg.r8RKFData data = new();

        double[] ts = new double[step_num + 1];
        double[] y  = new double[neqn];
        double[] yp = new double[neqn];
        double[] ys = new double[neqn * (step_num + 1)];

        Console.WriteLine("");
        Console.WriteLine("SIMPLE_RKF45_RUN");
        Console.WriteLine("  Simulate the planar three-body problem as an ODE system");
        Console.WriteLine("  using RKF45 for the ODE integration.");

        const double abserr = 1.0E-10;
        double       relerr = 1.0E-10;

        int flag = 1;

        const double t_start = 0.0;
        const double t_stop  = 63.0;

        double t = 0.0;

        y[0]  = 1.0;
        y[1]  = -1.0;
        y[2]  = 0.0;
        y[3]  = 0.0;
        y[4]  = 1.0;
        y[5]  = 3.0;
        y[6]  = 0.0;
        y[7]  = 0.0;
        y[8]  = -2.0;
        y[9]  = -1.0;
        y[10] = 0.0;
        y[11] = 0.0;

        simple_f(t, y, yp);

        for (i = 0; i < neqn; i++)
        {
            ys[i + 0 * neqn] = y[i];
        }

        ts[0] = t;

        for (step = 1; step <= step_num; step++)
        {
            t = ((step_num - step + 1) * t_start
                 + (step - 1) * t_stop)
                / step_num;

            double t_out = ((step_num - step) * t_start
                            + step * t_stop)
                           / step_num;

            flag = RungeKuttaFehlberg.r8_rkf45(ref data, simple_f, neqn, ref y, ref yp, ref t, t_out, ref relerr,
                                               abserr, flag);

            if (Math.Abs(flag) != 2)
            {
                Console.WriteLine("");
                Console.WriteLine("SIMPLE_RKF45_RUN - Warning");
                Console.WriteLine("  Output value of FLAG = " + flag
                                  + " at output time T_OUT = " + t_out + "");
            }

            flag = flag switch
            {
                7 => 2,
                _ => flag
            };

            for (i = 0; i < neqn; i++)
            {
                ys[i + step * neqn] = y[i];
            }

            ts[step] = t_out;
        }

        typeMethods.r8mat_write(t_filename, 1, step_num + 1, ts);
        typeMethods.r8mat_write(y_filename, neqn, step_num + 1, ys);

        Console.WriteLine("");
        Console.WriteLine("SIMPLE_RKF45_RUN:");
        Console.WriteLine("  Time data written to \"" + t_filename + "\".");
        Console.WriteLine("  Solution data written to \"" + y_filename + "\".");
    }
Ejemplo n.º 2
0
    private static void test01()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 solves a scalar ODE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 June 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int i_step;

        RungeKuttaFehlberg.r4RKFData data = new();

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  Solve a scalar equation using R4_RKF:");
        Console.WriteLine("");
        Console.WriteLine("  Y' = 0.25 * Y * ( 1 - Y / 20 )");
        Console.WriteLine("");

        const int neqn = 1;

        float[] y  = new float[neqn];
        float[] yp = new float[neqn];

        float abserr = (float)Math.Sqrt(typeMethods.r4_epsilon());
        float relerr = (float)Math.Sqrt(typeMethods.r4_epsilon());

        int flag = 1;

        const float t_start = 0.0f;
        const float t_stop  = 20.0f;

        const int n_step = 5;

        float t = 0.0f;

        y[0] = 1.0f;
        yp   = r4_f1(t, y, yp);

        Console.WriteLine("");
        Console.WriteLine("FLAG             T          Y         Y'          Y_Exact         Error");
        Console.WriteLine("");

        Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                          + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + yp[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + r4_y1x(t).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + (y[0] - r4_y1x(t)).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");

        for (i_step = 1; i_step <= n_step; i_step++)
        {
            t = ((n_step - i_step + 1) * t_start
                 + (i_step - 1) * t_stop)
                / n_step;

            float t_out = ((n_step - i_step) * t_start
                           + i_step * t_stop)
                          / n_step;

            flag = RungeKuttaFehlberg.r4_rkf45(ref data, r4_f1, neqn, ref y, ref yp, ref t, t_out, ref relerr, abserr,
                                               flag);

            Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                              + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + yp[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + r4_y1x(t).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + (y[0] - r4_y1x(t)).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
        }
    }
Ejemplo n.º 3
0
    private static void test05()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 solves a vector ODE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 June 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int NEQN = 2;

        int i_step;

        double[] y  = new double[NEQN];
        double[] yp = new double[NEQN];
        RungeKuttaFehlberg.r8RKFData data = new();

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  Solve a vector equation using R8_RKF:");
        Console.WriteLine("");
        Console.WriteLine("  Y'(1) =  Y(2)");
        Console.WriteLine("  Y'(2) = -Y(1)");
        Console.WriteLine("");

        double abserr = Math.Sqrt(typeMethods.r8_epsilon());
        double relerr = Math.Sqrt(typeMethods.r8_epsilon());

        int flag = 1;

        const double t_start = 0.0;
        const double t_stop  = 2.0 * 3.14159265;

        const int n_step = 12;

        double t = 0.0;

        y[0] = 1.0;
        y[1] = 0.0;
        yp   = r8_f2(t, y, yp);

        Console.WriteLine("");
        Console.WriteLine("FLAG             T          Y(1)       Y(2)");
        Console.WriteLine("");

        Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                          + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + y[1].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");

        for (i_step = 1; i_step <= n_step; i_step++)
        {
            t = ((n_step - i_step + 1) * t_start
                 + (i_step - 1) * t_stop)
                / n_step;

            double t_out = ((n_step - i_step) * t_start
                            + i_step * t_stop)
                           / n_step;

            flag = RungeKuttaFehlberg.r8_rkf45(ref data, r8_f2, NEQN, ref y, ref yp, ref t, t_out, ref relerr, abserr,
                                               flag);

            Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                              + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + y[1].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
        }
    }
Ejemplo n.º 4
0
    private static void test06()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST06 solves a scalar ODE using single step mode.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 June 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int NEQN = 1;

        int i_step;

        double[] y  = new double[NEQN];
        double[] yp = new double[NEQN];
        RungeKuttaFehlberg.r8RKFData data = new();

        Console.WriteLine("");
        Console.WriteLine("TEST06");
        Console.WriteLine("  Solve a scalar equation using R8_RKF:");
        Console.WriteLine("");
        Console.WriteLine("  Y' = 0.25 * Y * ( 1 - Y / 20 )");
        Console.WriteLine("");
        Console.WriteLine("  This routine uses the SINGLE STEP mode.");
        Console.WriteLine("");

        double abserr = Math.Sqrt(typeMethods.r8_epsilon());
        double relerr = Math.Sqrt(typeMethods.r8_epsilon());

        int flag = -1;

        const double t_start = 0.0;
        const double t_stop  = 20.0;

        const int n_step = 5;

        double t = 0.0;

        y[0] = 1.0;
        yp   = r8_f1(t, y, yp);

        Console.WriteLine("");
        Console.WriteLine("FLAG             T          Y         Y'        Y_Exact         Error");
        Console.WriteLine("");

        Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                          + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + yp[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + r8_y1x(t).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + (y[0] - r8_y1x(t)).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");

        for (i_step = 1; i_step <= n_step; i_step++)
        {
            t = ((n_step - i_step + 1) * t_start
                 + (i_step - 1) * t_stop)
                / n_step;

            double t_out = ((n_step - i_step) * t_start
                            + i_step * t_stop)
                           / n_step;

            while (flag < 0)
            {
                flag = RungeKuttaFehlberg.r8_rkf45(ref data, r8_f1, NEQN, ref y, ref yp, ref t, t_out, ref relerr, abserr,
                                                   flag);

                Console.WriteLine(flag + "  "
                                  + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                                  + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                                  + yp[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                                  + r8_y1x(t).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                                  + (y[0] - r8_y1x(t)).ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
            }

            flag = -2;
        }
    }
Ejemplo n.º 5
0
    private static void test02()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 solves a vector ODE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 June 2006
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int i_step;

        RungeKuttaFehlberg.r4RKFData data = new();

        Console.WriteLine("");
        Console.WriteLine("TEST02");
        Console.WriteLine("  Solve a vector equation using R4_RKF:");
        Console.WriteLine("");
        Console.WriteLine("  Y'(1) =  Y(2)");
        Console.WriteLine("  Y'(2) = -Y(1)");
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("  This system is equivalent to the following");
        Console.WriteLine("  second order system:");
        Console.WriteLine("");
        Console.WriteLine("  Z\" = - Z.");

        const int neqn = 2;

        float[] y  = new float[neqn];
        float[] yp = new float[neqn];

        float abserr = (float)Math.Sqrt(typeMethods.r4_epsilon());
        float relerr = (float)Math.Sqrt(typeMethods.r4_epsilon());

        int flag = 1;

        const float t_start = 0.0f;
        const float t_stop  = 2.0f * 3.14159265f;

        const int n_step = 12;

        float t = 0.0f;

        y[0] = 1.0f;
        y[1] = 0.0f;

        Console.WriteLine("");
        Console.WriteLine("FLAG             T          Y(1)       Y(2)");
        Console.WriteLine("");

        Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                          + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                          + y[1].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");

        for (i_step = 1; i_step <= n_step; i_step++)
        {
            t = ((n_step - i_step + 1) * t_start
                 + (i_step - 1) * t_stop)
                / n_step;

            float t_out = ((n_step - i_step) * t_start
                           + i_step * t_stop)
                          / n_step;

            flag = RungeKuttaFehlberg.r4_rkf45(ref data, r4_f2, neqn, ref y, ref yp, ref t, t_out, ref relerr, abserr,
                                               flag);

            Console.WriteLine(flag.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  "
                              + t.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "  "
                              + y[1].ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
        }
    }