示例#1
0
文件: block.c 项目: oeo4b/krigmap
int 
main(int argc, char** argv)
{
  /**
   * fprintf(stdout) | ./block l d x y
   * option:
   *   -x [gpu, cpu]
   *   -m [lin, sph, exp, gau]
   *   -l [1..13]
   */
  if(argc<5) {
    fprintf(stderr, "Error: not enough arguments.\n");
    exit(1);
  }
  int level = atoi(argv[1]);
  int depth = atoi(argv[2]);
  int xblock = atoi(argv[3]);
  int yblock = atoi(argv[4]);

  grid g;
  features f;
  model m;
  spectrum s;

  /* Stdin -> features */
  readfeatures(&f);

  /* Estimate grid based on model */
  singleblock(&g, level, depth, xblock, yblock);
  readgrid(&g); 

  /* Predict using model */
  int i;
  if(f.n>0) {
    if(argc<11) {
      fprintf(stderr, "argc: %d\n", argc);
      for(i=0;i<argc;i++) {
        fprintf(stderr, "%s\n", argv[i]);
      }
      fprintf(stderr, "Error: not enough arguments.\n");
      exit(1);
    }
    m.type = atoi(argv[5]);
    m.nugget = atof(argv[6]);
    m.range = atof(argv[7]);
    m.sill = atof(argv[8]);
    m.a = atof(argv[9]);
    m.MSE = atof(argv[10]);

    predict(&g, &m, &f);
    heatspectrum(&s);
  }

  /* Write image */
  printppm(&g, &f, &s, f.n==0 ? 1 : 0);

  return 0;
}
示例#2
0
文件: orig_p3.c 项目: pbarrio/IEC-mod
int main()
{
  int i,j,k,l;
  memcount = 0;
  readgrid();
  printf("Grid reading finished, memcount = %ld\n",memcount);
  
  double tol = 1e-6; //0.1;
  double kappa = 0.0, beta = 0.5, omega = 1 - kappa/beta, alpha0 = 1-omega;
  double tin = 1000.0, tw = 0.0;
  double dh1 = 0.0, dh2 = 100.0;
  double sigma = 5.67e-8;

  buildcoeffs(alpha0,beta);
  printf("Built co-efficients, memcount = %ld\n",memcount);

  tb = (double*) malloc(sizeof(double)*nbcfaces);
  t = (double*) malloc(sizeof(double)*ncells);
  memcount += sizeof(double)*nbcfaces + sizeof(double)*ncells;
  //printf("Memcount = %ld\n",memcount);

  for( i = 0 ; i < ncells ; i++ )
    t[i] = 0.0;
  
  for( i = 0 ; i < nbcfaces ; i++){
    if( bcname[i] == BOTTOM ){
      int face_num = bf_to_f[i];
      if( xf[face_num] >= dh1 && xf[face_num] <= dh2 )
	tb[i] = tin;
      else
	tb[i] = tw;
    }
    else
      tb[i] = tw;
  }
  /* for( i = 0 ; i < nbcfaces ; i++ ) */
  /*   printf("%lf\n",tb[i]); */
  
  phic_old = malloc_2d(ncells,4);
  phic_new = malloc_2d(ncells,4);
  sc = malloc_2d(ncells,4);
  memcount += sizeof(double)*4*ncells*3 + sizeof(double*)*3*ncells;
  //printf("Memcount = %ld\n",memcount);
  for( i = 0 ; i < ncells ; i++ ){
    for( j = 0 ; j < 4  ;j++){
      phic_old[i][j] = 0.0;
      phic_new[i][j] = 0.0;
    }
  }
  
  sb = malloc_2d(nbcfaces,4);
  phib = malloc_2d(nbcfaces,4);
  memcount += sizeof(double)*4*nbcfaces*2 + sizeof(double*)*2*nbcfaces;
  //printf("Memcount = %ld\n",memcount);
  for( i = 0 ; i < nbcfaces ; i++ ){
    for( j = 0 ; j < 4 ; j++ )
      phib[i][j] = 0.0;
  }

  phiv = malloc_2d(nnodes,4);
  memcount += sizeof(double)*4*nnodes + sizeof(double*)*nnodes;
  //printf("Memcount = %ld\n",memcount);
  for( i = 0 ; i < nnodes ; i++ )
    for( j = 0 ; j < 4 ; j++ )
      phiv[i][j] = 0.0;
  
  dphi = malloc_2d(nfaces,4);
  memcount += sizeof(double)*4*nfaces + sizeof(double*)*nfaces;
  //printf("Memcount = %ld, dphi = %p\n",memcount,dphi);
  for( i = 0 ; i < nfaces ; i++ )
    for( j = 0 ; j < 4 ; j++ )
      dphi[i][j] = 0.0;

  double resid0,resid1,resid2,resid3;

  resid0 = tol * 2; 
  resid1 = tol * 2; 
  resid2 = tol * 2; 
  resid3 = tol * 2; 

  for( i = 0 ; i < nbcfaces ; i++ ){
    sb[i][0] = 4.0 * sigma * tb[i] * tb[i] * tb[i] * tb[i];
    sb[i][1] = 0.0;
    sb[i][2] = 4.0 * sigma * tb[i] * tb[i] * tb[i] * tb[i];
    sb[i][3] = 0.0;
  }

  for( i = 0 ; i < ncells ; i++ ){
    sc[i][0] = 0.0;
    sc[i][1] = 0.0;
    sc[i][2] = 0.0;
    sc[i][3] = -20.0 * alpha0 * sigma * t[i]*t[i]*t[i]*t[i] * beta * volcell[i];
  }

  int count = 0;
  double start_t = rtclock();
  while ( resid0 > tol || resid1 > tol || resid2 > tol || resid3 > tol ){
  /* while(count < 1 ){ */
    for(i = 0 ; i < nbcfaces ; i++ ){
      double new_phib[4];
      double lhs_matrix[4][4];
      int currf = bf_to_f[i];
      assert( currf >=0 && currf < nfaces);
      assert( bf_to_c[i] >=0 && bf_to_c[i] < nfaces);
      for( j = 0 ; j < 4 ; j++ )
    	new_phib[j] = sb[i][j];
      for( k = 0 ; k < 4 ; k++ )
    	for( l = 0 ; l < 4 ; l++ ){
    	  new_phib[k] += bc[i][4*k+l] * dphi[currf][l] + bb[i][4*k+l]*phic_new[bf_to_c[i]][l];
    	  lhs_matrix[k][l] = ba[i][4*k+l];
    	}
      inverse_multiply(new_phib,lhs_matrix);
      
      for( j = 0 ; j < 4 ; j++ )
    	phib[i][j] = new_phib[j];
    }

    for( i = 0 ; i < nnodes ; i++ )
      for( j = 0 ; j < 4 ; j++ )
    	phiv[i][j] = 0.0;
  
    for( i = 0 ; i < nbcfaces ; i++ ){
      int currf = bf_to_f[i];
      int n1 = lfv0[currf];
      int n2 = lfv1[currf];
      assert(currf >=0 && currf < nfaces );
      assert(n1 >=0 && n1 < nnodes );
      assert(n2 >=0 && n2 < nnodes );
      for( j = 0 ; j < 4 ; j++ ){
    	phiv[n1][j] += phib[i][j] * wbfv[i][0];
    	phiv[n2][j] += phib[i][j] * wbfv[i][1];
      }
    }

    for( i= 0 ; i < ncells ; i++ ){
      for( j = ia_cv[i] ; j < ia_cv[i+1]  ; j++ ){
    	int currv = lcv[j];
    	assert(currv >=0 && currv < nnodes);
    	if( bnode[currv] == 0 )
    	  for( k = 0 ; k < 4 ; k++ )
    	    phiv[currv][k] += phic_new[i][k] * wcv[j];
      }
      for( j = 0 ; j < 4 ; j++ )
    	phic_old[i][j] = phic_new[i][j];
    }
    

    for( i = 0 ; i < nfaces ; i++ ){
      int n1 = lfv0[i];
      int n2 = lfv1[i];
      assert(n1 >= 0 && n1 < nnodes );
      assert(n2 >= 0 && n2 < nnodes );

      double xv1 = xv[n1];
      double xv2 = xv[n2];
      double yv1 = yv[n1];
      double yv2 = yv[n2];
      /* if( i == 0 ) { */
      /* 	printf("i= %d, phiv[%d]=[%lf,%lf,%lf,%lf], phiv[%d]=[%lf,%lf,%lf,%lf]\n",i,n1,phiv[n1][0],phiv[n1][1],phiv[n1][2],phiv[n1][3],n2,phiv[n2][0],phiv[n2][1],phiv[n2][2],phiv[n2][3]); */
      /* } */
	
      for( j = 0 ; j < 4 ; j++ )
    	dphi[i][j] = ( phiv[n2][j] - phiv[n1][j] ) / sqrt( ( xv2 - xv1 ) * ( xv2 - xv1 ) + ( yv2 - yv1 ) * ( yv2 - yv1 ) );

      /* if( i == 0 ) */
      /* 	printf("i=%d,dphi[%d]=[%lf,%lf,%lf,%lf]\n",i,i,dphi[i][0],dphi[i][1],dphi[i][2],dphi[i][3]); */
    }
    
    
    for( i = 0 ; i < ncells ; i++ ){
      double diag_matrix[4][4];
      double new_phi[4];
      for( j = 0 ; j < 4 ; j++ ){
    	for( k = 0 ; k < 4 ; k++ )
    	  diag_matrix[j][k] = 0.0;
    	new_phi[j] = sc[i][j];
    	diag_matrix[j][j] = vol_vec[i][j];
      }
    
      for( j = ia_cf[i] ; j < ia_cf[i+1] ; j++ ){
    	int currf = lcf[j];
    	if( bface[currf] == 0 ){
    	  int currcell;
    	  double alpha;
    	  if( lfc0[currf] == i ){
    	    currcell = lfc1[currf];
    	    alpha = 1;
    	  }
    	  else{
    	    alpha = -1;
    	    currcell = lfc0[currf];
    	  }
	  
    	  for( k = 0 ; k < 4 ; k++ )
    	    for( l = 0 ; l < 4 ; l++ ){
    	      new_phi[k] -= (fclink[currf][k*4+l] * phic_old[currcell][l] + alpha * ftlink[currf][k*4+l] * dphi[currf][l]) ;
    	      diag_matrix[k][l] -= fclink[currf][k*4+l];
    	    }
    	}
    	else{
    	  int currbf = f_to_bf[currf];
    	  assert(currbf >= 0 );
    	  for( k = 0 ; k < 4 ; k++ )
    	    for( l = 0 ; l < 4 ; l++ ){
    	      new_phi[k] -= (fclink[currf][k*4+l] * phib[currbf][l] + ftlink[currf][k*4+l] * dphi[currf][l]) ;
    	      diag_matrix[k][l] -= fclink[currf][k*4+l];
    	    }
    	}
      }
      
      inverse_multiply(new_phi,diag_matrix);
      
      for( j = 0 ; j < 4 ; j++ )
    	phic_new[i][j] = new_phi[j];
    }
    
    resid0 = 0.0;
    resid1 = 0.0;
    resid2 = 0.0;
    resid3 = 0.0;
    
    for( i = 0 ; i < ncells ; i++ ){
      double temp = phic_new[i][0] - phic_old[i][0];
      resid0 += temp * temp;
      temp = phic_new[i][1] - phic_old[i][1];
      resid1 += temp * temp;
      temp = phic_new[i][2] - phic_old[i][2];
      resid2 += temp * temp;
      temp = phic_new[i][3] - phic_old[i][3];
      resid3 += temp * temp;
    }
    resid0 = sqrt(resid0) / ncells;
    resid1 = sqrt(resid1) / ncells;
    resid2 = sqrt(resid2) / ncells;
    resid3 = sqrt(resid3) / ncells;

    count++;
#ifndef NDEBUG
    printf("%d %lf %lf %lf %lf\n",count,resid0,resid1,resid2,resid3);
#endif
  }
  double stop_t = rtclock();
  printf("[IEC]:OriginalTime:%lf\n",stop_t - start_t);
  //printf("%d %lf %lf %lf %lf\n",count,resid0,resid1,resid2,resid3);

  /* print_output(); */

  free(xc);
  free(yc);
  free(xf);
  free(yf);
  free(volcell);
  free(areaf);
  free(vecfx);
  free(vecfy);
  free(xv);
  free(yv);
  free(ia_cf);
  free(ia_cv);
  free(lcf);
  free(lcv);
  /* free_2d_int(lfc); */
  /* free_2d_int(lfv); */
  free(lfc0);
  free(lfc1);
  free(lfv0);
  free(lfv1);
  
  free(bface);
  free(f_to_bf);
  free(bf_to_f);
  free(bf_to_c);
  free(bnode);
  free(bctype);
  free(bcname);
  free(wcv);
  free_2d(wbfv);
  free(t);
  free(tb);
  free_2d(sc);
  free_2d(sb);
  free_2d(phic_new);
  free_2d(phic_old);
  free_2d(phib);
  free_2d(phiv);
  free_2d(dphi);
  free_2d(fclink);
  free_2d(ftlink);
  free_2d(vol_vec);
  free_2d(ba);
  free_2d(bb);
  free_2d(bc); 

  return 0;
}
int main (int argc, char* argv[]) {

  srand(atoi(argv[1])); //Take argument to write special extension to file
  double Btotalple, Bnurseple, Bspawnple, Btotalsol, Bnursesol, Bspawnsol ;    /* biomass on nursery, total biomass */

  //Read in the data
  readgrid(&GridFood , X_MAX, Y_MAX, 52, theFood);
  cout << "Read Food completed" << endl;

  readgrid(&GridTemp , X_MAX, Y_MAX, 52, theTemp);
  cout << "Read Temp completed" << endl;

  readgrid(&GridLMort , X_MAX, Y_MAX, 52, theLMort);
  cout << "Read Larval Mortality completed" << endl;

  readgrowthgam(&WeekPropFood,52,theGrowthGam);
  cout << "Read growth gam completed" << endl;

  /* INITIALISE INDIVIDUALS AT START, FIRST PLAICE, THEN SOLE */
  for(int i=0; i < POPMAX; i++) {
    ple[i].sex    = (i%2)+1;
    ple[i].weight = BORNWGHT;
    ple[i].id     = id ;
    ple[i].stage  = 1 ; /* everybody should be mature */
    ple[i].age    = 52 ;
    ple[i].u_m    = U_M ;
    ple[i].u_f    = U_F;
    if(SPAREA == 1){ 
      ple[i].X      = 75 ;
      ple[i].Y      = 53 ;
    } else if(SPAREA == 2){
        ple[i].X      = 91 ;
        ple[i].Y      = 67 ;
    }             
    int X         = ple[i].X;
    int Y         = ple[i].Y;
    int resX, resY;
    for(int dd=0; dd < L_CHR1;  dd++){ //check juvenile strategy
        do{ple[i].juvXdir[dd] = (char)( (rand()% 11) -5); // Movement of maximum 5 left or right //
           ple[i].juvYdir[dd] = (char)( (rand()% 11) -5); // Movement of maximum 5 up or down    //
           resX = (int) (ple[i].juvXdir[dd] * ple[i].swim()) ;
           resY = (int) (ple[i].juvYdir[dd] * ple[i].swim()) ;
        } while ((theTemp[1][X + resX][Y + resY ] < -15) ||(( X + resX) <0) || (( X + resX) > X_MAX) ||(( Y + resY) < 0) || ((Y + resY) > Y_MAX));
        X += resX;
        Y += resY;
        ple[i].weight  = ple[i].weight  + ple[i].growth(theFood[(dd+6)%52][X][Y], theTemp[(dd+6)%52][X][Y],theGrowthGam[(dd+6)%52]);      
    }    
    for(int dd=0; dd <L_CHR2;  dd++){ //check juvenile strategy
      ple[i].adultXdir[dd] = (char)( (rand()% 11) -5); // Movement of maximum 5 left or right //
      ple[i].adultYdir[dd] = (char)( (rand()% 11) -5); // Movement of maximum 5 up or down    //
    }
    ple[i].weight = BORNWGHT;
    id++;
  }

//  for(int i=0; i < POPMAX; i++){
//    sol[i].sex    = (i%2)+1;
//    sol[i].weight = BORNWGHT ;
//    sol[i].id     = id ;
//    sol[i].stage  = 1 ;
//    sol[i].age    = 52 ;
//    sol[i].u_m    = U_M ;
//    sol[i].u_f    = U_F;
//    if(SPAREA == 1){ 
//      sol[i].X      = 75 ;
//      sol[i].Y      = 53 ;
//    } else if(SPAREA == 2){
//        sol[i].X      = 91 ;
//        sol[i].Y      = 67 ;
//    } 
//    int X         = sol[i].X;
//    int Y         = sol[i].Y;
//    int resX, resY;
//    for(int dd=0; dd < L_CHR1;  dd++){                         //check juvenile strategy
//        do{sol[i].juvXdir[dd] = (char)( (rand()% 11) -5);      // Movement of maximum 5 left or right //
//           sol[i].juvYdir[dd] = (char)( (rand()% 11) -5);      // Movement of maximum 5 up or down    //
//           resX = (int) (sol[i].juvXdir[dd] * sol[i].swim()) ;
//           resY = (int) (sol[i].juvYdir[dd] * sol[i].swim()) ;
//        } while ((theTemp[1][X + resX][Y + resY ] < -15) ||(( X + resX) <0) ||((X + resX) > X_MAX) ||((Y + resY )< 0) ||((Y + resY) > Y_MAX));
//        X += resX;
//        Y += resY;
//        sol[i].weight  = sol[i].weight  + sol[i].growth(theFood[(dd+6)%52][X][Y], theTemp[(dd+6)%52][X][Y],theGrowthGam[(dd+6)%52]);      
//    }
//    for(int dd=0; dd < L_CHR2 ;  dd++){                        //check juvenile strategy
//      sol[i].adultXdir[dd] = (char)((rand()% 11) -5);          // Movement of maximum 5 left or right //
//      sol[i].adultYdir[dd] = (char)((rand()% 11) -5);          // Movement of maximum 5 up or down    //
//    }
//    sol[i].weight = BORNWGHT;
//    id++;
//  }                                                            // end for loop over individuals /
  cout << "Initialisation of Plaice and Sole done" << endl;

  int aliveple = POPMAX, alivesol = POPMAX;

  string ext(".csv");                                          //Open file to write output to disk
  string SPname("_SPAREA");
  char buffer [4];
  string SP(itoa(SPAREA,buffer,10));
  filename += ( argv[1] + SPname + SP + ext);
  cout << filename << endl;
  popname += (argv[1] + SPname + SP + ext);
  myfile.open (filename.c_str() );
  mypopulation.open(popname.c_str());

  /* START SIM */
  for(int t = 6; t < T_MAX; t++){
   /* CALCULATE TOTAL BIOMASS AND BIOMASS ON NURSERY FOR TWO SPECIES */
    Bnurseple = Btotalple = Bspawnple = Bnursesol = Btotalsol = Bspawnsol = 0;

    for(int n = 0 ; n < aliveple ; n++) {
        if (ple[n].stage < 3 ) {
            Btotalple  += ple[n].weight ;
            if (ple[n].stage < 2 ) Bnurseple += ple[n].weight;
        }
    }

//    for(int n = 0 ; n < alivesol ; n++) {
//        if (sol[n].stage < 3 ) {
//            Btotalsol  += sol[n].weight ; 
//            if (sol[n].stage < 2 ) Bnursesol += sol[n].weight;
//        }
//    }

    Bspawnple =  Btotalple - Bnurseple;
//    Bspawnsol =  Btotalsol - Bnursesol;

    move(ple, aliveple, t%52, theTemp);                                                       // Move individuals every tenth timestep //
//    move(sol, alivesol, t%52, theTemp);                                                       // Move individuals every tenth timestep //  

    age        (ple, aliveple)    ;                                                           // Function of ageing //
//    age        (sol, alivesol)    ;                                                           // Function of ageing //    

    mortality(ple, LAMBDAple, aliveple ,Bnurseple ) ;                                         // Function mortality //
//    mortality(sol, LAMBDAsol, alivesol ,Bnursesol ) ;                                         // Function mortality */

    growth     (ple, aliveple, Bnurseple, t % 52, theFood, theTemp, theGrowthGam) ;                        // Function of growth //   
//    growth     (sol, alivesol, Bnursesol, t % 52, theFood, theTemp, theGrowthGam) ;                        // Function of growth //

    if(t%52 == 10 ) maturation (ple, aliveple)   ; //Checked with Cindy, gonads start to develop in March // Function of maturation //
//    if(t%52 == 10 ) maturation (sol, alivesol)   ;                                           // Function of maturation //

    if(t%52 == 5) cout<<"i " << argv[1]  <<" t " << t << " ssb ple " << Bspawnple<<" num ple "<<aliveple<< endl; //output(ple,t, 3);            // Write biomass and number to screen, followed by data for 10$
//    if(t%52 == 5) cout<<"i " << argv[1]  <<  " t " << t << " ssb sol " << Bspawnsol<<" num sol "<<alivesol<< endl; //output(sol,t, 3);            // Write biomass and number to screen, followed by data fo$

    aliveple = alive2front (ple)  ;                                                           // shuffle so that alives are in front*/
//    alivesol = alive2front (sol)  ;                                                           // shuffle so that alives are in front*/

    if(t%52 == 5) aliveple = reproduction(ple, R1ple, R2ple, aliveple, Bspawnple, theTemp); // Function of reproduction  in week 5*/
//    if(t%52 == 5) alivesol = reproduction(sol, R1sol, R2sol, alivesol, Bspawnsol, theTemp); // Function of reproduction  in week 5*/

    if(t%52 == 5){ larvalmortality (ple, aliveple, theLMort); aliveple = alive2front (ple);} // larvalmortality depends on field, now uniform field where everybody survives //
//    if(t%52 == 5){ larvalmortality (sol, alivesol, theLMort); alivesol = alive2front (sol);} // larvalmortality depends on field, now uniform field where everybody survives // 

    //Write output
    if ((t==6) ||( (t+A_MAX) % (int)(T_MAX/(T_STEP-1)) < 52 && t % 52 == 6)){
        int nn  = aliveple;
        int age = ple[nn].age;
        do{ age = ple[nn].age;
          nn--;
        } while ((nn > (aliveple - P_WRITE)) && (age <= 53));
        minid = ple[nn + 1].id;
        maxid = ple[aliveple - 1].id;
    } else if ((t < 6 + A_MAX) ||( (t + A_MAX)% (int)(T_MAX/(T_STEP-1)) < A_MAX +52)){
      for(int nn = 0; nn < aliveple; nn++){
        if(ple[nn].stage < 3 && (ple[nn].id > minid & ple[nn].id < maxid)){
          myfile <<t << "," <<       ple[nn].id          << "," << (int) ple[nn].sex          << "," <<       ple[nn].age                           << "," << (int) ple[nn].stage 
                     << "," <<       ple[nn].X           << "," <<       ple[nn].Y            << "," <<       ple[nn].weight       
                     << "," << (int) ple[nn].juvXdir[(int) (ple[nn].age)]                     << "," << (int) ple[nn].juvYdir[(int) (ple[nn].age)]  
                     << "," << (int) ple[nn].adultXdir[(t+1)%52]                              << "," << (int) ple[nn].adultYdir[(t+1)%52]           << endl;
        }
      }
    }

    //Write output every 15 years (cycle of complete new population)        
    if(t % (A_MAX) == 5){ writePopStruct(mypopulation, ple,aliveple,t);}

  } //end of timeloop

  myfile.close() ; mypopulation.close();
  return 0 ;
}