/*
 * file:   s1dman_compute.c
 * author: Hinke Osinga and Reza Rokni --- University of Exeter
 * date:   April 2001
 *
 * driver for computing strong one-d manifolds of fixed points
 */

/*
 * s1dman_compute(struct Fixpt_DataS*, int)
 *
 */


#include <stdio.h>

#include "memory.h"
#include "constants.h"
#include "utilities.h"
#include "eigen.h"
#include "fixed.h"
#include "$DSTOOL/src/fixed/fixed_local.h"      
                                /* replace $DSTOOL with full address */
#include "ssman1d.h"

int s1dman_compute(struct Fixpt_DataS* fp, int us)
{
  int      status = 0;
  int      num = 0;
  double   *points, *params;
  int      flow_type(),j,k,var_dim, *extra_iparams, n_extra_iparams;

  int      It = (fp->stab_steps*fp->stab_pts > 
		 fp->unstab_steps*fp->unstab_pts) ?
    (fp->stab_steps*fp->stab_pts) : (fp->unstab_steps*fp->unstab_pts);/*11/02/02*/

  system_mess_proc(0,"\nComputing strong 1-d manifolds of fixed points.");
  
  if (fp->prop_cntl.mapping_flag){
    system_mess_proc(1,"This software is for vector fields only!!\n");
    return 0;
  }

  var_dim = fp->prop_cntl.ph_space_dim-1;

  /* search memory object for any fixed points */
  if (memory_reset_read(fp->memory) == 0){
    while (memory_read_next_flow(fp->memory, NULL, NULL, NULL, 
				 &extra_iparams, &n_extra_iparams) == 0  
	   && status>=0){
      while (memory_read_next_traj(fp->memory, &fp->fp_map_period, 
				   NULL, NULL) == 0 && status >= 0){
	fp->fptype = extra_iparams[0];

	/* Have to make sure object in memory is fixed point */
	/* It could be a manifold!! */
	if ((fp->fptype != STAB_MAN) && (fp->fptype != UNSTAB_MAN)){
	  while (memory_read_next_point(fp->memory, &points, &params,
					NULL, NULL, NULL) == 0 
		 && status >= 0){
	    fp->x1[var_dim] = points[var_dim];
	    for (j=0; j < var_dim; j++){
	      fp->x1[j] = points[j];
	      fp->x2[j] = fp->fd_step;
	    }
	    for (j=0; j < fp->prop_cntl.parameter_dim; j++)
	      fp->prop_cntl.parameters[j] = params[j];
	    fp_jac(fp,0);

	    if (var_dim==1){
	      fp->eval[0][0] = fp->jacobian[0][0];
	      fp->eval[0][1] = 0.0;
	      fp->evectors[0][0] = 1.0;
	    }
	    else{
	      /* use converted Fortran routines to compute */
	      /* eigenvalues and eigenvectors */
	      for (j=0; j < var_dim; j++){
		for (k=0; k < var_dim; k++)
		  fp->jact[j+1][k+1] = fp->jacobian[j][k];
	      }
	      status = rg(var_dim, var_dim, fp->jact, fp->eval[0]-1, 
			  fp->eval[1]-1, 1, fp->evectors, fp->indx, fp->x3);
	      for (j=0; j < var_dim; j++){
		for (k=0; k < var_dim; k++)
		  fp->evectors[j][k] = fp->evectors[j+1][k+1];
	      }
	    }

	    status = ssman1d_ode(fp,us,It);/*11/02/02*/
	    if (status >= 0)
	      num++;
	    else
	      status = 0;
	  }
	}
      }
    }
  }

  if (num == 0){
    if (us == 0)
      system_mess_proc(1,"No 1-d strong stable manifolds to draw.");
    else
      system_mess_proc(1,"No 1-d strong unstable manifolds to draw.");
  }
  
  return(status);
}


