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

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         GWSIM - Simulation        */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*          Gauss reduction          */
  14. /*            dialog box             */
  15. /*                                   */
  16. /*           QuickC/WIN 1.0          */
  17. /*                                   */
  18. /*   (include here compilers that    */
  19. /*   compiled GWSIM successfully)    */
  20. /*                                   */
  21. /*************************************/
  22.  
  23. #include <windows.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include "globals.h"
  27. #include "gep2.h"
  28. #include "strtbl.h"
  29.  
  30. #define ALMOST_ZERO 1.0e-7
  31.  
  32. GLOBALHANDLE    hRSto;                    /* handle to memory block w/ rstoi        */
  33. GLOBALHANDLE    hSto;                    /* handle to memory block w/ stoi        */
  34. GLOBALHANDLE    hMl;                    /* handle to memory block w/ ml            */
  35. GLOBALHANDLE    hLm;                    /* handle to memory block w/ lm            */
  36. GLOBALHANDLE    hLD;                    /* handle to memory block w/ ld            */
  37. GLOBALHANDLE    hMetn;                    /* handle to memory block w/ metn        */
  38. GLOBALHANDLE    hStepn;                    /* handle to memory block w/ stepn        */
  39. GLOBALHANDLE    hStepst;                /* handle to memory block w/ stepst        */
  40. GLOBALHANDLE    hImet;                    /* handle to memory block w/ imet        */
  41. GLOBALHANDLE    hRs;                    /* handle to memory block w/ rs            */
  42. GLOBALHANDLE    hLoo;                    /* handle to memory block w/ loo        */
  43. float            huge *rstoi;            /* pointer to work with rstoi array        */
  44. float             huge *ml;                /* pointer to work with ml array        */
  45. float             huge *lm;                /* pointer to work with lm array        */
  46. float             huge *ld;                /* pointer to work with ld array        */
  47. int                huge *imet;                /* pointer to work with imet vector        */
  48. int                huge *loo;                /* pointer to work with loo array        */
  49. int        (huge *rs)[MAX_STEP][MAX_MOL];        /* pointer to work with rs array        */
  50. char    (huge *metn)[NAME_L];            /* pointer to work with metname array    */
  51. char    (huge *stepn)[NAME_L];            /* pointer to work with metname array    */
  52. char    (huge *stepst)[256];            /* pointer to work with stepst array    */
  53. int ur[MAX_MET];                        /* permut. on metabolites  u -> g         */
  54. int uc[MAX_MET];                        /* permutations on steps   u -> g        */
  55.  
  56. void rowsw( int r1, int r2, int c );
  57. void colsw( int c1, int c2, int r );
  58. void rowsw_m( int r1, int r2, int c );
  59. void colsw_m( int c1, int c2, int r );
  60. int emptyrow( int rw, int nsteps);
  61. void invml11( void );
  62. void calc_ld( void );
  63. void init_moiety( void );
  64. void lindep( void );
  65. int gauss( void );
  66. void more_ext( void);
  67. void int_ord( void );
  68. void ext_ord( void );
  69. void initreds( void );
  70. void virt_step( void );
  71. int reduce( int swapnames );
  72.  
  73. #pragma alloc_text( CODE14, rowsw, colsw, rowsw_m, colsw_m, emptyrow, invml11, calc_ld, init_moiety, lindep, gauss, int_ord, ext_ord, more_ext, initreds, virt_step, reduce )
  74.  
  75. void rowsw( int r1, int r2, int c )
  76. {
  77.  register int j;
  78.  float dummy;
  79.  double dumb;
  80.  int dum;
  81.  
  82.  for(j=0;j<c;j++)
  83.  {
  84.   dum         = stoi[r1*MAX_MET + j];                      /* switch rows in stoi and */
  85.   stoi[r1*MAX_MET + j] = stoi[r2*MAX_MET + j];
  86.   stoi[r2*MAX_MET + j] = dum;
  87.   dummy        = rstoi[r1*MAX_MET + j];                    /* switch rows in rstoi &  */
  88.   rstoi[r1*MAX_MET + j] = rstoi[r2*MAX_MET + j];
  89.   rstoi[r2*MAX_MET + j] = dummy;
  90.  }
  91.  j      = ur[r1];                                 /* switch rows in ur and   */
  92.  ur[r1] = ur[r2];
  93.  ur[r2] = j;
  94. }
  95.  
  96. void colsw( int c1, int c2, int r )
  97. {
  98.  register int j;
  99.  float dummy;
  100.  int dum;
  101.  
  102.  for( j=0; j<r; j++ )
  103.  {
  104.   dum         = stoi[j*MAX_MET + c1];                      /* switch cols in stoi and */
  105.   stoi[j*MAX_MET + c1] = stoi[j*MAX_MET + c2];
  106.   stoi[j*MAX_MET + c2] = dum;
  107.   dummy        = rstoi[j*MAX_MET + c1];                    /* switch cols in rstoi &  */
  108.   rstoi[j*MAX_MET + c1] = rstoi[j*MAX_MET + c2];
  109.   rstoi[j*MAX_MET + c2] = dummy;
  110.  }
  111.  j      = uc[c1];                                 /* switch cols in uc too   */
  112.  uc[c1] = uc[c2];
  113.  uc[c2] = j;
  114. }
  115.  
  116. void rowsw_m( int r1, int r2, int c )             /* switch rows in ml       */
  117. {
  118.  register int j;
  119.  float dummy;
  120.  
  121.  for(j=0;j<c;j++)
  122.  {
  123.   dummy     = ml[r1*MAX_MET + j];
  124.   ml[r1*MAX_MET + j] = ml[r2*MAX_MET + j];
  125.   ml[r2*MAX_MET + j] = dummy;
  126.  }
  127. }
  128.  
  129. void colsw_m( int c1, int c2, int r )             /* switch cols in ml       */
  130. {
  131.  register int j;
  132.  float dummy;
  133.  
  134.  for(j=0;j<r;j++)
  135.  {
  136.   dummy     = ml[j*MAX_MET + c1];
  137.   ml[j*MAX_MET + c1] = ml[j*MAX_MET + c2];
  138.   ml[j*MAX_MET + c2] = dummy;
  139.  }
  140. }
  141.  
  142. int emptyrow( int rw, int nsteps)
  143. {
  144.  register int j, ct;                              /* ct counts entries != 0  */
  145.  
  146.  for( ct=0, j=0; j<nsteps; j++)
  147.   if ( fabs( rstoi[rw*MAX_MET + j] ) > ALMOST_ZERO ) ct++;
  148.  return ( ct );
  149. }
  150.  
  151. void invml11( void )
  152. {                                                 /* as ml is lower triangul.*/
  153.  register int i,j,k;                              /* inverting is simply to  */
  154.  float acum;                                      /* forward-substitute with */
  155.                                                   /* the identity matrix     */
  156.  for(i=0;i<nmetab; i++) ml[i*MAX_MET + i] = 1 ;
  157.  for( j=0; j<indmet; j++)
  158.   for( k=0; k<indmet; k++)
  159.   {
  160.    for( acum=0, i=0; i<k; i++ )
  161.     acum += ml[k*MAX_MET + i] * lm[i*MAX_MET + j];
  162.    lm[k*MAX_MET + j] = ( (k==j) ? (float) 1.0 : - acum ) / ml[k*MAX_MET + k];
  163.   }
  164. }
  165.  
  166. void calc_ld( void )
  167. {
  168.  register int i,j,k;
  169.  
  170.  for( i=indmet; i<nmetab; i++ )                   /* first calculate L0      */
  171.   for( j=0; j<indmet; j++ )                       /* which is                */
  172.   {
  173.    ld[i*MAX_MET + j] = 0;
  174.    for( k=0; k<indmet; k++ )                      /*                 -1      */
  175.     ld[i*MAX_MET + j] += ml[i*MAX_MET + k] * lm[k*MAX_MET + j];              /* L0 = L21 * (L11)        */
  176.   }
  177.  for( i=indmet; i<nmetab; i++ )                   /* Now map the lin. dep.   */
  178.  {
  179.   for( j=0; j<indmet; j++ )                       /* which is                */
  180.    ld[i*MAX_MET + j] = -ld[i*MAX_MET + j];                          /* -L0                     */
  181.   for( j=indmet; j<nmetab; j++ )                  /* concateneted with the   */
  182.    ld[i*MAX_MET + j] = (i == j)
  183.     ? (float) 1.0 : (float) 0.0;                  /* m0 -> m part of Id.     */
  184.  }
  185. }
  186.  
  187. void init_moiety( void )
  188. {
  189.  register int i,j;
  190.  
  191.  for( i=indmet; i<nmetab; i++)
  192.  {
  193.   moiety[i] = (float) 0.0;
  194.   for( j=0; j<nmetab; j++ )
  195.    moiety[i] += ld[i*MAX_MET + j] * xu[j];
  196.  }
  197. }
  198.  
  199. void lindep( void )
  200. {
  201.  invml11();                                       /* invert ml11 into lm     */
  202.  calc_ld();                                       /* calculate L2 * -L1'     */
  203.  init_moiety();                                   /* setup the moiety conc.  */
  204. }
  205.  
  206. int gauss( void )
  207. {
  208.  register int i, j;
  209.  int k, flag;
  210.  float m;
  211.  
  212.  for( k=0; k<nsteps+1; k++)
  213.  {
  214.   for( flag=0, i=k; i<nmetab; i++ )
  215.    if( fabs(rstoi[i*MAX_MET + k]) > ALMOST_ZERO )
  216.    {
  217.     flag = 1;
  218.     break;                                        /* suitable divisor        */
  219.    }
  220.   if ( flag )
  221.   {
  222.    if ( i != k )
  223.    {
  224.     rowsw( k, i, nsteps );                        /* switch row k with i     */
  225.     rowsw_m( k, i, nmetab );
  226.    }
  227.   }
  228.   else
  229.   {
  230.    do
  231.    {
  232.     for( j=k+1; j<nsteps; j++ )
  233.      if( fabs(rstoi[k*MAX_MET + j]) > ALMOST_ZERO )
  234.      {
  235.       flag = 1;
  236.       break;                                      /* suitable value          */
  237.      }
  238.     if ( flag && ( j != k ) )
  239.     {
  240.      colsw( j, k, nmetab );                       /* switch col j with k     */
  241.      colsw_m( j, k, nmetab );
  242.     }
  243.     if( !flag )
  244.     {
  245.      for( i=0; i<nmetab; i++ )                    /* get rid of empty rows   */
  246.      {
  247.       if ( ! emptyrow( i, nsteps ) )              /* all entries in row = 0  */
  248.       {
  249.        for( j=i+1; j<nmetab; j++ )
  250.         if ( emptyrow( j, nsteps ) )              /* j = first non-empty row */
  251.         {
  252.          rowsw( i, j, nsteps );                   /* switch row i with row j */
  253.          rowsw_m( i, j, nmetab );
  254.          flag = 2;
  255.          break;
  256.         }
  257.       }
  258.      }
  259.     }
  260.     if ( !flag )
  261.     {
  262.      indmet = k;                                  /* # of independent metabs */
  263.      return(-1);                                  /* flag conservation & ret */
  264.     }
  265.    }
  266.    while( flag != 1 );
  267.   }
  268.   for( i=k+1; i<nmetab; i++)
  269.   {
  270.    if ( fabs(rstoi[k*MAX_MET + k]) > ALMOST_ZERO )
  271.    {
  272.     m =  rstoi[i*MAX_MET + k] / rstoi[k*MAX_MET + k];               /* calculate the divisor   */
  273.     ml[i*MAX_MET + k] = m;                                 /* and keep its symmetric  */
  274.     if( m != (float) 0.0 )
  275.     {
  276.      for( j=k; j<nsteps; j++ )
  277.      {
  278.       rstoi[i*MAX_MET + j] = rstoi[i*MAX_MET + j] - m*rstoi[k*MAX_MET + j];  /* reduce another row      */
  279.       if( fabs(rstoi[i*MAX_MET + j]) <= ALMOST_ZERO )      /* then make it a true zero*/
  280.        rstoi[i*MAX_MET + j] = (float) 0.0;
  281.      }
  282.     }
  283.    }
  284.   }
  285.  }
  286.  return 0;                                       /* job done: no lin. dep.! */
  287. }
  288.  
  289. void int_ord( void )                       /* start by putting all internal  */
  290. {                                          /* metabolites as the first       */
  291.  register int i,j,k;                       /* m rows in stoi                 */
  292.  
  293.  for( i=0, k=0; i<totmet; i++ )
  294.   if (intmet[i])
  295.   {
  296.    ur[k] = i;                              /* record the old number          */
  297.    for( j=0; j<nsteps; j++ )
  298.     stoi[k*MAX_MET + j] = stoiu[i*MAX_MET + j];
  299.    k++;
  300.   }
  301.  nmetab = k;                               /* the no.of internal metabolites */
  302. }
  303.  
  304. void ext_ord( void )                       /* now put the declared exernal   */
  305. {                                          /* metabolites in the last rows   */
  306.  register int i,j,k;                       /* of stoi                        */
  307.  
  308.  k = nmetab;
  309.  for( i=0; i<totmet; i++ )
  310.   if (!intmet[i])
  311.   {
  312.    ur[k] = i;                              /* record the old number          */
  313.    for( j=0; j<nsteps; j++ )
  314.     stoi[k*MAX_MET + j] = stoiu[i*MAX_MET + j];
  315.    k++;
  316.   }
  317.  nextmet = totmet - nmetab;                /* the no.of external metabolites */
  318. }
  319.  
  320. void initreds( void )
  321. {
  322.  register int i,j;
  323.  
  324.  for( i=0; i<nmetab; i++)
  325.  {
  326.   for( j=0; j<nsteps; j++)
  327.    rstoi[i*MAX_MET + j] = (float) stoi[i*MAX_MET + j];              /* init rstoi from stoi    */
  328.   for( j=0; j<nmetab; j++)
  329.    ml[i*MAX_MET + j] = (float) 0;                          /* init matrix of multipl. */
  330.  }
  331. }
  332.  
  333. void more_ext( void)
  334. {
  335.  register int i;
  336.  
  337.  for( i=0; i<nmetab; i++ )                        /* get rid of empty rows   */
  338.  {                                                /* which are xtrnl metabs. */
  339.   if ( ! emptyrow( i, nsteps ) )                  /* all entries in row = 0  */
  340.   {
  341.    rowsw( i, nmetab-1, nsteps );                  /* put row at the end      */
  342.    intmet[ur[nmetab]] = 0;                        /* signal this 1 is xtrnl! */
  343.    nmetab--;                                      /* signal 1 less int. met. */
  344.    nextmet++;                                     /* and 1 more extern. met. */
  345.   }
  346.  }
  347. }
  348.  
  349. void virt_step( void )
  350. {
  351.  register int i, j, ct;
  352.  
  353.  for( i=0; i<nsteps; i++ )                        /* get rid of empty cols   */
  354.  {                                                /* which are 0 rate steps  */
  355.   for( ct=0, j=0; j<nmetab; j++)
  356.    if ( fabs( rstoi[j*MAX_MET + i] ) > ALMOST_ZERO ) ct++; /* ct counts entries != 0  */
  357.   if ( !ct )                                      /* all entries in col = 0  */
  358.   {
  359.    colsw( i, nsteps-1, nmetab );                  /* put col at the end      */
  360.    nsteps--;                                      /* one less active step    */
  361.   }
  362.  }
  363. }
  364.  
  365. int reduce( int swapnames )
  366. {
  367.  int i, j, k, ix, s;
  368.  int kinetype[MAX_STEP];                            /* type of kinetics (user numb.)        */
  369.  
  370.  /* first lets allocate memory for mirrors and aux matrices    */
  371.  
  372.  hRSto = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * MAX_STEP * sizeof( float ) );
  373.  if( hRSto == NULL )
  374.   return IDS_ERR_OUT_OF_MEM;
  375.  hMetn = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * NAME_L * sizeof( char ) );
  376.  if( hMetn == NULL )
  377.  {
  378.   GlobalFree( hRSto );
  379.   return IDS_ERR_OUT_OF_MEM;
  380.  }
  381.  hStepn = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * NAME_L * sizeof( char ) );
  382.  if( hStepn == NULL )
  383.  {
  384.   GlobalFree( hMetn );
  385.   GlobalFree( hRSto );
  386.   return IDS_ERR_OUT_OF_MEM;
  387.  }
  388.  hStepst = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * 256 * sizeof( char ) );
  389.  if( hStepst == NULL )
  390.  {
  391.   GlobalFree( hStepn );
  392.   GlobalFree( hMetn );
  393.   GlobalFree( hRSto );
  394.   return IDS_ERR_OUT_OF_MEM;
  395.  }
  396.  hMl = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * MAX_MET * sizeof( float ) );
  397.  if( hMl == NULL )
  398.  {
  399.   GlobalFree( hStepn );
  400.   GlobalFree( hStepst );
  401.   GlobalFree( hMetn );
  402.   GlobalFree( hRSto );
  403.   return IDS_ERR_OUT_OF_MEM;
  404.  }
  405.  hLm = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * MAX_MET * sizeof( float ) );
  406.  if( hLm == NULL )
  407.  {
  408.   GlobalFree( hMl );
  409.   GlobalFree( hStepst );
  410.   GlobalFree( hStepn );
  411.   GlobalFree( hMetn );
  412.   GlobalFree( hRSto );
  413.   return IDS_ERR_OUT_OF_MEM;
  414.  }
  415.  hLD = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * MAX_MET * sizeof( float ) );
  416.  if( hLD == NULL )
  417.  {
  418.   GlobalFree( hLm );
  419.   GlobalFree( hMl );
  420.   GlobalFree( hStepst );
  421.   GlobalFree( hStepn );
  422.   GlobalFree( hMetn );
  423.   GlobalFree( hRSto );
  424.   return IDS_ERR_OUT_OF_MEM;
  425.  }
  426.  hImet = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * sizeof( int ) );
  427.  if( hImet == NULL )
  428.  {
  429.   GlobalFree( hLD );
  430.   GlobalFree( hLm );
  431.   GlobalFree( hMl );
  432.   GlobalFree( hStepst );
  433.   GlobalFree( hStepn );
  434.   GlobalFree( hMetn );
  435.   GlobalFree( hRSto );
  436.   return IDS_ERR_OUT_OF_MEM;
  437.  }
  438.  hRs = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * MAX_MOL * sizeof( int ) );
  439.  if( hRs == NULL )
  440.  {
  441.   GlobalFree( hImet );
  442.   GlobalFree( hLD );
  443.   GlobalFree( hLm );
  444.   GlobalFree( hMl );
  445.   GlobalFree( hStepst );
  446.   GlobalFree( hStepn );
  447.   GlobalFree( hMetn );
  448.   GlobalFree( hRSto );
  449.   return IDS_ERR_OUT_OF_MEM;
  450.  }
  451.  hLoo = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * MAX_MET * sizeof( int ) );
  452.  if( hLoo == NULL )
  453.  {
  454.   GlobalFree( hRs );
  455.   GlobalFree( hImet );
  456.   GlobalFree( hLD );
  457.   GlobalFree( hLm );
  458.   GlobalFree( hMl );
  459.   GlobalFree( hStepst );
  460.   GlobalFree( hStepn );
  461.   GlobalFree( hMetn );
  462.   GlobalFree( hRSto );
  463.   return IDS_ERR_OUT_OF_MEM;
  464.  }
  465.  /* and lock the memory blocks            */
  466.  rstoi    = (float huge *) GlobalLock( hRSto );
  467.  metn    = (char (huge *)[NAME_L]) GlobalLock( hMetn );
  468.  stepn    = (char (huge *)[NAME_L]) GlobalLock( hStepn );
  469.  stepst    = (char (huge *)[256]) GlobalLock( hStepst );
  470.  ml        = (float huge *) GlobalLock( hMl );
  471.  lm        = (float huge *) GlobalLock( hLm );
  472.  ld        = (float huge *) GlobalLock( hLD );
  473.  imet    = (int huge *) GlobalLock( hImet );
  474.  rs        = (int (huge *)[MAX_MET][MAX_MOL]) GlobalLock( hRs );
  475.  loo    = (int huge *) GlobalLock( hLoo );
  476.  
  477.  for( i=0; i<nsteps; i++) uc[i] = i;       /* setup reaction perm. vector    */
  478.  for( i=0; i<totmet; i++) ur[i] = i;       /* setup metabolite perm. vector  */
  479.  
  480.  int_ord();                                /* put int. metabs. at the beggin.*/
  481.  ext_ord();                                /* put ext. metabs. at the end    */
  482.  initreds();                               /* reset rstoi and ml             */
  483.  more_ext();                               /* spot implicit external metabs. */
  484.  virt_step();                              /* spot virtual steps             */
  485.  gauss();                                  /* stoi -> rstoi by gaussian red. */
  486.  
  487.  /* make the mirrors be indexed by the new mapping        */
  488.  /* if swapnames remap metabolite and step names        */
  489.  
  490.  for( i=0; i<nsteps; i++ )
  491.  {
  492.   if( swapnames )
  493.   {
  494.    lstrcpy( (LPSTR) stepn[i], (LPSTR) stepname[uc[i]] );
  495.    lstrcpy( (LPSTR) stepst[i], (LPSTR) stepstr[uc[i]] );
  496.    kinetype[i] = kinetu[uc[i]];
  497.    for(j=0;j<totmet;j++)
  498.     loo[MAX_STEP*i+j] = (*loop)[uc[i]][ur[j]];
  499.    for(j=0;j<MAX_MOL;j++)
  500.    {
  501.     ix = (*rstr)[uc[i]][j];
  502.     if( ix == 0 )
  503.     {
  504.      (*rs)[i][j] = 0;
  505.      continue;
  506.     }
  507.     s = ix/ abs( ix );  /*the signal*/
  508.     ix = abs( ix )-1;   /*the index */
  509.     for( k=0; k<totmet; k++ )
  510.      if( ix == ur[k] ) break;
  511.     (*rs)[i][j] = s*(k+1);
  512.    }
  513.   }
  514.   else
  515.   {
  516.    lstrcpy( (LPSTR) stepn[i], (LPSTR) stepname[i] );
  517.    lstrcpy( (LPSTR) stepst[i], (LPSTR) stepstr[i] );
  518.    kinetype[i] = kinetu[i];
  519.    for(j=0;j<totmet;j++)
  520.     loo[MAX_STEP*i+j] = (*loop)[i][j];
  521.    for(j=0;j<MAX_MOL;j++)
  522.     (*rs)[i][j] = (*rstr)[i][j];
  523.   }
  524.  }
  525. /*  if( swapnames )
  526.   {
  527.    for( i=0; i<nsteps; i++)
  528.     for(j=0;j<MAX_MOL;j++)
  529.     {
  530.      if( (*rstr)[uc[i]][j] > 0 )
  531.       (*rs)[i][j] = ur[(*rstr)[uc[i]][j] - 1] + 1;
  532.      else
  533.       if( (*rstr)[uc[i]][j] < 0 )
  534.        (*rs)[i][j] = -ur[-(*rstr)[uc[i]][j] - 1] - 1;
  535.       else
  536.        (*rs)[i][j] = 0;
  537.     }
  538.   }  */
  539.  
  540.  for( i=0; i<totmet; i++ )
  541.   if( swapnames )
  542.   {
  543.    lstrcpy( (LPSTR) metn[i], (LPSTR) metname[ur[i]] );
  544.    imet[i] = intmet[ur[i]];
  545.   }
  546.   else
  547.   {
  548.    lstrcpy( (LPSTR) metn[i], (LPSTR) metname[i] );
  549.    imet[i] = intmet[i];
  550.   }
  551.  
  552.  /* copy everything back to the original structures        */
  553.  _fmemcpy( (void __far *) metname,
  554.              (void __far *) metn,
  555.               (size_t) MAX_MET * NAME_L * sizeof( char ) );
  556.  _fmemcpy( (void __far *) stepname,
  557.               (void __far *) stepn,
  558.               (size_t) MAX_STEP * NAME_L * sizeof( char ) );
  559.  _fmemcpy( (void __far *) stepstr,
  560.               (void __far *) stepst,
  561.               (size_t) MAX_STEP * 256 * sizeof( char ) );
  562.  _fmemcpy( (void __far *) kinetu,
  563.               (void __far *) kinetype,
  564.            (size_t) MAX_STEP * sizeof( int ) );
  565.  _fmemcpy( (void __far *) intmet,
  566.               (void __far *) imet,
  567.            (size_t) MAX_MET * sizeof( int ) );
  568.  _fmemcpy( (void __far *) rstr,
  569.               (void __far *) rs,
  570.            (size_t) MAX_STEP * MAX_MOL * sizeof( int ) );
  571.  for(i=0;i<nsteps;i++)
  572.   for(j=0;j<totmet;j++)
  573.    (*loop)[i][j]  = (unsigned char) loo[MAX_STEP*i+j];
  574.  
  575.  lindep();                                 /* work out linear dependencies   */
  576.  
  577.  /* unlock and free the mirrors                            */
  578.  GlobalUnlock( hLoo );
  579.  GlobalUnlock( hRs );
  580.  GlobalUnlock( hImet );
  581.  GlobalUnlock( hLD );
  582.  GlobalUnlock( hLm );
  583.  GlobalUnlock( hMl );
  584.  GlobalUnlock(  hStepst );
  585.  GlobalUnlock( hStepn );
  586.  GlobalUnlock( hMetn );
  587.  GlobalUnlock( hRSto );
  588.  GlobalFree( hLoo );
  589.  GlobalFree( hRs );
  590.  GlobalFree( hImet );
  591.  GlobalFree( hLD );
  592.  GlobalFree( hLm );
  593.  GlobalFree( hMl );
  594.  GlobalFree(  hStepst );
  595.  GlobalFree( hStepn );
  596.  GlobalFree( hMetn );
  597.  GlobalFree( hRSto );
  598.  return 0;
  599. }
  600.