Exemplo n.º 1
0
/**@brief Saves the Galaxy_Output structure for all the galaxies in
 *        the current tree into the current output file (one for each
 *        input dark matter file) for the chosen snapshots.
 *
 *        If UPDATETYPETWO=1 then the positions and velocities of type 2
 *        galaxies are updated from the most bound dark matter particle.
 *        After that the GALAXY_OUTPUT structure is created and written.
 *        input: int file number (current file where the output is
 *        being written), int tree number (tree being currently treated).
 *
 *        If USE_MEMORY_TO_MINIMIZE_IO ON, this write statements in this
 *        routine copy the galaxy data from the working structures into
 *        pointers until that has been done for all the tree in a given file.
 */
void save_galaxy_append(int tree, int i, int n)
{
  struct GALAXY_OUTPUT galaxy_output;
#ifndef NORMALIZEDDB
  prepare_galaxy_for_output(n, &HaloGal[i], &galaxy_output);

  myfwrite(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, FdGalDumps[n]);
#endif
  TotGalaxies[n]++;		//this will be written later
  TreeNgals[n][tree]++;		//this will be written later (Number of galaxies in each tree)
}
/*
 * for now store SFH bins boht in GALAXY_OUTPUT and SFH_OUTPUT to check things are ok.
 * Later choose only latter mode.
 */
void save_galaxy_tree_append(int i)
{
	int ibin,numbins;
  struct GALAXY_OUTPUT galaxy_output;

  prepare_galaxy_for_output(HaloGal[i].SnapNum, &HaloGal[i], &galaxy_output);

#ifdef STAR_FORMATION_HISTORY
  galaxy_output.sfh_numbins = galaxy_output.sfh_ibin;
#endif

  myfwrite(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);

}
Exemplo n.º 3
0
void save_galaxy_for_mcmc(int gal_index)
{
    //printf("ID=%lld snap=%d sampleID=%d\n", HaloIDs[HaloGal[gal_index].HaloNr].FirstHaloInFOFgroup,
    //		HaloGal[gal_index].SnapNum, SampleIDs[treenr]);
    int snap, fof, j, aa;
    float low_mass_limit, high_mass_limit, StellarMass;
    double log10_Hubble_h, PMass;

#ifdef MR_PLUS_MRII
    if(Switch_MR_MRII==1)
    {
        low_mass_limit=9.5;
        high_mass_limit=13.0;
    }
    else if(Switch_MR_MRII==2)
    {
        low_mass_limit=6.0;
        high_mass_limit=9.5;
    }
#else
    low_mass_limit=7.27;
    high_mass_limit=13.0;
#ifdef MRII
    low_mass_limit=6.0;
    high_mass_limit=11.27;
#endif
#endif


    log10_Hubble_h=log10(Hubble_h);

    //if(HaloGal[gal_index].SnapNum==53)
    //	printf("id=%lld\n",HaloIDs[HaloGal[gal_index].HaloNr].FirstHaloInFOFgroup);
    for(snap=0; snap<NOUT; snap++)
    {
        //StellarMass=log10(1E10 * (HaloGal[gal_index].DiskMass+HaloGal[gal_index].BulgeMass)/Hubble_h);
        //StellarMass=log10(1E10 * (HaloGal[gal_index].DiskMass+HaloGal[gal_index].BulgeMass)/Hubble_h)+ran3(&MCMCseed)*0.04*(1+MCMCConstraintsZZ[snap]);
        StellarMass=log10(1E10 * (HaloGal[gal_index].DiskMass+HaloGal[gal_index].BulgeMass)*Hubble_h);
        StellarMass+=gassdev(&MCMCseed)*0.04*(1+MCMCConstraintsZZ[snap]);

        for(fof=0; fof<NFofsInSample[snap]; fof++)
            if( StellarMass > low_mass_limit &&  StellarMass < high_mass_limit &&
                    HaloIDs[HaloGal[gal_index].HaloNr].FirstHaloInFOFgroup == MCMC_FOF[fof].FoFID[snap])
            {
                //MCMC_GAL[TotMCMCGals[snap]].StellarMass[snap] = log10(1E10 * ((HaloGal[gal_index].DiskMass+HaloGal[gal_index].BulgeMass)/Hubble_h));
                //if((double)((int)((MCMCConstraintsZZ[snap]*10)+0.5)/10.)>0.9)
                //MCMC_GAL[TotMCMCGals[snap]].StellarMass[snap] += ran3(&MCMCseed)*0.04*(1+MCMCConstraintsZZ[snap]);
                MCMC_GAL[TotMCMCGals[snap]].StellarMass[snap] = StellarMass;
                MCMC_GAL[TotMCMCGals[snap]].ColdGas[snap] = log10(1E10 * (HaloGal[gal_index].ColdGas*0.54*Hubble_h));
                MCMC_GAL[TotMCMCGals[snap]].BulgeMass[snap] = log10(1E10 * HaloGal[gal_index].BulgeMass*Hubble_h);
                MCMC_GAL[TotMCMCGals[snap]].BlackHoleMass[snap] = log10(1E10 * HaloGal[gal_index].BlackHoleMass*Hubble_h);
                //in units of Solar Masses per yr
                MCMC_GAL[TotMCMCGals[snap]].Sfr[snap]
                    = HaloGal[gal_index].Sfr * UnitMass_in_g / UnitTime_in_s * SEC_PER_YEAR / SOLAR_MASS;

#ifdef COMPUTE_SPECPHOT_PROPERTIES
#ifdef POST_PROCESS_MAGS
                struct GALAXY_OUTPUT galaxy_output;

//#ifndef HALOMODEL
                //in case of postprocess magnitudes they are only calculates here, inside prepare
                prepare_galaxy_for_output(snap, &HaloGal[gal_index], &galaxy_output);


                MCMC_GAL[TotMCMCGals[snap]].MagU[snap] = galaxy_output.MagDust[0]-5.*log10_Hubble_h;
                ////MCMC_GAL[TotMCMCGals[snap]].MagB[snap] = galaxy_output.MagDust[1]-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].MagV[snap] = galaxy_output.MagDust[1]-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].MagJ[snap] = galaxy_output.MagDust[2]-5.*log10_Hubble_h;
                ////MCMC_GAL[TotMCMCGals[snap]].MagK[snap] = galaxy_output.MagDust[4]-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].Magu[snap] = galaxy_output.MagDust[3]-5.*log10_Hubble_h;
                ////MCMC_GAL[TotMCMCGals[snap]].Magg[snap] = galaxy_output.MagDust[6]-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].Magr[snap] = galaxy_output.MagDust[4]-5.*log10_Hubble_h;
                ////MCMC_GAL[TotMCMCGals[snap]].Magi[snap] = galaxy_output.MagDust[8]-5.*log10_Hubble_h;
                ////MCMC_GAL[TotMCMCGals[snap]].Magz[snap] = galaxy_output.MagDust[9]-5.*log10_Hubble_h;
//#endif
                /*MCMC_GAL[TotMCMCGals[snap]].MagV[snap] = galaxy_output.MagDust[0]-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].MagB[snap] = galaxy_output.MagDust[1]-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].MagK[snap] = galaxy_output.MagDust[2]-5.*log10_Hubble_h;*/
#else

                MCMC_GAL[TotMCMCGals[snap]].MagU[snap] = lum_to_mag(HaloGal[gal_index].LumDust[0][snap])-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].MagV[snap] = lum_to_mag(HaloGal[gal_index].LumDust[1][snap])-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].MagJ[snap] = lum_to_mag(HaloGal[gal_index].LumDust[2][snap])-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].Magu[snap] = lum_to_mag(HaloGal[gal_index].LumDust[3][snap])-5.*log10_Hubble_h;
                MCMC_GAL[TotMCMCGals[snap]].Magr[snap] = lum_to_mag(HaloGal[gal_index].LumDust[4][snap])-5.*log10_Hubble_h;

                //MCMC_GAL[TotMCMCGals[snap]].MagV[snap] = lum_to_mag(HaloGal[gal_index].LumDust[0][snap])-5.*log10_Hubble_h;
                //MCMC_GAL[TotMCMCGals[snap]].MagB[snap] = lum_to_mag(HaloGal[gal_index].LumDust[1][snap])-5.*log10_Hubble_h;
                //MCMC_GAL[TotMCMCGals[snap]].MagK[snap] = lum_to_mag(HaloGal[gal_index].LumDust[2][snap])-5.*log10_Hubble_h;

#endif //POST_PROCESS_MAGS
#endif //COMPUTE_SPECPHOT_PROPERTIES

#ifdef HALOMODEL
                if(snap==0)
                {
                    MCMC_GAL[TotMCMCGals[snap]].fofid[snap] = fof;
                    //MCMC_GAL[TotMCMCGals[snap]].M_Crit200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].Len*PartMass*1.e10);
                    MCMC_GAL[TotMCMCGals[snap]].M_Crit200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].M_Crit200*1.e10);
                    MCMC_GAL[TotMCMCGals[snap]].M_Mean200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].M_Mean200*1.e10);
#ifdef MCRIT
                    MCMC_GAL[TotMCMCGals[snap]].M_Mean200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].M_Crit200*1.e10);
#endif
                    MCMC_GAL[TotMCMCGals[snap]].x[snap] = HaloGal[gal_index].Pos[0];
                    MCMC_GAL[TotMCMCGals[snap]].y[snap] = HaloGal[gal_index].Pos[1];
                    MCMC_GAL[TotMCMCGals[snap]].z[snap] = HaloGal[gal_index].Pos[2];
                    MCMC_GAL[TotMCMCGals[snap]].Type[snap] = HaloGal[gal_index].Type;
                    MCMC_GAL[TotMCMCGals[snap]].ngal[snap] = 0;
                }
#endif

                MCMC_GAL[TotMCMCGals[snap]].Weight[snap] = MCMC_FOF[fof].Weight[snap];

                //#ifdef MR_PLUS_MRII
                ////

                //NOW GET PROPERTIES FOR FOF GROUPS - done ofr the particular fof that current galaxy resides in

                ++MCMC_FOF[fof].NGalsInFoF[snap];

#ifdef HALOMODEL
                if(HaloGal[gal_index].Type==0)
                {
                    MCMC_FOF[fof].IndexOfCentralGal[snap]=TotMCMCGals[snap];
                    //MCMC_FOF[fof].M_Crit200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].Len*PartMass*1.e10);
                    MCMC_FOF[fof].M_Crit200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].M_Crit200*1.e10);
                    MCMC_FOF[fof].M_Mean200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].M_Mean200*1.e10);
#ifdef MCRIT
                    MCMC_FOF[fof].M_Mean200[snap] = log10(Halo[HaloGal[gal_index].HaloNr].M_Crit200*1.e10);
#endif
                }
#endif

                ++TotMCMCGals[snap];

                if(TotMCMCGals[snap] > MCMCAllocFactor)
                    terminate("Maximum number of galaxies in MCMC structure reached. Increase MCMCSmartFactor\n");
            }
    }

    return;
}
Exemplo n.º 4
0
void save_galaxies(int filenr, int tree)
{
  char buf[MAX_STRING_LEN];
  int i, n;
  struct GALAXY_OUTPUT galaxy_output = {0};
  int OutputGalCount[MAXSNAPS], *OutputGalOrder, nwritten;

  OutputGalOrder = (int*)malloc( NumGals*sizeof(int) );
  if(OutputGalOrder == NULL) {
    fprintf(stderr,"Error: Could not allocate memory for %d int elements in array `OutputGalOrder`\n", NumGals);
    ABORT(10);
  }

  // reset the output galaxy count and order
  for(i = 0; i < MAXSNAPS; i++)
    OutputGalCount[i] = 0;
  for(i = 0; i < NumGals; i++)
    OutputGalOrder[i] = -1;
  
  // first update mergeIntoID to point to the correct galaxy in the output
  for(n = 0; n < NOUT; n++)
    {
      for(i = 0; i < NumGals; i++)
	{
	  if(HaloGal[i].SnapNum == ListOutputSnaps[n])
	    {
	      OutputGalOrder[i] = OutputGalCount[n];
	      OutputGalCount[n]++;
	    }
	}
    }
  
  for(i = 0; i < NumGals; i++)
    if(HaloGal[i].mergeIntoID > -1)
      HaloGal[i].mergeIntoID = OutputGalOrder[HaloGal[i].mergeIntoID];    
  
  // now prepare and write galaxies
  for(n = 0; n < NOUT; n++)
  {
      // only open the file if it is not already open.
      if( !save_fd[n] )
	    {
        snprintf(buf, MAX_STRING_LEN - 1, "%s/%s_z%1.3f_%d", OutputDir, FileNameGalaxies, ZZ[ListOutputSnaps[n]], filenr);
       
        save_fd[n] = fopen(buf, "r+");
        if (save_fd[n] == NULL) 
        {
          fprintf(stderr, "can't open file `%s'\n", buf);
          ABORT(0);
        }
        
        // write out placeholders for the header data.
        size_t size = (Ntrees + 2)*sizeof(int); /* Extra two inegers are for saving the total number of trees and total number of galaxies in this file */
        int* tmp_buf = (int*)malloc( size );
        if (tmp_buf == NULL)
        {
          fprintf(stderr, "Error: Could not allocate memory for header information for file %d\n", n);
          ABORT(10);
        }

        memset( tmp_buf, 0, size );
        nwritten = fwrite( tmp_buf, sizeof(int), Ntrees + 2, save_fd[n] );
        if (nwritten != Ntrees + 2)
        {
          fprintf(stderr, "Error: Failed to write out %d elements for header information for file %d.  Only wrote %d elements.\n", Ntrees + 2, n, nwritten);
        }
        free( tmp_buf );
	    }
      
      for(i = 0; i < NumGals; i++)
	    {
        if(HaloGal[i].SnapNum == ListOutputSnaps[n])
	      {        
          prepare_galaxy_for_output(filenr, tree, &HaloGal[i], &galaxy_output);
   
          nwritten = myfwrite(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, save_fd[n]);
          if (nwritten != 1)
          {
            fprintf(stderr, "Error: Failed to write out the galaxy struct for galaxy %d within file %d.  Meant to write 1 element but only wrote %d elements.\n", i, n, nwritten); 
          }
          
          TotGalaxies[n]++;
          TreeNgals[n][tree]++;	      
	      }
	    }
      
  }

  // don't forget to free the workspace.
  free( OutputGalOrder );

}