Blame ode-initval/gear2.c

Packit 67cb25
/* ode-initval/gear2.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
/* Gear 2 */
Packit 67cb25
Packit 67cb25
/* Author:  G. Jungman
Packit 67cb25
 */
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <string.h>
Packit 67cb25
#include <gsl/gsl_math.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include "odeiv_util.h"
Packit 67cb25
#include <gsl/gsl_odeiv.h>
Packit 67cb25
Packit 67cb25
Packit 67cb25
/* gear2 state object */
Packit 67cb25
typedef struct
Packit 67cb25
{
Packit 67cb25
  int primed;                   /* flag indicating that yim1 is ready */
Packit 67cb25
  double t_primed;              /* system was primed for this value of t */
Packit 67cb25
  double last_h;                /* last step size */
Packit 67cb25
  gsl_odeiv_step *primer;       /* stepper to use for priming */
Packit 67cb25
  double *yim1;                 /* y_{i-1}    */
Packit 67cb25
  double *k;                    /* work space */
Packit 67cb25
  double *y0;                   /* work space */
Packit 67cb25
  double *y0_orig;
Packit 67cb25
  double *y_onestep;
Packit 67cb25
  int stutter;
Packit 67cb25
}
Packit 67cb25
gear2_state_t;
Packit 67cb25
Packit 67cb25
static void *
Packit 67cb25
gear2_alloc (size_t dim)
Packit 67cb25
{
Packit 67cb25
  gear2_state_t *state = (gear2_state_t *) malloc (sizeof (gear2_state_t));
Packit 67cb25
Packit 67cb25
  if (state == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for gear2_state", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->yim1 = (double *) malloc (dim * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (state->yim1 == 0)
Packit 67cb25
    {
Packit 67cb25
      free (state);
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for yim1", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->k = (double *) malloc (dim * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (state->k == 0)
Packit 67cb25
    {
Packit 67cb25
      free (state->yim1);
Packit 67cb25
      free (state);
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for k", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->y0 = (double *) malloc (dim * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (state->y0 == 0)
Packit 67cb25
    {
Packit 67cb25
      free (state->k);
Packit 67cb25
      free (state->yim1);
Packit 67cb25
      free (state);
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->y0_orig = (double *) malloc (dim * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (state->y0_orig == 0)
Packit 67cb25
    {
Packit 67cb25
      free (state->y0);
Packit 67cb25
      free (state->k);
Packit 67cb25
      free (state->yim1);
Packit 67cb25
      free (state);
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->y_onestep = (double *) malloc (dim * sizeof (double));
Packit 67cb25
Packit 67cb25
  if (state->y_onestep == 0)
Packit 67cb25
    {
Packit 67cb25
      free (state->y0_orig);
Packit 67cb25
      free (state->y0);
Packit 67cb25
      free (state->k);
Packit 67cb25
      free (state->yim1);
Packit 67cb25
      free (state);
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for y0_orig", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->primed = 0;
Packit 67cb25
  state->primer = gsl_odeiv_step_alloc (gsl_odeiv_step_rk4imp, dim);
Packit 67cb25
Packit 67cb25
  if (state->primer == 0)
Packit 67cb25
    {
Packit 67cb25
      free (state->y_onestep);
Packit 67cb25
      free (state->y0_orig);
Packit 67cb25
      free (state->y0);
Packit 67cb25
      free (state->k);
Packit 67cb25
      free (state->yim1);
Packit 67cb25
      free (state);
Packit 67cb25
      GSL_ERROR_NULL ("failed to allocate space for primer", GSL_ENOMEM);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  state->last_h = 0.0;
Packit 67cb25
Packit 67cb25
  return state;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
gear2_step (double *y, gear2_state_t * state,
Packit 67cb25
            const double h, const double t,
Packit 67cb25
            const size_t dim, const gsl_odeiv_system * sys)
Packit 67cb25
{
Packit 67cb25
  /* Makes a Gear2 advance with step size h.
Packit 67cb25
     y0 is the initial values of variables y. 
Packit 67cb25
     The implicit matrix equations to solve are:
Packit 67cb25
     k = y0 + h * f(t + h, k)
Packit 67cb25
     y = y0 + h * f(t + h, k)
Packit 67cb25
   */
Packit 67cb25
Packit 67cb25
  const int iter_steps = 3;
Packit 67cb25
  int nu;
Packit 67cb25
  size_t i;
Packit 67cb25
  double *y0 = state->y0;
Packit 67cb25
  double *yim1 = state->yim1;
Packit 67cb25
  double *k = state->k;
Packit 67cb25
Packit 67cb25
  /* Iterative solution of k = y0 + h * f(t + h, k)
Packit 67cb25
     Note: This method does not check for convergence of the
Packit 67cb25
     iterative solution! 
Packit 67cb25
   */
Packit 67cb25
Packit 67cb25
  for (nu = 0; nu < iter_steps; nu++)
Packit 67cb25
    {
Packit 67cb25
      int s = GSL_ODEIV_FN_EVAL (sys, t + h, y, k);
Packit 67cb25
Packit 67cb25
      if (s != GSL_SUCCESS)
Packit 67cb25
        {
Packit 67cb25
          return s;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      for (i = 0; i < dim; i++)
Packit 67cb25
        {
Packit 67cb25
          y[i] = ((4.0 * y0[i] - yim1[i]) + 2.0 * h * k[i]) / 3.0;
Packit 67cb25
        }
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
gear2_apply (void *vstate,
Packit 67cb25
             size_t dim,
Packit 67cb25
             double t,
Packit 67cb25
             double h,
Packit 67cb25
             double y[],
Packit 67cb25
             double yerr[],
Packit 67cb25
             const double dydt_in[],
Packit 67cb25
             double dydt_out[], const gsl_odeiv_system * sys)
Packit 67cb25
{
Packit 67cb25
  gear2_state_t *state = (gear2_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  state->stutter = 0;
Packit 67cb25
Packit 67cb25
  if (state->primed == 0 || t == state->t_primed || h != state->last_h)
Packit 67cb25
    {
Packit 67cb25
      /* Execute a single-step method to prime the process.  Note that
Packit 67cb25
       * we do this if the step size changes, so frequent step size
Packit 67cb25
       * changes will cause the method to stutter. 
Packit 67cb25
       * 
Packit 67cb25
       * Note that we reuse this method if the time has not changed,
Packit 67cb25
       * which can occur when the adaptive driver is attempting to find
Packit 67cb25
       * an appropriate step-size on its first iteration */
Packit 67cb25
Packit 67cb25
      int status;
Packit 67cb25
      DBL_MEMCPY (state->yim1, y, dim);
Packit 67cb25
Packit 67cb25
      status =
Packit 67cb25
        gsl_odeiv_step_apply (state->primer, t, h, y, yerr, dydt_in, dydt_out,
Packit 67cb25
                              sys);
Packit 67cb25
Packit 67cb25
      /* Make note of step size and indicate readiness for a Gear step. */
Packit 67cb25
Packit 67cb25
      state->primed = 1;
Packit 67cb25
      state->t_primed = t;
Packit 67cb25
      state->last_h = h;
Packit 67cb25
      state->stutter = 1;
Packit 67cb25
Packit 67cb25
      return status;
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      /* We have a previous y value in the buffer, and the step
Packit 67cb25
       * sizes match, so we go ahead with the Gear step.
Packit 67cb25
       */
Packit 67cb25
Packit 67cb25
      double *const k = state->k;
Packit 67cb25
      double *const y0 = state->y0;
Packit 67cb25
      double *const y0_orig = state->y0_orig;
Packit 67cb25
      double *const yim1 = state->yim1;
Packit 67cb25
      double *y_onestep = state->y_onestep;
Packit 67cb25
Packit 67cb25
      int s;
Packit 67cb25
      size_t i;
Packit 67cb25
Packit 67cb25
      /* initialization */
Packit 67cb25
      DBL_MEMCPY (y0, y, dim);
Packit 67cb25
Packit 67cb25
      /* Save initial values for possible failures */
Packit 67cb25
      DBL_MEMCPY (y0_orig, y, dim);
Packit 67cb25
Packit 67cb25
      /* iterative solution */
Packit 67cb25
Packit 67cb25
      if (dydt_out != NULL)
Packit 67cb25
        {
Packit 67cb25
          DBL_MEMCPY (k, dydt_out, dim);
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      /* First traverse h with one step (save to y_onestep) */
Packit 67cb25
Packit 67cb25
      DBL_MEMCPY (y_onestep, y, dim);
Packit 67cb25
Packit 67cb25
      s = gear2_step (y_onestep, state, h, t, dim, sys);
Packit 67cb25
Packit 67cb25
      if (s != GSL_SUCCESS)
Packit 67cb25
        {
Packit 67cb25
          return s;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      /* Then with two steps with half step length (save to y) */
Packit 67cb25
Packit 67cb25
      s = gear2_step (y, state, h / 2.0, t, dim, sys);
Packit 67cb25
Packit 67cb25
      if (s != GSL_SUCCESS)
Packit 67cb25
        {
Packit 67cb25
          /* Restore original y vector */
Packit 67cb25
          DBL_MEMCPY (y, y0_orig, dim);
Packit 67cb25
          return s;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      DBL_MEMCPY (y0, y, dim);
Packit 67cb25
Packit 67cb25
      s = gear2_step (y, state, h / 2.0, t + h / 2.0, dim, sys);
Packit 67cb25
Packit 67cb25
      if (s != GSL_SUCCESS)
Packit 67cb25
        {
Packit 67cb25
          /* Restore original y vector */
Packit 67cb25
          DBL_MEMCPY (y, y0_orig, dim);
Packit 67cb25
          return s;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      /* Cleanup update */
Packit 67cb25
Packit 67cb25
      if (dydt_out != NULL)
Packit 67cb25
        {
Packit 67cb25
          s = GSL_ODEIV_FN_EVAL (sys, t + h, y, dydt_out);
Packit 67cb25
Packit 67cb25
          if (s != GSL_SUCCESS)
Packit 67cb25
            {
Packit 67cb25
              /* Restore original y vector */
Packit 67cb25
              DBL_MEMCPY (y, y0_orig, dim);
Packit 67cb25
              return s;
Packit 67cb25
            }
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      /* Estimate error and update the state buffer. */
Packit 67cb25
Packit 67cb25
      for (i = 0; i < dim; i++)
Packit 67cb25
        {
Packit 67cb25
          yerr[i] = 4.0 * (y[i] - y_onestep[i]);
Packit 67cb25
          yim1[i] = y0[i];
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      /* Make note of step size. */
Packit 67cb25
      state->last_h = h;
Packit 67cb25
Packit 67cb25
      return 0;
Packit 67cb25
    }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static int
Packit 67cb25
gear2_reset (void *vstate, size_t dim)
Packit 67cb25
{
Packit 67cb25
  gear2_state_t *state = (gear2_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  DBL_ZERO_MEMSET (state->yim1, dim);
Packit 67cb25
  DBL_ZERO_MEMSET (state->k, dim);
Packit 67cb25
  DBL_ZERO_MEMSET (state->y0, dim);
Packit 67cb25
Packit 67cb25
  state->primed = 0;
Packit 67cb25
  state->last_h = 0.0;
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static unsigned int
Packit 67cb25
gear2_order (void *vstate)
Packit 67cb25
{
Packit 67cb25
  gear2_state_t *state = (gear2_state_t *) vstate;
Packit 67cb25
  state = 0;                    /* prevent warnings about unused parameters */
Packit 67cb25
  return 3;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
gear2_free (void *vstate)
Packit 67cb25
{
Packit 67cb25
  gear2_state_t *state = (gear2_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  free (state->yim1);
Packit 67cb25
  free (state->k);
Packit 67cb25
  free (state->y0);
Packit 67cb25
  free (state->y0_orig);
Packit 67cb25
  free (state->y_onestep);
Packit 67cb25
  gsl_odeiv_step_free (state->primer);
Packit 67cb25
Packit 67cb25
  free (state);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static const gsl_odeiv_step_type gear2_type = { "gear2",        /* name */
Packit 67cb25
  1,                            /* can use dydt_in */
Packit 67cb25
  0,                            /* gives exact dydt_out */
Packit 67cb25
  &gear2_alloc,
Packit 67cb25
  &gear2_apply,
Packit 67cb25
  &gear2_reset,
Packit 67cb25
  &gear2_order,
Packit 67cb25
  &gear2_free
Packit 67cb25
};
Packit 67cb25
Packit 67cb25
const gsl_odeiv_step_type *gsl_odeiv_step_gear2 = &gear2_type;