Exemple #1
0
int
main (void)
{
  FILE *f;

  mdl_t source_mdl;
  inp_t fake;
  fake.output.time_steps = 128;
  fake.solver.ti = 0.000001  * CONST_MKSA_YEAR;
  fake.solver.tf = 10000000  * CONST_MKSA_YEAR;
  int verbose = 0;

  /* Create the input.ini file */

  f = fopen ("source.mdl", "w");
  fprintf (f, "# This source model file was created by source_test\n");
  fprintf (f, "# cell number, Av [mag], n(H) [cm^-3], Tgas [K], Tdust [K]\n");
  fprintf (f, "0	 0.1	1e+02	15.0	12.0\n");
  fprintf (f, "1	 1.0	1e+03	11.0	10.0\n");
  fprintf (f, "2	10.0	1e+04	 8.0	 7.0\n");
  fclose (f);

  /* Read it */

  if( read_source ("source.mdl", &source_mdl, &fake, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Check that the values are correct */
  if ((source_mdl.n_cells == 3) && 
      (source_mdl.cell[0].av[0] == 0.1) &&
      (source_mdl.cell[0].nh[0] == 1e2) && 
      (source_mdl.cell[0].tgas[0] == 15) &&
      (source_mdl.cell[0].tdust[0] == 12) &&
      (source_mdl.cell[1].av[0] == 1.0) &&
      (source_mdl.cell[1].nh[0] == 1e3) &&
      (source_mdl.cell[1].tgas[0] == 11) &&
      (source_mdl.cell[1].tdust[0] == 10) &&
      (source_mdl.cell[2].av[0] == 10.0) &&
      (source_mdl.cell[2].nh[0] == 1e4) &&
      (source_mdl.cell[2].tgas[0] == 8) &&
      (source_mdl.cell[2].tdust[0] == 7) &&
      (source_mdl.ts.time_steps[10] - 332.988055 < 0.0001) && 
      (source_mdl.ts.time_steps[23] - 7130.784562 < 0.0001) &&
      (source_mdl.ts.time_steps[47] - 2040939.960351 < 0.0001) )
    {
      free_mdl(&source_mdl);
      return EXIT_SUCCESS;
    }

  else
  {
    free_mdl(&source_mdl);
    return EXIT_FAILURE;
  }
}
Exemple #2
0
int
main (void)
{
  FILE *f;

  mdl_t source_mdl;
  inp_t fake;
  int verbose = 0;

  /* Create the input.ini file */

  f = fopen ("source_dyn.mdl", "w");
  fprintf (f, "# Source model file example for a time-dependant source structure\n"
      "[times]\n"
      "   0    1.00e-06\n"
      "   1    1.24e-06\n"
      "   2    1.55e-06\n"
      "   3    1.92e-06\n"
      "   4    2.39e-06\n"
      "   5    2.97e-06\n"
      "   6    3.69e-06\n"
      "   7    4.59e-06\n"
      "   8    5.70e-06\n"
      "   9    7.09e-06\n"
      "  10    8.81e-06\n"
      "[cells]\n"
      "# cell (= shell) number, time index, Av [mag], nH [cm^-3], Tgas [K], Tdust [K]\n"
      "   0      0    0.00  1.00e+04   10.00   10.00\n"
      "   0      1    0.16  1.11e+04   11.50   11.50\n"
      "   0      2    0.31  1.24e+04   12.99   12.99\n"
      "   0      3    0.47  1.39e+04   14.49   14.49\n"
      "   0      4    0.63  1.55e+04   15.98   15.98\n"
      "   0      5    0.79  1.72e+04   17.48   17.48\n"
      "   0      6    0.94  1.92e+04   18.98   18.98\n"
      "   0      7    1.10  2.14e+04   20.47   20.47\n"
      "   0      8    1.26  2.39e+04   21.97   21.97\n"
      "   0      9    1.42  2.66e+04   23.46   23.46\n"
      "   0     10    1.57  2.97e+04   24.96   24.96\n"
      "   1      0    0.00  1.00e+04   10.00   10.00\n"
      "   1      1    0.16  1.11e+04   11.50   11.50\n"
      "   1      2    0.31  1.24e+04   12.99   12.99\n"
      "   1      3    0.47  1.39e+04   14.49   14.49\n"
      "   1      4    0.63  1.55e+04   15.98   15.98\n"
      "   1      5    0.79  1.72e+04   17.48   17.48\n"
      "   1      6    0.94  1.92e+04   18.98   18.98\n"
      "   1      7    1.10  2.14e+04   20.47   20.47\n"
      "   1      8    1.26  2.39e+04   21.97   21.97\n"
      "   1      9    1.42  2.66e+04   23.46   23.46\n"
      "   1     10    1.57  2.97e+04   24.96   24.96\n"
      "   2      0    0.00  1.00e+04   10.00   10.00\n"
      "   2      1    0.16  1.11e+04   11.50   11.50\n"
      "   2      2    0.31  1.24e+04   12.99   12.99\n"
      "   2      3    0.47  1.39e+04   14.49   14.49\n"
      "   2      4    0.63  1.55e+04   15.98   15.98\n"
      "   2      5    0.79  1.72e+04   17.48   17.48\n"
      "   2      6    0.94  1.92e+04   18.98   18.98\n"
      "   2      7    1.10  2.14e+04   20.47   20.47\n"
      "   2      8    1.26  2.39e+04   21.97   21.97\n"
      "   2      9    1.42  2.66e+04   23.46   23.46\n"
      "   2     10    1.57  2.97e+04   24.96   24.96\n");
  fclose (f);

  /* Read it */

  if( read_source ("./source_dyn.mdl", &source_mdl, &fake, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }
  /* Check that the values are correct */
  if ((source_mdl.n_cells == 3) &&
      (source_mdl.ts.n_time_steps == 11) &&
      (source_mdl.cell[0].av[0] == 0.0) &&
      (source_mdl.cell[0].nh[0] == 1e4) && 
      (source_mdl.cell[0].tgas[0] == 10) &&
      (source_mdl.cell[0].tdust[0] == 10) &&
      (source_mdl.cell[1].av[5] == 0.79) &&
      (source_mdl.cell[1].nh[5] == 1.72e4) &&
      (source_mdl.cell[1].tgas[5] == 17.48) &&
      (source_mdl.cell[1].tdust[5] == 17.48) &&
      (source_mdl.cell[2].av[10] == 1.57) &&
      (source_mdl.cell[2].nh[10] == 2.97e4) &&
      (source_mdl.cell[2].tgas[10] == 24.96) &&
      (source_mdl.cell[2].tdust[10] == 24.96) &&
      (source_mdl.ts.time_steps[5] / CONST_MKSA_YEAR - 2.97e-6 < 0.01e-6 ))
  {
    free_mdl(&source_mdl);
    return EXIT_SUCCESS;
  }

  else
  {
    free_mdl(&source_mdl);
    return EXIT_FAILURE;
  }
}
Exemple #3
0
int
main (int argc, char *argv[])
{
  inp_t input_params;
  mdl_t source_mdl;
  net_t network;
  int cell_index;

  int verbose = 1;
  char *input_file;

  /* Parse options and command line arguments. Diplay help
     message if no (or more than one) argument is given. */

    {
      int opt;

      static struct option longopts[] = {
          {"help", no_argument, NULL, 'h'},
          {"version", no_argument, NULL, 'V'},
          {"verbose", no_argument, NULL, 'v'},
          {"quiet", no_argument, NULL, 'q'},
          {0, 0, 0, 0}
      };

      while ((opt = getopt_long (argc, argv, "hVvq", longopts, NULL)) != -1)
        {
          switch (opt)
            {
            case 'h':
              usage ();
              return EXIT_SUCCESS;
              break;
            case 'V':
              version ();
              return EXIT_SUCCESS;
              break;
            case 'v':
              verbose = 2;
              break;
            case 'q':
              verbose = 0;
              break;
            default:
              usage ();
              return EXIT_FAILURE;
            }
        };
      argc -= optind;
      argv += optind;
      if (argc != 1)
        {
          usage ();
          return EXIT_FAILURE;
        }
      input_file = argv[0];
    }

  /* Read the input file */
  if( read_input_file_names (input_file, &input_params.files, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the chemical network file */
  if( read_network (input_params.files.chem_file, &network, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the input file */
  if( read_input (input_file, &input_params, &network, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the source model file */
  if( read_source (input_params.files.source_file, &source_mdl, &input_params,
               verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  // Hdf5 files, datatype and dataspace
  hid_t       fid, datatype, dataspace, dataset, tsDataset, tsDataspace,  speciesDataset, speciesDataspace, speciesType;
  datatype = H5Tcopy(H5T_NATIVE_DOUBLE);

  hsize_t     dimsf[ ROUTE_DATASET_RANK ]={  source_mdl.n_cells, source_mdl.ts.n_time_steps, input_params.output.n_output_species, N_OUTPUT_ROUTES };
  fid = H5Fcreate( "astrochem_output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); 
  dataspace = H5Screate_simple( ABUNDANCE_DATASET_RANK, dimsf, NULL);

  // Add Atributes
  hid_t simpleDataspace = H5Screate(H5S_SCALAR);
  hid_t attrType = H5Tcopy(H5T_C_S1);
  H5Tset_size ( attrType, MAX_CHAR_FILENAME );
  H5Tset_strpad(attrType,H5T_STR_NULLTERM);
  hid_t attrNetwork = H5Acreate( fid, "chem_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT);
  H5Awrite( attrNetwork, attrType, realpath( input_params.files.chem_file, NULL ) );
  H5Aclose( attrNetwork );
  hid_t attrModel = H5Acreate( fid, "source_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT);
  H5Awrite( attrModel, attrType, realpath( input_params.files.source_file, NULL ) );
  H5Aclose( attrModel );

  H5Tclose( attrType );
  H5Sclose( simpleDataspace );

  // Define chunk property
  hsize_t     chunk_dims[ ROUTE_DATASET_RANK ] = { 1, 1, input_params.output.n_output_species, N_OUTPUT_ROUTES };
  hid_t prop_id = H5Pcreate(H5P_DATASET_CREATE);
  H5Pset_chunk(prop_id, ABUNDANCE_DATASET_RANK , chunk_dims);

  // Create dataset
  dataset = H5Dcreate(fid, "Abundances", datatype, dataspace, H5P_DEFAULT, prop_id, H5P_DEFAULT);

  int i;
  hid_t dataspaceRoute, route_t_datatype, r_t_datatype, route_prop_id, routeGroup;
  hid_t routeDatasets[ input_params.output.n_output_species ];
  if (input_params.output.trace_routes)
    {

      // Create route dataspace
      dataspaceRoute = H5Screate_simple( ROUTE_DATASET_RANK, dimsf, NULL);

      // Create route datatype
      r_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(r_t));
      H5Tinsert( r_t_datatype, "reaction_number", HOFFSET(r_t, reaction_no ), H5T_NATIVE_INT);
      H5Tinsert( r_t_datatype, "reaction_rate", HOFFSET(r_t, rate), H5T_NATIVE_DOUBLE);

      route_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(rout_t));
      H5Tinsert( route_t_datatype, "formation_rate", HOFFSET(rout_t, formation ), r_t_datatype );
      H5Tinsert( route_t_datatype, "destruction_rate", HOFFSET(rout_t, destruction ), r_t_datatype );

      // Define route chunk property
      route_prop_id = H5Pcreate(H5P_DATASET_CREATE);
      H5Pset_chunk( route_prop_id, ROUTE_DATASET_RANK, chunk_dims);


      // Create each named route dataset
      routeGroup = H5Gcreate( fid, "Routes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
      char routeName[6] = "route_";
      char tempName[ MAX_CHAR_SPECIES + sizeof( routeName ) ];
      for( i = 0; i < input_params.output.n_output_species ; i++ )
        {
          strcpy( tempName, routeName );
          strcat( tempName, network.species[input_params.output.output_species_idx[i]].name );
          routeDatasets[i] = H5Dcreate( routeGroup, tempName, route_t_datatype, dataspaceRoute, H5P_DEFAULT, route_prop_id, H5P_DEFAULT);
        }
    }
  // Timesteps and species
  hsize_t n_ts = source_mdl.ts.n_time_steps;
  hsize_t n_species =  input_params.output.n_output_species ;
  tsDataspace = H5Screate_simple( 1, &n_ts, NULL);
  speciesDataspace = H5Screate_simple( 1, &n_species, NULL);
  speciesType = H5Tcopy (H5T_C_S1);
  H5Tset_size (speciesType, MAX_CHAR_SPECIES );
  H5Tset_strpad(speciesType,H5T_STR_NULLTERM);

  // Create ts and species datasets
  tsDataset = H5Dcreate(fid, "TimeSteps", datatype, tsDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  speciesDataset = H5Dcreate(fid, "Species", speciesType, speciesDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  double* convTs = (double*) malloc( sizeof(double)* source_mdl.ts.n_time_steps );
  for( i=0; i< source_mdl.ts.n_time_steps; i++ )
    {
      convTs[i] = source_mdl.ts.time_steps[i] / CONST_MKSA_YEAR;
    }

  H5Dwrite( tsDataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, convTs );


  char speciesName [ input_params.output.n_output_species ][ MAX_CHAR_SPECIES ];
  for( i = 0; i < input_params.output.n_output_species ; i++ )
    {
      strcpy( speciesName[i], network.species[input_params.output.output_species_idx[i]].name );
    }

  H5Dwrite( speciesDataset, speciesType, H5S_ALL, H5S_ALL, H5P_DEFAULT, speciesName );

  free( convTs );
  H5Dclose( tsDataset );
  H5Dclose( speciesDataset );
  H5Tclose( speciesType );
  H5Sclose( tsDataspace );
  H5Sclose( speciesDataspace );


#ifdef HAVE_OPENMP
  /*Initialize lock*/
  omp_init_lock(&lock);


  /* Solve the ODE system for each cell. */
    {
#pragma omp parallel for schedule (dynamic, 1)
#endif
      for (cell_index = 0; cell_index < source_mdl.n_cells; cell_index++)
        {
          if (verbose >= 1)
            fprintf (stdout, "Computing abundances in cell %d...\n",
                     cell_index);
          if( full_solve ( fid, dataset, routeDatasets, dataspace, dataspaceRoute, datatype, route_t_datatype, cell_index, &input_params, source_mdl.mode,
                       &source_mdl.cell[cell_index], &network, &source_mdl.ts, verbose) != EXIT_SUCCESS )
            {
	      exit (EXIT_FAILURE);
            }
          if (verbose >= 1)
            fprintf (stdout, "Done with cell %d.\n", cell_index);
        }
#ifdef HAVE_OPENMP
    }


  /*Finished lock mechanism, destroy it*/
  omp_destroy_lock(&lock);
#endif

  /*
   * Close/release hdf5 resources.
   */
  if (input_params.output.trace_routes)
    {


      for( i = 0; i <  input_params.output.n_output_species ; i++ )
        {
          H5Dclose(routeDatasets[i] );
        }
      H5Sclose(dataspaceRoute);
      H5Gclose(routeGroup);
      H5Pclose(route_prop_id);
      H5Tclose(r_t_datatype);
      H5Tclose(route_t_datatype);
    }
  H5Dclose(dataset);
  H5Pclose(prop_id);
  H5Sclose(dataspace);
  H5Tclose(datatype);

  H5Fclose(fid);

  free_input (&input_params);
  free_mdl (&source_mdl);
  free_network (&network);
  return (EXIT_SUCCESS);
}