home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / NERVES.ZIP / 87 / INITSIM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-12  |  12.7 KB  |  570 lines

  1. #include "defn.h"
  2. #include "nsdata.c"
  3. #include "proto.h"
  4. #include "update.c"
  5.  
  6. int initsim(void)
  7.  { /* initialize simulation */
  8.  
  9.   int i,j,k,run,step,savIext;
  10.   char str[26],*p;
  11.   enum asc_val asc;
  12.   enum ext_val ext;
  13.   double r,t;
  14.   float f;
  15.   struct neuron *np,*nq;
  16.   struct con *cp;
  17.   struct neurcalc *ncp;
  18.   struct Iint *ip;
  19.   FILE *recfile;
  20.  
  21.   /* find all connection names */
  22.   for (j=0, np=ns; j<nn; j++, np++)
  23.    {
  24.     if (np->name[0])
  25.      for (cp=np->con; cp != NULL; cp=cp->next)
  26.       {
  27.        for (i=0, nq=ns; i<nn; i++, nq++)
  28.     if (nq->name[0] && !strcmp(cp->sname,nq->name))
  29.      break;
  30.        if (i == nn)
  31.     { /* name not found */
  32.      gotoxy(1,1);
  33.      cprintf("Sending neuron %s for neuron %s not found",cp->sname,np->name);
  34.      gotoxy(1,2);
  35.      cprintf("Press any key and N to correct");
  36.      bioskey(0);
  37.      clrline(1);
  38.      clrline(2);
  39.      erasecursor();
  40.      return(0);
  41.     }
  42.        else
  43.     cp->s = i;
  44.        if (cp->ctype)
  45.     {
  46.      for (i=0, nq=ns; i<nn; i++, nq++)
  47.       if (nq->name[0] && !strcmp(cp->cname,nq->name))
  48.        break;
  49.      if (i == nn)
  50.       { /* name not found */
  51.        gotoxy(1,1);
  52.        cprintf("Compound neuron %s for neuron %s not found",cp->cname,np->name);
  53.        gotoxy(1,2);
  54.        cprintf("Press any key and N to correct");
  55.        bioskey(0);
  56.        clrline(1);
  57.        clrline(2);
  58.        erasecursor();
  59.        return(0);
  60.       }
  61.      else
  62.       cp->c = i;
  63.     }
  64.       }
  65.    }
  66.  
  67.   /* find all graph names */
  68.   for (j=0; j<5; j++)
  69.    if (gr[j].gtype == 1)
  70.     {
  71.      for (i=0, nq=ns; i<nn; i++, nq++)
  72.       if (nq->name[0] && !strcmp(gr[j].gneur,nq->name))
  73.        break;
  74.      if (i == nn)
  75.       { /* name not found */
  76.        gotoxy(1,1);
  77.        cprintf("Graph neuron %s for graph %d not found",gr[j].gneur,j+1);
  78.        gotoxy(1,2);
  79.        cprintf("Press any key and N or G to correct");
  80.        bioskey(0);
  81.        clrline(1);
  82.        clrline(2);
  83.        erasecursor();
  84.        return(0);
  85.       }
  86.      else
  87.       gr[j].g = i;
  88.     }
  89.  
  90.   if (Iextgr >= 0 && gr[Iextgr].gtype == 1) /* set Iext neuron */
  91.    Iextind = gr[Iextgr].g;
  92.   else
  93.    Iextind = -1;
  94.  
  95.   for (i=0, ip=Iinta; i<ni; i++, ip++)
  96.    {
  97.     ip->H = 0;
  98.     ip->tint = 0;
  99.     if (ip->type == 1) /* if random, initialize tmax */
  100.      ip->tmax = (random((int)(1000.*ip->pL[1])-(int)(1000.*ip->pL[0])+1)
  101.               +(int)(1000.*ip->pL[0]))/1000.;
  102.    }
  103.  
  104.   /* initialize leg angles, foot states, and foot positions */
  105.   legang[0] = legang[1] = legang[2] = 0.;
  106.   legang[3] = legang[4] = legang[5] = PI;
  107.   for (j=0; j<6; j++)
  108.    {
  109.     bug.foot[j] = 1; /* all down */
  110.     bug.footx[j] = bug.x + attl[j] * cos(bug.ang + attang[j])
  111.             + legl[j] * cos(bug.ang + legang[j]);
  112.     bug.footy[j] = bug.y + attl[j] * sin(bug.ang + attang[j])
  113.             + legl[j] * sin(bug.ang + legang[j]);
  114.    }
  115.  
  116.   /* initialize antenna and cercus ends */
  117.   for (j=0; j<2; j++)
  118.    {
  119.     bug.antx[j] = bug.x + antl * cos(bug.ang + antang[j]);
  120.     bug.anty[j] = bug.y + antl * sin(bug.ang + antang[j]);
  121.     bug.cercx[j] = bug.x + cercl * cos(bug.ang + cercang[j]);
  122.     bug.cercy[j] = bug.y + cercl * sin(bug.ang + cercang[j]);
  123.     edget[j] = 0.;
  124.    }
  125.   /* initialize mouth and odors */
  126.   odor[0] = odor[1] = mouthodor = 0.;
  127.   bug.mouthx = bug.x + hdtl * cos(bug.ang + hdtang);
  128.   bug.mouthy = bug.y + hdtl * sin(bug.ang + hdtang);
  129.   for (j=0; j<nfood; j++)
  130.    { /* calculate odors for each antenna & mouth */
  131.     for (k=0; k<2; k++)
  132.      {
  133.       r = bug.antx[k] - foodx[j];
  134.       t = bug.anty[k] - foody[j];
  135.       odor[k] += foodsize[j]/(r*r + t*t);
  136.      }
  137.     r = bug.mouthx - foodx[j];
  138.     t = bug.mouthy - foody[j];
  139.     mouthodor += foodsize[j]/(r*r + t*t);
  140.     foodr[j] = sqrt(foodsize[j]/PI);
  141.     ofoodr[j] = foodr[j];
  142.    }
  143.   bug.mouthst = 0; /* initialize mouth state to closed */
  144.   mouth = 0;
  145.  
  146.   for (i=0, ncp=nsc; i<nn; i++, ncp++)
  147.    {
  148.     ncp->V = (ns+i)->Vt;
  149.     ncp->F = 0.;
  150.     ncp->Vlast = ncp->V;
  151.     ncp->I = 0.;
  152.    }
  153.  
  154.   graph();
  155.   setcolor(7);
  156.   for (j=0; j<nfood; j++)
  157.    {
  158.     r = sqrt(foodsize[j]/PI);
  159.     circle(foodx[j]/2, AspR * foody[j]/2, r);
  160.    }
  161.   obug = bug;
  162.   drawbug(bug,15);
  163.   setviewport(0,0,639,349,0);
  164.   t = 0;
  165.   step = 0;
  166.   while (TRUE)
  167.    {
  168. getkey:
  169.     /* wait for key */
  170.     i = bioskey(0);
  171.     asc = toupper(i & 0xff);
  172.     if (asc != 0)
  173.      {
  174.       if (asc == ESC)
  175.        {
  176.         if (recnum != 0)
  177.          {
  178. stoprec:  fclose(recfile);
  179.       setcolor(3);
  180.       bar(226,336,370,348);
  181.       outtextxy(226,336,"Menu  Tab:ChgPlots");
  182.       recnum = 0;
  183.       goto getkey;
  184.          }
  185.         else
  186.          return(0);
  187.        }
  188.       if (asc == CR)
  189.        {
  190.     i = multstep(&step,&t,recfile,&savIext); /* run indefinitely */
  191.     if (i)
  192.      {
  193.       if (recnum != 0)
  194.        goto stoprec;
  195.       else
  196.        return(0);
  197.      }
  198.        }
  199.       else
  200.        if (asc == SPACE)
  201.         {
  202.          if (!singstep(&step,&t,recfile,&savIext)) /* run one step */
  203.       singstep(&step,&t,recfile,&savIext);
  204.         }
  205.        else
  206.         if (asc == 'D')
  207.          {
  208.           if (!recnum)
  209.            {
  210.         drawmult *= 2;
  211.         if (drawmult > 64)
  212.          drawmult = 2;
  213.         setcolor(3);
  214.         bar(408,336,425,348);
  215.         sprintf(str,"%d ",drawmult);
  216.         outtextxy(408,336,str);
  217.            }
  218.          }
  219.         else
  220.          if (asc >= '0' && asc <= '9')
  221.       {
  222.        p = str;
  223.        while (asc != CR)
  224.         {
  225.          *p = asc;
  226.          p++;
  227.          asc = bioskey(0) & 0xff;
  228.         }
  229.        *p = 0;
  230.        if (str[0] >= '0' && str[0] <= '5' && str[1] == '=')
  231.         {
  232.          j = str[0] - '1';
  233.          setcolor(3);
  234.          if (j < 0)
  235.           {
  236.            bar(455,348,629,336);
  237.            Iextgr = -1;
  238.            Iextind = -1;
  239.           }
  240.          else
  241.           if (gr[j].gtype == 1)
  242.            {
  243.             f = atof(str+2);
  244.             if (fabs(f) < 100.)
  245.              {
  246.           Iextgr = j;
  247.           Iextind = gr[j].g;
  248.           Iextint = f;
  249.           bar(455,348,629,336);
  250.           settextjustify(2,2);
  251.           sprintf(str,"Iext(%s)[nA]:%-.1f",gr[Iextgr].gneur,Iextint);
  252.           outtextxy(629,336,str);
  253.           settextjustify(0,2);
  254.                   savIext = TRUE;
  255.              }
  256.            }
  257.             }
  258.       }
  259.          else
  260.       if (asc == TAB)
  261.            if (!recnum)
  262.         { /* change plot type */
  263.          bar(480,1,638,329); /* first blank entire block */
  264.          plottype = !plottype;
  265.          if (!plottype)
  266.           graphsetup();
  267.          else
  268.           activesetup();
  269.         }
  270.      }
  271.     else
  272.      {
  273.       ext = i>>8;
  274.       if (ext >= F1 && ext <= F10)
  275.        { /* start recording */
  276.     if ((step%4) != 0) /* if not where bug should be drawn, get there */
  277.      {
  278.       singstep(&step,&t,recfile,&savIext);
  279.       singstep(&step,&t,recfile,&savIext);
  280.      }
  281.     if (recnum != 0)
  282.      fclose(recfile);
  283.     recnum = ext - F1 + 1;
  284.     if (recpath[0] == 0)
  285.      sprintf(str,"ns%d.sav",recnum);
  286.     else
  287.      sprintf(str,"%s\\ns%d.sav\0",recpath,recnum);
  288.     recfile = fopen(str,"wb");
  289.     if (recfile == NULL)
  290.      {
  291.       recnum = 0;
  292.       return(1); /* return 1 to indicate recording path bad */
  293.      }
  294.     else
  295.      {
  296.       setcolor(3);
  297.       bar(226,336,370,348);
  298.       sprintf(str,"Stop  RECORDING:%d",recnum);
  299.       outtextxy(226,336,str);
  300.       if (drawmult > 2)
  301.        {
  302.         drawmult = 2; /* reset drawmult to 2 for recording */
  303.         setcolor(3);
  304.         bar(400,336,417,348);
  305.         sprintf(str,"%d ",drawmult);
  306.         outtextxy(400,336,str);
  307.        }
  308.       if (plottype)
  309.        { /* if on neuron activity plots, switch back to graphs */
  310.         bar(480,1,638,329); /* first blank entire block */
  311.         plottype = 0;
  312.         graphsetup();
  313.        }
  314.  
  315.       /* write code and version to file */
  316.       i = PLAYBUG;
  317.       fwrite(&i,2,1,recfile);
  318.       i = VERSION;
  319.       fwrite(&i,2,1,recfile);
  320.  
  321.       /* write initial data to file */
  322.       for (i=0; i<NFOOD; i++)
  323.        rin.foodr[i] = 0;
  324.       for (i=0; i<nfood; i++)
  325.        {
  326.         rin.foodr[i] = foodr[i];
  327.         rin.foodx[i] = foodx[i]/2;
  328.         rin.foody[i] = (AspR * foody[i])/2;
  329.        }
  330.       rin.nblock = nblock;
  331.       for (i=0; i<nblock; i++)
  332.        {
  333.         rin.blockx[i] = blockx[i]/2;
  334.         rin.blocky[i] = (AspR * blocky[i])/2;
  335.        }
  336.       fwrite(&rin.foodr[0],sizeof(struct recinit),1,recfile);
  337.  
  338.       /* write graph data to file */
  339.       for (i=0; i<5; i++)
  340.        {
  341.         rgr[i].gtype = gr[i].gtype;
  342.         strcpy(rgr[i].gneur,gr[i].gneur);
  343.         rgr[i].gvar = gr[i].gvar;
  344.         rgr[i].gscale = rgr[i].gscale;
  345.        }
  346.       fwrite(rgr,sizeof(struct recgraph),5,recfile);
  347.  
  348.       reccnt = 0; /* initialize records */
  349.       for (i=0; i<NREC; i++)
  350.        {
  351.         rc[i].footst = 0;
  352.         rc[i].mouthfood = 0;
  353.        }
  354.       if (Iextgr >= 0 && gr[Iextgr].gtype == 1)
  355.        savIext = TRUE;
  356.       else
  357.        savIext = FALSE;
  358.      }
  359.        }
  360.      }
  361.    }
  362.  }
  363.  
  364. int near multstep(int *step,double *t,FILE *recfile,int *savIext)
  365.  {
  366.   int i;
  367.   enum asc_val asc;
  368.  
  369.   while (TRUE)
  370.    {
  371.     if (singstep(step,t,recfile,savIext))
  372.      return(FALSE);
  373.     i = bioskey(1);
  374.     if (i)
  375.      {
  376.       bioskey(0);
  377.       asc = i & 0xff;
  378.       if (asc == ESC)
  379.        return(TRUE);
  380.       if (asc == SPACE)
  381.        return(FALSE);
  382.      }
  383.    }
  384.  }
  385.  
  386. int near singstep(int *step,double *t,FILE *recfile,int *savIext)
  387.  {
  388.   int y,j,i,k;
  389.   double s,v;
  390.   char str[6],c;
  391.   struct neuron *np;
  392.   struct neurcalc *ncp;
  393.  
  394.   if (update())
  395.    return(1);
  396.   (*step)++;
  397.   if (!(*step%2))
  398.    { /* graph */
  399.     if (!plottype)
  400.      {
  401.       if (recnum && (*step%(2*drawmult)))
  402.        rc[reccnt].graphx = (*t)*100; /* record x plot for graph */
  403.       for (j=0; j<5; j++) /* plot graph points */
  404.        {
  405.         if (gr[j].gtype == 1)
  406.      { /* variable */
  407.       if (gr[j].gvar == 1)
  408.        { /* F, 0 to 1 */
  409.         y = 52 - (int)(50.*(nsc+gr[j].g)->F);
  410.        }
  411.       else
  412.        {
  413.         if (gr[j].gvar == 0)
  414.          v = (nsc+gr[j].g)->V * 1e3;
  415.         else
  416.          v = (nsc+gr[j].g)->I * 1e9;
  417.         y = 28 - (int)(25*v/gr[j].gscale);
  418.        }
  419.       if (y < 3)
  420.        y = 3;
  421.       else
  422.        if (y > 52)
  423.         y = 52;
  424.       putpixel(538 + (int)(100.*(*t)+.5),y + 66*j,15);
  425.       if (recnum)
  426.        { /* record y plot for graph */
  427.         if ((*step%(2*drawmult)))
  428.          rc[reccnt].graphy1[j] = y;
  429.         else
  430.          rc[reccnt].graphy2[j] = y;
  431.            }
  432.      }
  433.         else
  434.      if (gr[j].gtype == 2)
  435.       { /* step */
  436.        for (i=0; i<6; i++)
  437.         {
  438.          if (!bug.foot[i])
  439.           putpixel(538 + (int)(100.*(*t)+.5),49-i*9+66*j,15);
  440.         }
  441.       }
  442.        }
  443.      }
  444.     else
  445.      { /* plot neuron activity */
  446.       i = 0;
  447.       k = 0;
  448.       for (j=0, np=ns, ncp=nsc; j<nn; j++,np++,ncp++)
  449.        {
  450.     if (np->name[0] != 0) /* if defined */
  451.      {
  452.       if (ncp->F == 0) /* set color depending on frequency */
  453.        setcolor(1);
  454.       else
  455.        if (ncp->F < .33)
  456.         setcolor(3);
  457.        else
  458.         if (ncp->F < .66)
  459.          setcolor(5);
  460.         else
  461.          setcolor(7);
  462.       outtextxy(483 + i*52,1 + k*8,np->name);
  463.       if (k < 39)
  464.        k++;
  465.       else
  466.        {
  467.         if (i < 2)
  468.          {
  469.           k = 0;
  470.           i++;
  471.          }
  472.         else
  473.          break;
  474.        }
  475.      }
  476.        }
  477.      }
  478.     if (!(*step%(2*drawmult)))
  479.      { /* draw bug */
  480.       setviewport(1,1,478,329,1);
  481.       for (j=0; j<nfood; j++)
  482.        {
  483.     if (ofoodr[j] != foodr[j])
  484.      y = 4;
  485.     else
  486.      y = 1;
  487.     if (mouth || y == 4)
  488.      {
  489.       setcolor(0);
  490.       circle(foodx[j]/2, AspR * foody[j]/2, ofoodr[j]);
  491.       ofoodr[j] = foodr[j];
  492.       setcolor(7);
  493.       circle(foodx[j]/2, AspR * foody[j]/2, foodr[j]);
  494.       if (recnum)
  495.        { /* note to redraw or reduce circle */
  496.         c = (1 << (j+y));
  497.         rc[reccnt].mouthfood = rc[reccnt].mouthfood | c;
  498.        }
  499.      }
  500.        }
  501.       setviewport(1,1,478,329,0);
  502.       showbug(obug,0);
  503.       obug = bug;
  504.       drawbug(bug,15);
  505.       setviewport(0,0,639,349,0);
  506.       bar(430,336,454,348);
  507.       sprintf(str,"%d",(int)energy);
  508.       setcolor(3);
  509.       outtextxy(430,336,str);
  510.       setcolor(7);
  511.       if (recnum)
  512.        {
  513.     if (*savIext)
  514.      {
  515.       strcpy(rc[reccnt].Iextname,gr[Iextgr].gneur);
  516.       rc[reccnt].Iext = Iextint * 10. + .5 * fsgn(Iextint);
  517.       *savIext = FALSE;
  518.      }
  519.     else
  520.      rc[reccnt].Iextname[0] = 0;
  521.     reccnt++;
  522.     if (reccnt == NREC)
  523.      { /* write to file */
  524.       if (fwrite(rc,sizeof(struct recbug),NREC,recfile) != NREC)
  525.        { /* disk full - close file & stop recording */
  526. stoprec:    fclose(recfile);
  527.         recnum = 0;
  528.         setcolor(3);
  529.         bar(226,336,370,348);
  530.         outtextxy(226,336,"Menu  Tab:ChgPlots");
  531.         return(TRUE);
  532.        }
  533.       reccnt = 0;
  534.       for (i=0; i<NREC; i++)
  535.        {
  536.         rc[i].footst = 0;
  537.         rc[i].mouthfood = 0;
  538.        }
  539.      }
  540.        }
  541.      }
  542.     if (!(*step%200) && !plottype)
  543.      { /* reset graphs */
  544.       *t = -DT;
  545.       for (j=0; j<5; j++)
  546.        {
  547.     if (gr[j].gtype)
  548.      {
  549.       bar(538,3+j*66,638,52+j*66);
  550.       setcolor(3);
  551.       line(547,3+j*66,547,55+j*66); /*graph x-axis ticks */
  552.       line(557,3+j*66,557,55+j*66);
  553.       line(567,3+j*66,567,55+j*66);
  554.       line(577,3+j*66,577,55+j*66);
  555.       line(587,3+j*66,587,56+j*66);
  556.       line(597,3+j*66,597,55+j*66);
  557.       line(607,3+j*66,607,55+j*66);
  558.       line(617,3+j*66,617,55+j*66);
  559.       line(627,3+j*66,627,55+j*66);
  560.       line(637,3+j*66,637,56+j*66);
  561.       setcolor(15);
  562.      }
  563.        }
  564.      }
  565.    }
  566.   *t += DT;
  567.   return(0);
  568.  }
  569.  
  570.