home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / sfit110.zip / DEMO / SWANSL.C < prev    next >
C/C++ Source or Header  |  1994-10-06  |  4KB  |  142 lines

  1. #define INCL_WIN
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <os2.h>
  7. // the standard number of data columns
  8. #define STDCOL 5
  9. #define XSLOT 0
  10. #define YSLOT 1
  11. #define ASLOT 2
  12. #define CALCSLOT -3
  13. #define BESTSLOT -2
  14. #define FLAGSLOT -1
  15.  
  16. /* static data and parameters */
  17. static double *data;                    /* start of complete data array  */
  18.  
  19. /* The data is all in a contigous block starting with datx then daty followed by
  20.  * zero or more additional data columns as read in from the data file. The last
  21.  * three slots are: datt , the current best results and the flag array. 
  22.  * Each point not in an exclusion zone has its corresponding flag TRUE. 
  23.  */
  24.  
  25.  
  26. static double *datx;                    /* x data read in from data file */
  27. static double *daty;                    /* y data read in from data file */
  28. static double *datt;                    /* generated data */
  29. static int Ndat;                        /* number of data points */
  30. static char firsttime;
  31. static double **Ptr;                    /* pointer to parameter pointer array */
  32.  
  33. struct info_line
  34.    {
  35.    char *line;
  36.    };
  37.  
  38. /*****************************************************************************/
  39. /* Do not modify any code not enclosed by this and the end banner            */
  40. /*****************************************************************************/
  41.  
  42. /* the next two values are compared to values passed by the program as check */
  43. /* NADD is the number of additional data columns required for this function  */
  44. /* NPARAM is the number of parameters required by this function              */
  45.  
  46. #define NADD 2              
  47. #define NPARAM 6
  48.  
  49. #define PI 3.14159
  50.  
  51. /* edit the following lines or add and delete new lines */ 
  52. static struct info_line dll_info[] = { 
  53.    "This function calculates magnetization ",
  54.    "of the liquid pool in magnetization transfer.",
  55.    "P1 is the solid spin relaxation time",
  56.    "P2 is the solid transverse relaxation time",
  57.    "P3 is the exchange rate",
  58.    "P4 is RM = RM0b/Ra",
  59.    "P5 is RT = 1/RaT2a",
  60.    "P6 is solid dipolar relaxation time",
  61.    NULL};
  62.    
  63. VOID EXPENTRY fitfunc(void)
  64.    {
  65.    int i, j;
  66.    double w1, D, Rrfb, num, den1, den2, Rb, T2b, R, RM, RT, T1d;
  67.    double Ds, T1b, M2, y, g, r, M2r;
  68.  
  69.    /* set pointers to additional read in data columns (if any) */
  70.    double *dat1,*dat2;
  71.    dat1 = datx + Ndat * (ASLOT + 0); 
  72.    dat2 = datx + Ndat * (ASLOT + 1); 
  73.  
  74.    /* The next section is executed the first time through only */
  75.    /* so put any initialization code in here                   */
  76.    if (firsttime) 
  77.       {
  78.       firsttime = FALSE;
  79.       }
  80.  
  81.    /* give parameters more meaningful names  */
  82.    Rb   = *Ptr[1];          
  83.    T2b  = *Ptr[2];
  84.    R    = *Ptr[3];
  85.    RM   = *Ptr[4];          
  86.    RT   = *Ptr[5];          
  87.    T1d  = *Ptr[6];
  88.  
  89.    Ds  = 1/T2b;
  90.    T1b = 1/Rb;
  91.    M2  = 1/(T2b*T2b);
  92.    
  93.    for (i = 0; i < Ndat;i++)
  94.       {
  95.       g = 0;
  96.       w1 = 2.0*PI * dat1[i];
  97.       D = 1000.0*2.0*PI*datx[i];
  98.       for (j=1;j<=91;j++)
  99.      {
  100.      r = (j-1)* PI/180.0;
  101.      M2r = M2 * (3.0*cos(r)*cos(r)-1.0) * (3.0*cos(r)*cos(r)-1) / 4.0;
  102.      y = sin(r) * 1/sqrt(2*PI*M2r) * exp(-D*D/(2.0*M2r));
  103.      g = g + y * PI /180.0;
  104.      }
  105.       Rrfb = w1*w1*PI*g;
  106.       num = (Ds*Ds+D*D*Rrfb*T1d)*(1+RM+R*T1b) + Ds*Ds*Rrfb*T1b;
  107.       den1 = (Ds*Ds+D*D*Rrfb*T1d)*(1+RM+R*T1b+(w1/D)*(w1/D)*RT*(1+T1b*R));
  108.       den2 = Ds*Ds*Rrfb*T1b*(1+(w1/D)*(w1/D)*RT+RM);
  109.       datt[i] = num/(den1+den2); 
  110.       }
  111.    }
  112.  
  113. /*****************************************************************************/
  114. /* Do not modify any code not enclosed by this and the start banner          */
  115. /*****************************************************************************/
  116.  
  117.  
  118.  
  119. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  120.    {
  121.    /* set globals */
  122.    firsttime = TRUE;
  123.    Ndat = N;
  124.    if (NADD != mb - STDCOL) return -1;
  125.    if (NPARAM != NP) return -2;
  126.    /* set array pointers */
  127.    data = dat;
  128.    daty = dat + N;
  129.    datx = dat;
  130.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  131.    /* set parameter pointers */
  132.    Ptr = p;
  133.    return 0;
  134.    }
  135.  
  136. struct info_line * EXPENTRY get_dll_info(void)
  137.    {
  138.    /* return pointer to array of info strings set up above */
  139.    return dll_info; 
  140.    }
  141.  
  142.