Exemplo n.º 1
0
int findElectrodeParameters(int rank,Parameter *D,LoadList *LL,char *input,char *block)
{
  int nodes,i,fail=0;
  char str[100],name[100];
  int FindParameters();

  if(FindParameters(block,rank,"nodes",input,str)) LL->nodes=atoi(str);
  else  LL->nodes=0;
  if(LL->nodes)
  {
    if(FindParameters(block,rank,"potential",input,str)) LL->pot=atof(str);
    else  {
      printf("in [%s], potential=?  [electric:eV, magnetic:flux]\n",block);
      fail=1;
    }
    LL->nodes+=1;
    LL->points=(float **)malloc(LL->nodes*sizeof(float *));
    for(i=0; i<LL->nodes; i++)
      LL->points[i]=(float *)malloc(3*sizeof(float ));

    for(i=0; i<LL->nodes-1; i++)
    {
      sprintf(name,"x%d",i);
      if(FindParameters(block,rank,name,input,str))
        LL->points[i][0]=atof(str);
      else  {
        printf("in [%s], x%d=?  \n",block,i);
        fail=1;
      }
      sprintf(name,"y%d",i);
      if(FindParameters(block,rank,name,input,str))
        LL->points[i][1]=atof(str);
      else  {
        printf("in [%s], y%d=?  \n",block,i);
        fail=1;
      }
    }
    LL->points[LL->nodes-1][0]=LL->points[0][0];
    LL->points[LL->nodes-1][1]=LL->points[0][1];
  }

  if(fail==1)
    exit(0);
 
  return LL->nodes;
}
Exemplo n.º 2
0
void parameterSetting(Parameter *D,char *input)
{
  int fail=0,i,j,k,rank;
  char str[100];
  int whatCalType();
  int findElectrodeParameters();
//   int myrank, nTasks;
//   MPI_Status status;

//   MPI_Comm_size(MPI_COMM_WORLD, &nTasks);     
//   MPI_Comm_rank(MPI_COMM_WORLD, &myrank);     
  LoadList *LL,*New;
   FILE *out;


   //initially
   if(FindParameters("Parameter",1,"dimension",input,str)) D->dimension=atoi(str);
   else  {
     printf("in [Parameter], dimension=?  (1:1D, 2:2D, 3:3D)\n");
     fail=1;
   }
   if(FindParameters("Parameter",1,"minX",input,str)) D->minX=atof(str);
   else  {
     printf("in [Parameter], minX=?  [SI]\n");
     fail=1;
   }
   if(FindParameters("Parameter",1,"maxX",input,str)) D->maxX=atof(str);
   else  {
     printf("in [Parameter], maxX=?  [SI]\n");
     fail=1;
   }
   if(FindParameters("Parameter",1,"minY",input,str)) D->minY=atof(str);
   else  {
     printf("in [Parameter], minY=?  [SI]\n");
     fail=1;
   }
   if(FindParameters("Parameter",1,"maxY",input,str)) D->maxY=atof(str);
   else  {
     printf("in [Parameter], maxY=?  [SI]\n");
     fail=1;
   }
   if(FindParameters("Parameter",1,"nx",input,str)) D->nx=atoi(str);
   else  {
     printf("in [Parameter], nx=?  \n");
     fail=1;
   }
   if(FindParameters("Parameter",1,"ny",input,str)) D->ny=atoi(str);
   else  {
     printf("in [Parameter], ny=?  \n");
     fail=1;
   }
   D->dx=(D->maxX-D->minX)/((float)D->nx);
   D->dy=(D->maxY-D->minY)/((float)D->ny);

   //Electrode
   D->EloadList=(LoadList *)malloc(sizeof(LoadList));
   D->EloadList->next=NULL;
   LL=D->EloadList;
   rank=1;   
   sprintf(str,"Electrode");
   while(findElectrodeParameters(rank,D,LL,input,str))
   {
     printf("rank=%d, potential=%g\n",rank,LL->pot);
     New=(LoadList *)malloc(sizeof(LoadList));
     New->next=NULL;
     LL->next=New;
     LL=LL->next;
     rank++;
   }

   if(fail==1)
     exit(0);


}
Exemplo n.º 3
0
void parameterSetting(Domain *D,External *Ext, char *input)
{
   int FindParameters();
   int findLoadParameters();
   int findLaserParameters();
   float minX,maxX,minY,maxY,positionX,positionY,factor,pMinX,pMaxX,pPosition;
   float normalB,normalE,E1,E2,E3,B1,B2,B3,dyoverdx;
   char str[100],name[100];
   int rank,minT,maxT,tmpInt,fail=0,cnt;
   int i,j,n,numProbeX,numProbeY,probeType;
   float lambda,tmpFloat,probeDx,probeDy,maxProbeX,minProbeX,maxProbeY,minProbeY;
   LoadList *LL, *New;
   LaserList *L, *LNew;

   //Field Type
   if(FindParameters("Domain",1,"FieldType",input,str)) D->fieldType=atoi(str);
   else  {
      printf("in [Domain], FieldType=?  (1:DSX, 12:DSXY, 123:DSXYZ)\n");
      fail=1;
   }
   //Current Type
   if(FindParameters("Domain",1,"CurrentType",input,str)) D->currentType=atoi(str);
   else  {
      printf("in [Domain], CurrentType=?  (1:1st, 2:2nd, 3:3rd Order)\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"InterpolationType",input,str)) D->interpolationType=atoi(str);
   else 
      D->interpolationType=1;
   if(D->interpolationType==1)  { D->numShareUp=1; D->numShareDn=1; }
   else if(D->interpolationType==2)  { D->numShareUp=2; D->numShareDn=3; }


   //Boost frame
   if(FindParameters("Domain",1,"boostGamma",input,str)) D->gamma=atof(str);
   else D->gamma=1;
   if(FindParameters("Domain",1,"boostSave",input,str)) D->boostSave=atoi(str);
   else D->boostSave=0;
   if(D->gamma>1)   D->boostOn=1;
   else             D->boostOn=0;
   D->beta=sqrt(1-1.0/D->gamma/D->gamma);



   //Domain parameter setting
   if(FindParameters("Domain",1,"maxStep",input,str)) D->maxStep=atoi(str);
   else  {
      printf("Domain maxStep must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"saveStep",input,str)) D->saveStep=atoi(str);
   else  {
      printf("Domain saveStep must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"saveStart",input,str)) D->saveStart=atoi(str);
   else  {
      printf("Domain saveStart must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"dumpSave",input,str)) D->dumpSave=atoi(str);
   else  {
      printf("in [Domain], dumpSave=?  [0:off, 1:on]\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"dumpStart",input,name)) D->dumpStart=atoi(name);
   else  D->dumpStart=D->saveStart;
   if(FindParameters("Domain",1,"dumpInter",input,name)) D->dumpInter=atoi(name);
   else  D->dumpInter=0;
   if(FindParameters("Domain",1,"fieldSave",input,name)) D->fieldSave=atoi(name);
   else  D->fieldSave=1;
   if(FindParameters("Domain",1,"particleSave",input,name)) D->particleSave=atoi(name);
   else  D->particleSave=1;
   if(FindParameters("Domain",1,"rhoSave",input,name)) D->rhoSave=atoi(name);
   else  D->rhoSave=1;

   if(FindParameters("Domain",1,"minX",input,str)) 
   {
      D->minX=atof(str);
      D->minX*=D->gamma*(1+D->beta);
   }
   else  {
      printf("minX value must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"maxX",input,str)) 
   {
      maxX=atof(str);
      maxX*=D->gamma*(1+D->beta);
   }
   else  {
      printf("maxX value must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"minY",input,str)) 
      D->minY=atof(str);
   else  {
      printf("minY value must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"maxY",input,str)) 
      maxY=atof(str);
   else  {
      printf("maxY value must be defined.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"dy_over_dx",input,str)) 
      dyoverdx=atof(str);
   else  {
      printf("in [Domain], dy_over_dx=?  [dy/dx]\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"divisionDirection",input,str)) D->divDirection=atof(str);
   else  {
      printf("in [Domain], divisionDirection=? How many cores do you use?\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"moving",input,str)) D->moving=atoi(str);
   else  {
      printf("moving must be defined.\n");
      printf("1:moving domain on,  0:moving domain off\n");
      fail=1;
   }   
   if(FindParameters("Domain",1,"lambda",input,str)) 
   {
      D->lambda=atof(str);
      D->lambda*=D->gamma*(1+D->beta);
   }
   else  {
      printf("in [Domain], lambda=? [m]\n");
      printf("basic parameter for dx, dt.\n");
      fail=1;
   }
   if(FindParameters("Domain",1,"divisionLambda",input,str)) 
   {
      D->divisionLambda=atof(str);
   }
   else  {
      printf("In [Domain], divisionLambda=? [number of devided wavelength]\n");
      fail=1;
   }

   //pml
   if(FindParameters("Domain",1,"pmlOn",input,str)) D->pmlOn=atoi(str);
   else  {
      printf("in [Domain], pmlOn=? \n");
      fail=1;
   }
   if(D->pmlOn==1)
   {
     if(FindParameters("Domain",1,"pmlCell",input,str)) D->pmlCell=atoi(str);
     else  {
        printf("in [Domain], pmlCell=? [ea]\n");
        fail=1;
     }
   }


   //External field parameter setting
   if(FindParameters("External",1,"E1",input,str)) E1=atof(str);
   else  {
      printf("in [External], E1=? [V/m]\n");
      fail=1;
   }
   if(FindParameters("External",1,"E2",input,str)) E2=atof(str);
   else  {
      printf("in [External], E2=? [V/m]\n");
      fail=1;
   }
   if(FindParameters("External",1,"E3",input,str)) E3=atof(str);
   else  {
      printf("in [External], E3=? [V/m]\n");
      fail=1;
   }
   if(FindParameters("External",1,"B1",input,str)) B1=atof(str);
   else  {
      printf("in [External], B1=? [Tesla]\n");
      fail=1;
   }
   if(FindParameters("External",1,"B2",input,str)) B2=atof(str);
   else  {
      printf("in [External], B2=? [Tesla]\n");
      fail=1;
   }
   if(FindParameters("External",1,"B3",input,str)) B3=atof(str);
   else  {
      printf("in [External], B3=? [Tesla]\n");
      fail=1;
   }


   //additional Domain parameters  
   D->dx=1.0/D->divisionLambda;
   D->dt=D->dx;   
   D->dy=D->dx*dyoverdx;
/*
   if(D->gamma>1) 
   {
     D->dy=D->dy*D->divisionLambda*0.5/dyoverdx/D->gamma/(1.0+D->beta);
     D->dx=D->dy*0.5;
     printf("labframe, dx=%g, dy=%g\n",D->dx,D->dy);
   }
*/

   tmpFloat=D->dx/(D->gamma*(1+D->beta));
   D->dy=tmpFloat*dyoverdx;
   printf("dx=%g, dy=%g\n",D->dx,D->dy);
   if(D->dy<=D->dx*1.5)   {
      printf("dyOverDx is too low. It must be over than %g.\n", D->gamma*(1+D->beta)*1.5);
      fail=1;
   }   

   D->nx=((int)((maxX-D->minX)/D->lambda/D->dx));
   D->ny=((int)((maxY-D->minY)/D->lambda/D->dy));
   D->omega=2*pi*velocityC/D->lambda;
   if(D->boostOn==1)   {
      D->minXSub=-D->nx;
      D->minYSub=0;
   }
   else   {
      D->minXSub=0;
      D->minYSub=(int)(D->minY/D->lambda/D->dy);
   }

   //Probe parameter
   if(FindParameters("Probe",1,"probeType",input,str)) probeType=atoi(str);
   else probeType=0;
   D->probeNum=0;

   if(probeType==0)
   {
     if(FindParameters("Probe",1,"probeNum",input,str)) D->probeNum=atoi(str);
     else  {
       printf("in [Probe], probeNum=?  [ea]\n");
       fail=1;
     }
     if(D->probeNum>0)
     {
       D->probeX=(int *)malloc(D->probeNum*sizeof(int));
       D->probeY=(int *)malloc(D->probeNum*sizeof(int));
       for(i=0; i<D->probeNum; i++)
       {
         sprintf(name,"probeX%d",i);
         if(FindParameters("Probe",1,name,input,str))   
           D->probeX[i]=((int)(atof(str)/D->lambda/D->dx));      
         else  {
           printf("in [Probe], probeX%d=?\n",i);
           fail=1;
         }
         sprintf(name,"probeY%d",i);
         if(FindParameters("Probe",1,name,input,str))      
           D->probeY[i]=((int)(atof(str)/D->lambda/D->dy));      
         else  {
           printf("in [Probe], probeY%d=?\n",i);
           fail=1;
         }
       }
     }  
   }
   else if(probeType==1)
   {
     if(FindParameters("Probe",1,"minProbeX",input,str)) minProbeX=atof(str);
     else  {
       printf("in [Probe], minProbeX=? [m]\n");
       fail=1;
     }
     if(FindParameters("Probe",1,"maxProbeX",input,str)) maxProbeX=atof(str);
     else  {
       printf("in [Probe], maxProbeX=? [m]\n");
       fail=1;
     }
     if(FindParameters("Probe",1,"numProbeX",input,str)) numProbeX=atoi(str);
     else 
       numProbeX=1;
     if(FindParameters("Probe",1,"minProbeY",input,str)) minProbeY=atof(str);
     else  {
       printf("in [Probe], minProbeY=? [m]\n");
       fail=1;
     }
     if(FindParameters("Probe",1,"maxProbeY",input,str)) maxProbeY=atof(str);
     else  {
       printf("in [Probe], maxProbeY=? [m]\n");
       fail=1;
     }
     if(FindParameters("Probe",1,"numProbeY",input,str)) numProbeY=atoi(str);
     else 
       numProbeY=1;
     if(numProbeX==0 && numProbeY==0)  {
       printf("in [Probe], it must be that numProbeX or numProbeY > 0 !!\n");
       fail=1;
     }
           
     probeDx=(maxProbeX-minProbeX)/((float)numProbeX);
     probeDy=(maxProbeY-minProbeY)/((float)numProbeY);
     D->probeNum=numProbeX*numProbeY;
     D->probeX=(int *)malloc(D->probeNum*sizeof(int));
     D->probeY=(int *)malloc(D->probeNum*sizeof(int));

     n=0;
     for(i=0; i<numProbeX; i++)
       for(j=0; j<numProbeY; j++)
       {       
         tmpFloat=minProbeX+i*probeDx;
         D->probeX[n]=((int)(tmpFloat/D->lambda/D->dx));      
         tmpFloat=minProbeY+j*probeDy;
         D->probeY[n]=((int)(tmpFloat/D->lambda/D->dy));     
         n++;
       } 
   }			//End of else if(probeType=2)
    
   //additional Boost parameters
   factor=D->gamma*(1+D->beta);
   D->minT=(int)(D->maxStep/factor/factor); 	//boost frame iteration
   D->maxT=(int)(D->gamma*(D->maxStep/factor+D->beta*D->nx/factor-factor*D->beta*D->minT));	//boost frame iteration
printf("maxT=%d\n",D->maxT);


   //additional external field parameters
   normalB=eMass*D->omega/(-eCharge);
   normalE=normalB*velocityC;
   Ext->Pr=(E2/normalE+B3/normalB)*0.5;
   Ext->Pl=(E2/normalE-B3/normalB)*0.5;
   Ext->E1=E1/normalE;
   Ext->Sr=(E3/normalE-B2/normalB)*0.5;
   Ext->Sl=(E3/normalE+B2/normalB)*0.5;
   Ext->B1=B1/normalB;

   //Laser parameter setting
   D->laserList = (LaserList *)malloc(sizeof(LaserList));
   D->laserList->next = NULL;
   L = D->laserList;
   rank = 1;
   while(findLaserParameters(rank,L,D,input)) 
   {
      LNew = (LaserList *)malloc(sizeof(LaserList));
      LNew->next = NULL;
      L->next=LNew;
      L=L->next;
      rank ++;
   }
   D->nLaser = rank-1;

   //Plasma parameter setting
   D->loadList = (LoadList *)malloc(sizeof(LoadList));
   D->loadList->next = NULL;
   LL = D->loadList;
   rank = 1;
   while(findLoadParameters(rank, LL, D,input)) 
   {
      New = (LoadList *)malloc(sizeof(LoadList));
      New->next = NULL;
      LL->next=New;
      LL=LL->next;
      rank ++;
   }
   D->nSpecies = rank-1;

   if(fail==1)
      exit(0);

}
Exemplo n.º 4
0
int findLoadParameters(int rank, LoadList *LL,Domain *D,char *input)
{
   int FindParameters();
   LoadList *New;
   int whatSpecies();
   int whatPlasmaType();
   double whatMass();
   float pointPosition,wp,pDt;
   int whatCharge();
   char name[100], str[100];
   int i,species,fail=0;

   if(FindParameters("Plasma",rank,"type",input,name)) 
      LL->type = whatPlasmaType(name);
   else LL->type=0;

   if(LL->type>0)
   {
      LL->species=species;
      if(FindParameters("Plasma",rank,"density",input,str)) 
      {
         LL->density=atof(str);
         LL->density*=D->gamma;
      }
      else  {
         printf("in [Plasma], density=? [m-3]\n");
         fail=1;
      }

/*    
      //testing optimal dx(divisionLambda) size
      wp=sqrt(LL->density*eCharge*eCharge/eMass/eps0);
      pDt=2*pi/wp;   
      pDt=pDt/(2*pi/D->omega)/20.0;
      if(D->dt>pDt)
      {
         printf("dt must be less then %g!\n",pDt);
         printf("So, divisionLambda>%g!\n",1/pDt);
         fail=1;
      }
*/

      if(FindParameters("Plasma",rank,"species",input,name)) 
         species = whatSpecies(name);
      else  species = 0;
      if(FindParameters("Plasma",rank,"numberInCell",input,str)) 
         LL->numberInCell=atoi(str);
      else  {
         printf("in [Plasma], numberInCell=? \n");
         fail=1;
      }
      if(FindParameters("Plasma",rank,"with_next_species",input,str)) 
         LL->withNextSpcs=atoi(str);
      else  {
         printf("in [Plasma], with_next_species=? (0:no, 1:yes)\n");
         fail=1;
      }
      if(FindParameters("Plasma",rank,"with_prev_species",input,str)) 
         LL->withPrevSpcs=atoi(str);
      else  {
         printf("in [Plasma], with_prev_species=? (0:no, 1:yes)\n");
         fail=1;
      }
      if(FindParameters("Plasma",rank,"startIndex",input,str)) 
         LL->index=atoi(str);
      else  {
         printf("in [Plasma], startIndex=? \n");
         printf("It is the starting particle index which may be 0 as default.\n");
         fail=1;
      }
      if(FindParameters("Plasma",rank,"Lnodes",input,str)) LL->lnodes=atoi(str);
      else  {
         printf("in [Plasma], Lnodes=?\n");
         printf("Each nodes indicates the point of plasma density changing.\n");
         fail=1;
      }
      if(FindParameters("Plasma",rank,"Tnodes",input,str)) LL->tnodes=atoi(str);
      else  {
         printf("in [Plasma], Tnodes=?\n");
         printf("Each nodes indicates the point of plasma density changing.\n");
         fail=1;
      }
   
      LL->lpoint = (float *)malloc(LL->lnodes*sizeof(float));
      LL->ln = (float *)malloc(LL->lnodes*sizeof(float));   
      for(i=0; i<LL->lnodes; i++)
      {
         sprintf(name,"X%d",i);
         if(FindParameters("Plasma",rank,name,input,str)) 
            LL->lpoint[i] = atof(str)/D->gamma/D->lambda/D->dx;
         else 
         { printf("X%d should be defined.\n",i);  fail=1; }

         sprintf(name,"Ln%d",i);
         if(FindParameters("Plasma",rank,name,input,str)) 
            LL->ln[i] = atof(str);
         else 
         { printf("Ln%d should be defined.\n",i);  fail=1; } 
      }
      LL->tpoint = (float *)malloc(LL->tnodes*sizeof(float));
      LL->tn = (float *)malloc(LL->tnodes*sizeof(float));   
      for(i=0; i<LL->tnodes; i++)
      {
         sprintf(name,"Y%d",i);
         if(FindParameters("Plasma",rank,name,input,str)) {
            LL->tpoint[i] = atof(str)/D->lambda/D->dy;
         }
         else 
         { printf("Y%d should be defined.\n",i);  fail=1; }

         sprintf(name,"Tn%d",i);
         if(FindParameters("Plasma",rank,name,input,str)) 
            LL->tn[i] = atof(str);
         else 
         { printf("Tn%d should be defined.\n",i);  fail=1; } 
      }

      if(FindParameters("Plasma",rank,"cx",input,str))  {
         LL->cx=atof(str)/D->lambda;
      }
      else   LL->cx=D->nx*D->dx; 
      if(FindParameters("Plasma",rank,"cy",input,str))  {
         LL->cy=atof(str)/D->lambda;
      }
      else   LL->cy=D->ny*0.5*D->dy;  

      if(FindParameters("Plasma",rank,"temperature",input,str))  
         LL->temperature=atof(str);
      else   LL->temperature=0.0;	

      LL->mass=whatMass(species);
      LL->charge=whatCharge(species);
      LL->criticalDensity=eps0*eMass*D->omega*D->omega/eCharge/eCharge;
      LL->superP=LL->density*D->lambda*D->dx*D->lambda*D->dy/LL->numberInCell;
      
   }	//end of if(species)

   if(fail==1)
      exit(0);

   return LL->type;
}
Exemplo n.º 5
0
int findLaserParameters(int rank, LaserList *L,Domain *D,char *input)
{
   int FindParameters();
   float positionX,positionY;
   char name[100], str[100];
   int fail=0,polarity;

   if(FindParameters("Laser",rank,"polarity",input,str)) polarity=atoi(str);
   else  polarity=0;

   if(polarity)
   {
     if(FindParameters("Laser",rank,"wavelength",input,str)) 
     {
        L->lambda=atof(str);
        L->lambda*=D->gamma*(1.0+D->beta);
     }
     else  L->lambda=D->lambda;
  
     if(FindParameters("Laser",rank,"a0",input,str)) 
        L->amplitude=atof(str);
     else  {
        printf("in [Laser], a0=??\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"rU",input,str)) L->rU=atof(str);
     else  {
        printf("in [Laser], rU=? [# of basic wavelength]\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"rD",input,str)) L->rD=atof(str);
     else  {
        printf("in [Laser], rD=? [# of basic wavelength]\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"loadPositionX",input,str)) positionX=atof(str);
     else  {
        printf("in [Laser], loadPositionX=?  [m]\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"loadPositionY",input,str)) positionY=atof(str);
     else  {
        printf("in [Laser], loadPositionY=?  [m]\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"beamWaist",input,str)) L->beamWaist=atof(str);
     else  {
        printf("in [Laser], beamWaist=?  [m]\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"focus",input,str)) L->focus=atof(str);
     else  {
        printf("in [Laser], focus=?  [m]\n");
        fail=1;
     }
     if(FindParameters("Laser",rank,"flat",input,str)) L->flat=atof(str);
     else  L->flat=0.0;
     if(FindParameters("Laser",rank,"direction",input,str)) L->direction=atoi(str);
     else  L->direction=1;

     //additional laser parameters
     L->polarity=polarity;
     L->omega=2*pi*velocityC/L->lambda;
     L->loadPointX=((int)(positionX/D->lambda/D->dx));   
     L->loadPointY=((int)(positionY/D->lambda/D->dy));   
     L->rayleighLength=pi/L->lambda*D->gamma*(1+D->beta)*L->beamWaist*L->beamWaist/D->lambda;
     L->beamWaist=L->beamWaist/D->lambda;
     L->focus=L->focus/D->lambda;
     if(fail==1)
        exit(0);
   }
   return polarity;
}
Exemplo n.º 6
0
bool FArchiveXML::LoadAnimationChannel(FCDObject* object, xmlNode* channelNode)
{ 
	FCDAnimationChannel* animationChannel = (FCDAnimationChannel*)object;
	FCDAnimationChannelData& data = FArchiveXML::documentLinkDataMap[animationChannel->GetDocument()].animationChannelData[animationChannel];

	bool status = true;

	// Read the channel-specific ID
	fm::string daeId = ReadNodeId(channelNode);
	fm::string samplerId = ReadNodeSource(channelNode);
	ReadNodeTargetProperty(channelNode, data.targetPointer, data.targetQualifier);

#ifdef DONT_DEFINE_THIS
	FCDAnimation* anim = animationChannel->GetParent();
	FCDExtra* extra = anim->GetExtra();
	extra->SetTransientFlag(); // Dont save this, its wasted whatever it is.
	FCDEType* type = extra->AddType("AnimTargets");
	FCDETechnique* teq = type->AddTechnique("TEMP");
	teq->AddChildNode("pointer")->SetContent(TO_FSTRING(data.targetPointer));
	teq->AddChildNode("pointer")->SetContent(TO_FSTRING(data.targetQualifier));
#endif
	


	xmlNode* samplerNode = FArchiveXML::FindChildByIdFCDAnimation(animationChannel->GetParent(), samplerId);
	if (samplerNode == NULL || !IsEquivalent(samplerNode->name, DAE_SAMPLER_ELEMENT))
	{
		FUError::Error(FUError::ERROR_LEVEL, FUError::ERROR_MISSING_ELEMENT, channelNode->line);
		return false;
	}

	// Find and process the sources
	xmlNode* inputSource = NULL, * outputSource = NULL, * inTangentSource = NULL, * outTangentSource = NULL, * tcbSource = NULL, * easeSource = NULL, * interpolationSource = NULL;
	xmlNodeList samplerInputNodes;
	fm::string inputDriver;
	FindChildrenByType(samplerNode, DAE_INPUT_ELEMENT, samplerInputNodes);
	for (size_t i = 0; i < samplerInputNodes.size(); ++i) // Don't use iterator here because we are possibly appending source nodes in the loop
	{
		xmlNode* inputNode = samplerInputNodes[i];
		fm::string sourceId = ReadNodeSource(inputNode);
		xmlNode* sourceNode = FArchiveXML::FindChildByIdFCDAnimation(animationChannel->GetParent(), sourceId);
		fm::string sourceSemantic = ReadNodeSemantic(inputNode);

		if (sourceSemantic == DAE_INPUT_ANIMATION_INPUT) inputSource = sourceNode;
		else if (sourceSemantic == DAE_OUTPUT_ANIMATION_INPUT) outputSource = sourceNode;
		else if (sourceSemantic == DAE_INTANGENT_ANIMATION_INPUT) inTangentSource = sourceNode;
		else if (sourceSemantic == DAE_OUTTANGENT_ANIMATION_INPUT) outTangentSource = sourceNode;
		else if (sourceSemantic == DAEFC_TCB_ANIMATION_INPUT) tcbSource = sourceNode;
		else if (sourceSemantic == DAEFC_EASE_INOUT_ANIMATION_INPUT) easeSource = sourceNode;
		else if (sourceSemantic == DAE_INTERPOLATION_ANIMATION_INPUT) interpolationSource = sourceNode;
		else if (sourceSemantic == DAEMAYA_DRIVER_INPUT) inputDriver = sourceId;
	}
	if (inputSource == NULL || outputSource == NULL)
	{
		FUError::Error(FUError::ERROR_LEVEL, FUError::ERROR_MISSING_INPUT, samplerNode->line);
		return false;
	}

	// Calculate the number of curves that in contained by this channel
	xmlNode* outputAccessor = FindTechniqueAccessor(outputSource);
	fm::string accessorStrideString = ReadNodeProperty(outputAccessor, DAE_STRIDE_ATTRIBUTE);
	uint32 curveCount = FUStringConversion::ToUInt32(accessorStrideString);
	if (curveCount == 0) curveCount = 1;

	// Create the animation curves
	for (uint32 i = 0; i < curveCount; ++i)
	{
		animationChannel->AddCurve();
	}

	// Read in the animation curves
	// The input keys and interpolations are shared by all the curves
	FloatList inputs;
    ReadSource(inputSource, inputs);
	size_t keyCount = inputs.size();
	if (keyCount == 0) return true; // Valid although very boring channel.

	UInt32List interpolations; interpolations.reserve(keyCount);
	ReadSourceInterpolation(interpolationSource, interpolations);
	if (interpolations.size() < keyCount)
	{
		// Not enough interpolation types provided, so append BEZIER as many times as needed.
		interpolations.insert(interpolations.end(), keyCount - interpolations.size(), FUDaeInterpolation::FromString(""));
	}

	// Read in the interleaved outputs as floats
	fm::vector<FloatList> tempFloatArrays;
	tempFloatArrays.resize(curveCount);
	fm::pvector<FloatList> outArrays(curveCount);
	for (uint32 i = 0; i < curveCount; ++i) outArrays[i] = &tempFloatArrays[i];
	ReadSourceInterleaved(outputSource, outArrays);
	for (uint32 i = 0; i < curveCount; ++i)
	{
		// Fill in the output array with zeroes, if it was not large enough.
		if (tempFloatArrays[i].size() < keyCount)
		{
			tempFloatArrays[i].insert(tempFloatArrays[i].end(), keyCount - tempFloatArrays[i].size(), 0.0f);
		}

		// Create all the keys, on the curves, according to the interpolation types.
		for (size_t j = 0; j < keyCount; ++j)
		{
			FCDAnimationKey* key = animationChannel->GetCurve(i)->AddKey((FUDaeInterpolation::Interpolation) interpolations[j]);
			key->input = inputs[j];
			key->output = tempFloatArrays[i][j];

			// Set the default values for Bezier/TCB interpolations.
			if (key->interpolation == FUDaeInterpolation::BEZIER)
			{
				FCDAnimationKeyBezier* bkey = (FCDAnimationKeyBezier*) key;
				float previousInput = (j == 0) ? inputs[j] - 1.0f : inputs[j-1];
				float nextInput = (j == keyCount - 1) ? inputs[j] + 1.0f : inputs[j+1];
				bkey->inTangent.x = (previousInput + 2.0f * bkey->input) / 3.0f;
				bkey->outTangent.x = (nextInput + 2.0f * bkey->input) / 3.0f;
				bkey->inTangent.y = bkey->outTangent.y = bkey->output;
			}
			else if (key->interpolation == FUDaeInterpolation::TCB)
			{
				FCDAnimationKeyTCB* tkey = (FCDAnimationKeyTCB*) key;
				tkey->tension = tkey->continuity = tkey->bias = 0.5f;
				tkey->easeIn = tkey->easeOut = 0.0f;
			}
		}
	}
	tempFloatArrays.clear();

	// Read in the interleaved in_tangent source.
	if (inTangentSource != NULL)
	{
		fm::vector<FMVector2List> tempVector2Arrays;
		tempVector2Arrays.resize(curveCount);
		fm::pvector<FMVector2List> arrays(curveCount);
		for (uint32 i = 0; i < curveCount; ++i) arrays[i] = &tempVector2Arrays[i];

		uint32 stride = ReadSourceInterleaved(inTangentSource, arrays);
		if (stride == curveCount)
		{
			// Backward compatibility with 1D tangents.
			// Remove the relativity from the 1D tangents and calculate the second-dimension.
			for (uint32 i = 0; i < curveCount; ++i)
			{
				FMVector2List& inTangents = tempVector2Arrays[i];
				FCDAnimationKey** keys = animationChannel->GetCurve(i)->GetKeys();
				size_t end = min(inTangents.size(), keyCount);
				for (size_t j = 0; j < end; ++j)
				{
					if (keys[j]->interpolation == FUDaeInterpolation::BEZIER)
					{
						FCDAnimationKeyBezier* bkey = (FCDAnimationKeyBezier*) keys[j];
						bkey->inTangent.y = bkey->output - inTangents[j].x;
					}
				}
			}
		}
		else if (stride == curveCount * 2)
		{
			// This is the typical, 2D tangent case.
			for (uint32 i = 0; i < curveCount; ++i)
			{
				FMVector2List& inTangents = tempVector2Arrays[i];
				FCDAnimationKey** keys = animationChannel->GetCurve(i)->GetKeys();
				size_t end = min(inTangents.size(), keyCount);
				for (size_t j = 0; j < end; ++j)
				{
					if (keys[j]->interpolation == FUDaeInterpolation::BEZIER)
					{
						FCDAnimationKeyBezier* bkey = (FCDAnimationKeyBezier*) keys[j];
						bkey->inTangent = inTangents[j];
					}
				}
			}
		}
	}

	// Read in the interleaved out_tangent source.
	if (outTangentSource != NULL)
	{
		fm::vector<FMVector2List> tempVector2Arrays;
		tempVector2Arrays.resize(curveCount);
		fm::pvector<FMVector2List> arrays(curveCount);
		for (uint32 i = 0; i < curveCount; ++i) arrays[i] = &tempVector2Arrays[i];

		uint32 stride = ReadSourceInterleaved(outTangentSource, arrays);
		if (stride == curveCount)
		{
			// Backward compatibility with 1D tangents.
			// Remove the relativity from the 1D tangents and calculate the second-dimension.
			for (uint32 i = 0; i < curveCount; ++i)
			{
				FMVector2List& outTangents = tempVector2Arrays[i];
				FCDAnimationKey** keys = animationChannel->GetCurve(i)->GetKeys();
				size_t end = min(outTangents.size(), keyCount);
				for (size_t j = 0; j < end; ++j)
				{
					if (keys[j]->interpolation == FUDaeInterpolation::BEZIER)
					{
						FCDAnimationKeyBezier* bkey = (FCDAnimationKeyBezier*) keys[j];
						bkey->outTangent.y = bkey->output + outTangents[j].x;
					}
				}
			}
		}
		else if (stride == curveCount * 2)
		{
			// This is the typical, 2D tangent case.
			for (uint32 i = 0; i < curveCount; ++i)
			{
				FMVector2List& outTangents = tempVector2Arrays[i];
				FCDAnimationKey** keys = animationChannel->GetCurve(i)->GetKeys();
				size_t end = min(outTangents.size(), keyCount);
				for (size_t j = 0; j < end; ++j)
				{
					if (keys[j]->interpolation == FUDaeInterpolation::BEZIER)
					{
						FCDAnimationKeyBezier* bkey = (FCDAnimationKeyBezier*) keys[j];
						bkey->outTangent = outTangents[j];
					}
				}
			}
		}
	}

	if (tcbSource != NULL)
	{
		//Process TCB parameters
		fm::vector<FMVector3List> tempVector3Arrays;
		tempVector3Arrays.resize(curveCount);
		fm::pvector<FMVector3List> arrays(curveCount);
		for (uint32 i = 0; i < curveCount; ++i) arrays[i] = &tempVector3Arrays[i];

		ReadSourceInterleaved(tcbSource, arrays);

		for (uint32 i = 0; i < curveCount; ++i)
		{
			FMVector3List& tcbs = tempVector3Arrays[i];
			FCDAnimationKey** keys = animationChannel->GetCurve(i)->GetKeys();
			size_t end = min(tcbs.size(), keyCount);
			for (size_t j = 0; j < end; ++j)
			{
				if (keys[j]->interpolation == FUDaeInterpolation::TCB)
				{
					FCDAnimationKeyTCB* tkey = (FCDAnimationKeyTCB*) keys[j];
					tkey->tension = tcbs[j].x;
					tkey->continuity = tcbs[j].y;
					tkey->bias = tcbs[j].z;
				}
			}
		}
	}

	if (easeSource != NULL)
	{
		//Process Ease-in and ease-out data
		fm::vector<FMVector2List> tempVector2Arrays;
		tempVector2Arrays.resize(curveCount);
		fm::pvector<FMVector2List> arrays(curveCount);
		for (uint32 i = 0; i < curveCount; ++i) arrays[i] = &tempVector2Arrays[i];

		ReadSourceInterleaved(easeSource, arrays);

		for (uint32 i = 0; i < curveCount; ++i)
		{
			FMVector2List& eases = tempVector2Arrays[i];
			FCDAnimationKey** keys = animationChannel->GetCurve(i)->GetKeys();
			size_t end = min(eases.size(), keyCount);
			for (size_t j = 0; j < end; ++j)
			{
				if (keys[j]->interpolation == FUDaeInterpolation::TCB)
				{
					FCDAnimationKeyTCB* tkey = (FCDAnimationKeyTCB*) keys[j];
					tkey->easeIn = eases[j].x;
					tkey->easeOut = eases[j].y;
				}
			}
		}
	}

	// Read in the pre/post-infinity type
	xmlNodeList mayaParameterNodes; StringList mayaParameterNames;
	xmlNode* mayaTechnique = FindTechnique(inputSource, DAEMAYA_MAYA_PROFILE);
	FindParameters(mayaTechnique, mayaParameterNames, mayaParameterNodes);
	size_t parameterCount = mayaParameterNodes.size();
	for (size_t i = 0; i < parameterCount; ++i)
	{
		xmlNode* parameterNode = mayaParameterNodes[i];
		const fm::string& paramName = mayaParameterNames[i];
		const char* content = ReadNodeContentDirect(parameterNode);

		if (paramName == DAEMAYA_PREINFINITY_PARAMETER)
		{
			size_t curveCount = animationChannel->GetCurveCount();
			for (size_t c = 0; c < curveCount; ++c)
			{
				animationChannel->GetCurve(c)->SetPreInfinity(FUDaeInfinity::FromString(content));
			}
		}
		else if (paramName == DAEMAYA_POSTINFINITY_PARAMETER)
		{
			size_t curveCount = animationChannel->GetCurveCount();
			for (size_t c = 0; c < curveCount; ++c)
			{
				animationChannel->GetCurve(c)->SetPostInfinity(FUDaeInfinity::FromString(content));
			}
		}
		else
		{
			// Look for driven-key input target
			if (paramName == DAE_INPUT_ELEMENT)
			{
				fm::string semantic = ReadNodeSemantic(parameterNode);
				if (semantic == DAEMAYA_DRIVER_INPUT)
				{
					inputDriver = ReadNodeSource(parameterNode);
				}
			}
		}
	}

	if (!inputDriver.empty())
	{
		const char* driverTarget = FUDaeParser::SkipPound(inputDriver);
		if (driverTarget != NULL)
		{
			fm::string driverQualifierValue;
			FUStringConversion::SplitTarget(driverTarget, data.driverPointer, driverQualifierValue);
			data.driverQualifier = FUStringConversion::ParseQualifier(driverQualifierValue);
			if (data.driverQualifier < 0) data.driverQualifier = 0;
		}
	}
	animationChannel->SetDirtyFlag();

	return status;
}