home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / GEPMAIN.C < prev    next >
C/C++ Source or Header  |  1993-08-24  |  13KB  |  458 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         main control block        */
  11. /*                                   */
  12. /*        Zortech C/C++ 3.0 r4       */
  13. /*          MICROSOFT C 6.00         */
  14. /*          Visual C/C++ 1.0         */
  15. /*           QuickC/WIN 1.0          */
  16. /*             ULTRIX cc             */
  17. /*              GNU gcc              */
  18. /*                                   */
  19. /*   (include here compilers that    */
  20. /*   compiled GEPASI successfully)   */
  21. /*                                   */
  22. /*************************************/
  23.  
  24.  
  25. #ifndef MSDOS
  26. #define COMMON
  27. #endif
  28.  
  29. #ifdef _WINDOWS
  30. #define COMMON
  31. #endif
  32.  
  33. #if (_MSC_VER >= 610)                           /* QuickC/WIN or C/C++ 7.0                              */
  34.  #define fcloseall _fcloseall
  35.  #define unlink _unlink
  36.  #define putenv _putenv
  37. #endif
  38. #if ((_MSC_VER == 600)||(_MSC_VER==700))        /* MICROSOFT C 6.00 & 7.00              */
  39.  #pragma comment( exestr, "Compiled on " __DATE__ " at " __TIME__ )
  40. #endif
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <time.h>
  45. #include <math.h>
  46. #include <float.h>
  47. #include <setjmp.h>
  48. #include <signal.h>
  49. #include <stdlib.h>
  50. /*#include <conio.h>*/
  51.  
  52. #ifdef _WINDOWS
  53. #include <io.h>
  54. #include <fcntl.h>
  55. #endif
  56.  
  57. #include "pmu.h"                                    /* several utilities     */
  58. #include "globals.h"                                /* global symbols        */
  59. #include "globvar.h"                                /* global variables      */
  60.  
  61. #ifndef _WINDOWS
  62. #include "except.c"                                 /* exception error handl.*/
  63. #endif
  64.  
  65. #include "r250.h"                                                                       /* pseudo-random numbers */
  66. #include "matrix.h"                                 /* matrix algebra        */
  67. #include "kinetics.h"                               /* rate eq. & deriv.     */
  68. #include "rates.h"                                  /* net rates of met.     */
  69. #include "gauss.h"                                  /* structural properties */
  70. #include "newton.h"                                 /* steady state          */
  71. #include "metconan.h"                               /* metabolic control     */
  72. #include "lsoda.h"
  73. #include "dynamic.h"                                    /* transient behaviour   */
  74. #include "datab.h"
  75. #include "getall.h"
  76. #include "gepout.h"                                 /* data output           */
  77. #include "heapchk.h"
  78.  
  79. #ifdef _ZTC
  80. #define MEM_DEBUG 1
  81. #include "mem.h"
  82. #else
  83. #define mem_malloc malloc
  84. #define mem_free free
  85. #define mem_realloc realloc
  86. #endif
  87.  
  88. /* tidy up memory and disk                                                      */
  89.  
  90. void tidy( void )
  91. {
  92.  /* if -d switch delete the file with list of simulations       */
  93.  if( delete ) unlink( listfile );
  94.  
  95.  /* free up all the dynamic memory blocks                                       */
  96.  FreeMem();
  97. }
  98.  
  99. /* terminate gepasi due to errors */
  100.  
  101. void term_gepasi( void )
  102. {
  103.  tidy();
  104.  fprintf(stderr,errormsg[2]);
  105. #ifdef _MSC_VER
  106.  fcloseall();
  107. #endif
  108. #ifdef _ZTC
  109.  mem_term();
  110. #endif
  111.  exit( 1 );                                         /* signal error          */
  112. }
  113.  
  114. /* resets the state of the internal metabolites         */
  115.  
  116. void reset_state( double *keep )
  117. {
  118.  int i;
  119.  
  120.  for( i=0; i<indmet; i++ ) x[i] = x0[i];
  121.  if( &endtime != keep ) endtime = options.endtime;
  122. }
  123.  
  124. /* sets the values of the linked variables                      */
  125.  
  126. void set_linked( void )
  127. {
  128.  int i;
  129.  
  130.  for( i=0; i<nlinks; i++)
  131.   switch( sparam[lindex[i]].operation )
  132.   {
  133.    case 1: *(sparam[lindex[i]].var) = sparam[lindex[i]].factor *
  134.                       *(sparam[lindex[i]].linkedto);
  135.        break;
  136.   }
  137. }
  138.  
  139.  
  140. /* main simulation procedure                                    */
  141.  
  142. int simulate( int p, int simple )
  143. {
  144.  int i, ccfs, diverge;
  145.  time_t itime, ftime;
  146.  
  147. #ifndef _WINDOWS
  148.   /* Save stack environment for return in case of error.                */
  149.   /*  First time through, jmpret is 0, so true conditional              */
  150.   /*  is executed. If an error occurs, jmpret will be set               */
  151.   /*  to -1 and false conditional will be executed.                             */
  152.   jmpret = setjmp( mark );
  153.   if( jmpret == 0 )
  154.   {
  155. #endif
  156.  
  157.   /* reset concentration of external metabolites*/
  158.   for( i=indmet; i<totmet; i++ ) x[i] = x0[i];
  159.  
  160.   /* set the values of the linked parameters    */
  161.   if( nlinks ) set_linked();
  162.  
  163.   /* initialize the conserved moiety concentrations                                             */
  164.   init_moiety();
  165.   /* print the first part of the report file    */
  166.   if( options.txt ) report_init( p );
  167.  
  168.   /* dynamic simulation */
  169.   if( options.dyn )
  170.   {
  171.    time(&itime);
  172.    dynamic();                           /* numeric integration */
  173.    time(&ftime);
  174.    ftime -= itime;
  175.  
  176.    if ( ! options.debug ) set_cursor(cx, cy);
  177.  
  178.    if( simple )
  179.     printf( "\n  - integration completed in %ld s, ", ftime );
  180.    else printf( "." );
  181.  
  182.    /* calculate the fluxes      */
  183.    calcfluxes( x );
  184.  
  185.    /* print integration results in report file  */
  186.    if( options.txt ) report_dyn();
  187.    if( options.dat  && (!options.datss) ) dat_values();
  188.   }
  189.   else
  190.    for( i=0; i<nmetab; i++) x[i] = 10 * options.hrcz;
  191.  
  192.   /* steady-state solution      */
  193.   if ( options.ss )
  194.   {
  195.    if( simple )
  196.     if( ! options.debug ) printf( "\n  - ss solution " );
  197.    time( &itime );
  198.    if( (diverge = newton()) )                               /* steady state                 */
  199.    {
  200.     endtime = 1e+10;                                        /* new endtime                  */
  201.     int_to_ss();                                            /* new numeric integr.          */
  202.     diverge = newton();                                        /* try again st. state            */
  203.    }
  204.    time(&ftime);
  205.    ftime -= itime;
  206.  
  207.    if ( options.debug ) printf( "\n\n  - ss solution in %ld s\n", ftime );
  208.    else
  209.     if( simple ) printf( "in %ld s", ftime );
  210.     else printf( "." );
  211.  
  212.    if ( diverge == N_OK )
  213.    {
  214.     calcfluxes( xss );
  215.     calc_tt();                                        /* calc. trans. times  */
  216.     calc_Dxv();                                       /* calc. elasticities  */
  217.     if ( ( ccfs = calc_Gamma() ) != MCA_NOCC )        /* calc. [] cnt.coeff. */
  218.      calc_C();                                        /* calc. J cnt.coeff.  */
  219.    }
  220.  
  221.    if( options.txt ) report_ss( diverge, ccfs );
  222.  
  223.    /* scale the control coefficients if necessary       */
  224.    if( (options.dat) && (!options.datmca ) ) scale_coef( );
  225.    /* copy xss to x                                                                     */
  226.    memcpy( (void *) x, (void *) xss, sizeof( x ) );
  227.  
  228.    /* output data in columnar file      */
  229.    if( options.dat && options.datss && (diverge==N_OK) ) dat_values();
  230.   }
  231.  
  232.   if( options.txt && options.debug ) report_tech();
  233.   return 0;
  234. #ifndef _WINDOWS
  235.   }
  236.   else
  237.   {
  238.    fpcheck();
  239.    if( options.txt ) report_error();
  240.    if( options.dat ) dat_error();
  241.   }
  242. #endif
  243. }
  244.  
  245. int hypergrid( int index, int p )
  246. {
  247.  int i;
  248.  
  249.  *(sparam[scindex[index]].var) = sparam[scindex[index]].low;
  250.  if( options.dyn ) reset_state(sparam[scindex[index]].var);
  251.  for( i=0; i<sparam[scindex[index]].dens+1; i++ )
  252.  {
  253.   if( index != 0 )
  254.   {
  255.    hypergrid( index-1, p );
  256.   }
  257.   else
  258.   {
  259.    printf("\n %lu ", ++iter );
  260.    simulate( p, 0 );
  261.   }
  262.   if( sparam[scindex[index]].log )
  263.    *(sparam[scindex[index]].var) = sparam[scindex[index]].low *
  264.                    pow( 10, sparam[scindex[index]].ampl /
  265.                         sparam[scindex[index]].dens * (i+1)
  266.                       );
  267.   else
  268.    *(sparam[scindex[index]].var) = sparam[scindex[index]].low +
  269.                    (i+1) *
  270.                    sparam[scindex[index]].ampl /
  271.                    sparam[scindex[index]].dens;
  272.   if( options.dyn ) reset_state(sparam[scindex[index]].var);
  273.   if( (totscan > 1) && (index == totscan - 1) ) dat_nl();
  274.  }
  275.  return 0;
  276. }
  277.  
  278. int randomfill( int p )
  279. {
  280.  unsigned long i;
  281.  int j;
  282.  
  283.  for( i=0; i< (unsigned long) options.scandens; i++ )
  284.  {
  285.   for( j=0; j<totscan; j++ )
  286.   {
  287.    if( sparam[scindex[j]].log )
  288.     *(sparam[scindex[j]].var) = sparam[scindex[j]].low * pow( 10,
  289.        sparam[scindex[j]].ampl * dr250() );
  290.    else
  291.     *(sparam[scindex[j]].var) = sparam[scindex[j]].low +
  292.        sparam[scindex[j]].ampl * dr250();
  293.    if( options.dyn ) reset_state(sparam[scindex[j]].var);
  294.   }
  295.   printf("\n %lu ", i );
  296.   simulate( p, 0 );
  297.  }
  298.  return 0;
  299. }
  300.  
  301. void process_sim( int p )
  302. {
  303.  int nRc, err;
  304.  
  305.  get_cursor(&cx, &cy);
  306.  
  307.  printf("\n\nprocessing %s: ",filename[p]);
  308.  
  309.  /* read data for this simulation       */
  310.  if ((nRc = ReadSim(filename[p])))
  311.  {
  312.   out_error( nRc );
  313.   term_gepasi();
  314.  }
  315.  if( options.dyn )
  316.  {
  317.   reset_state( NULL );
  318.   strcpy( dyname, filename[p] );                         /* setup .dyn filename    */
  319.   fixext( dyname, '.', ".dyn" );
  320.   unlink( dyname );
  321.  }
  322.  
  323.  /* set up indexes to effectors and pointers to rate equations  */
  324.  eff_rateq();
  325.  
  326.  if( options.dat  && (!options.append) ) dat_fileovr();
  327.  if( (options.dat) && (options.dattit == 0) ) dat_titles();
  328.  
  329.  err = 0;
  330.  
  331.  switch( options.scan )
  332.  {
  333.   case 0:  if( options.dyn ) reset_state( NULL );
  334.        simulate( p, 1 );
  335.        break;
  336.   case 1:  iter=0;
  337.        hypergrid( totscan-1, p );
  338.        break;
  339.   case 2:  randomfill( p );
  340.        break;
  341.   default: if( options.dyn ) reset_state( NULL );
  342.        simulate( p, 1 );
  343.  }
  344.  
  345.  if( (options.dat) & (options.dattit == 1) ) dat_titles();
  346. }
  347.  
  348. /* the main body */
  349.  
  350. int main(int argc, char *argv[])
  351. {
  352.  int p;
  353.  
  354. #ifdef _WINDOWS
  355.  struct _wsizeinfo ws;
  356. #if _MSC_VER == 800
  357.  ws._version = _winver;
  358. #else
  359.  ws._version = _WINVER;
  360. #endif
  361.  ws._type = _WINSIZEMAX;
  362.  _wsetsize( fileno( stdout ), &ws );
  363.  _wsetscreenbuf( fileno( stdout ), _WINBUFINF );
  364.  _wabout( "GEPASI 2.0 for MS-Windows\n       (C) P. Mendes 1992/93" );
  365.  _wsetexit( _WINEXITPERSIST );
  366. #endif
  367.  
  368. #ifdef _ZTC
  369. mem_init();
  370. #endif
  371.  
  372.  if( getenv( "TZ" ) == NULL )
  373.   if( putenv( "TZ=GMT0BST" ) == -1 )
  374.    printf( "\n  * unable to set GMT, using Pacific time zone (PST)\n" );
  375.  
  376.  /* randomize the rand seed with the time function      */
  377.  r250_init( (int) time( NULL ) );
  378.  
  379.  delete=0;
  380.  
  381.  /* check command line arguments and act accordingly    */
  382.  if ( argc > 1 )
  383.  {
  384.   if ( ! strcmp( "-h", argv[1] ) )
  385.   {
  386.    fprintf( stderr, "\n%s (%s)\n\n", gepasi, version_no );
  387.    fprintf( stderr, "This program is free software; you can redistribute it and/or modify\n");
  388.    fprintf( stderr, "it under the terms of the GNU General Public License as published by\n");
  389.    fprintf( stderr, "the Free Software Foundation; either version 1, or (at your option)\n");
  390.    fprintf( stderr, "any later version.\n\n");
  391.    fprintf( stderr, "This program is distributed in the hope that it will be useful,\n");
  392.    fprintf( stderr, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  393.    fprintf( stderr, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
  394.    fprintf( stderr, "GNU General Public License for more details.\n\n");
  395.    fprintf( stderr, "You should have received a copy of the GNU General Public License\n");
  396.    fprintf( stderr, "along with this program, in the file COPYING; if not, write to the\n");
  397.    fprintf( stderr, "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
  398.    return 0;
  399.   }
  400.   else
  401.   {
  402.    strcpy( listfile, argv[1] );
  403.    getlist( listfile );              /* file with list of simulations   */
  404.    if( argc > 2 )
  405.     if( !strcmp( argv[2], "-d" ) )   /* -d switch ?                                             */
  406.      delete = 1;
  407.   }
  408.  }
  409.  else getlist( (char *) NULL );      /* get list of simuls. from console*/
  410.  
  411.  /* allocate memory for several data structures         */
  412.  if( MemAlloc() == -1 )
  413.  {
  414.   fprintf( stderr, "\nnot enough memory for GEPASI!" );
  415.   term_gepasi();
  416.  }
  417.  
  418.  /* setup ktype database                                        */
  419.  InitDataBase();
  420.  
  421. #ifndef _WINDOWS
  422.  /* set up floating point error handler         */
  423.  /* this line generates a warning in Microsoft C compilers, just ignore it! */
  424.  if( signal( SIGFPE, fphandler ) == SIG_ERR )
  425.  {
  426.   printf("  * ERROR - Couldn't set floating point error handler\n" );
  427.   term_gepasi();
  428.  }
  429. #endif
  430.  
  431. cls();
  432.  
  433.  /* print screen banner                                     */
  434.  printf("%s\n%s\n",gepasi,version_no);
  435.  
  436.  /* print no.of simulations*/
  437.  printf("\n\n%d simulation%s",nrunfiles, nrunfiles==1 ? "" : "s ");
  438.  
  439.  /* execute all simulations     */
  440.  for(p=0;p<nrunfiles;p++)
  441.  {
  442.   process_sim( p );
  443.  }
  444.  /* clean memory and delete file if necessary                           */
  445.  tidy();
  446.  
  447. #ifdef _ZTC
  448.   mem_term();
  449. #endif
  450.  
  451.  /* make sure a carriage return is sent to stdout                       */
  452.  printf("\n");
  453.  
  454.  /* sound signal and screen message of program termination      */
  455.  beep();
  456.  fprintf(stderr,"\nAll done.\n\n");
  457. }
  458.