示例#1
0
inline DataSpace DataSpace::clone() const{
    DataSpace res;
    if ( (res._hid = H5Scopy(_hid)) < 0){
         throw DataSpaceException("Unable to copy dataspace");
    }
    return res;
}
示例#2
0
文件: H5Sf.c 项目: MattNapsAlot/rHDF5
int_f
nh5scopy_c( hid_t_f *space_id , hid_t_f *new_space_id)
{
  int ret_value = 0;
  hid_t c_new_space_id;
  hid_t c_space_id;

  c_space_id = *space_id;
  c_new_space_id = H5Scopy(c_space_id);
  if ( c_new_space_id < 0  ) ret_value = -1;

  *new_space_id = (hid_t_f)c_new_space_id;
  return ret_value;
}
示例#3
0
int main (void)
{
     hid_t   file1, file2, dataset1, dataset2;
     hid_t   mid1, mid2, fid1, fid2;
     hsize_t fdim[] = {DIM1, DIM2};
     hsize_t mdim[] = {DIM1, DIM2};
     hsize_t start[2], stride[2], count[2], block[2];
     int buf1[DIM1][DIM2];
     int buf2[DIM1][DIM2];
     int bufnew[DIM1][DIM2];
     int val[] = {53, 59};
     hsize_t marray[] = {2};
     hsize_t coord[NUMP][RANK];
     herr_t ret;
     uint  i, j;

/***********************************************************************/
/*                                                                     */
/* Create two files containing identical datasets. Write 0's to one    */
/* and 1's to the other.                                               */
/*                                                                     */
/***********************************************************************/

     for ( i = 0; i < DIM1; i++ ) 
         for ( j = 0; j < DIM2; j++ )
             buf1[i][j] = 0;

     for ( i = 0; i < DIM1; i++ ) 
         for ( j = 0; j < DIM2; j++ )
             buf2[i][j] = 1;

     file1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
     file2 = H5Fcreate(FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

     fid1 = H5Screate_simple (RANK, fdim, NULL);
     fid2 = H5Screate_simple (RANK, fdim, NULL);

     dataset1 = H5Dcreate (file1, "Copy1", H5T_NATIVE_INT, fid1, H5P_DEFAULT);
     dataset2 = H5Dcreate (file2, "Copy2", H5T_NATIVE_INT, fid2, H5P_DEFAULT);

     ret = H5Dwrite(dataset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1);
     ret = H5Dwrite(dataset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2);

     ret = H5Dclose (dataset1);
     ret = H5Dclose (dataset2);

     ret = H5Sclose (fid1);
     ret = H5Sclose (fid2);

     ret = H5Fclose (file1);
     ret = H5Fclose (file2);

/***********************************************************************/
/*                                                                     */
/* Open the two files.  Select two points in one file, write values to */
/* those point locations, then do H5Scopy and write the values to the  */
/* other file.  Close files.                                           */
/*                                                                     */
/***********************************************************************/
 
     file1 = H5Fopen (FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
     file2 = H5Fopen (FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
     dataset1 = H5Dopen (file1, "Copy1");
     dataset2 = H5Dopen (file2, "Copy2");
     fid1 = H5Dget_space (dataset1);
     mid1 = H5Screate_simple(1, marray, NULL); 
     coord[0][0] = 0; coord[0][1] = 3;
     coord[1][0] = 0; coord[1][1] = 1;

     ret = H5Sselect_elements (fid1, H5S_SELECT_SET, NUMP, (const hsize_t **)coord);

     ret = H5Dwrite (dataset1, H5T_NATIVE_INT, mid1, fid1, H5P_DEFAULT, val); 

     fid2 = H5Scopy (fid1);

     ret = H5Dwrite (dataset2, H5T_NATIVE_INT, mid1, fid2, H5P_DEFAULT, val); 

     ret = H5Dclose (dataset1);
     ret = H5Dclose (dataset2);
     ret = H5Sclose (fid1);
     ret = H5Sclose (fid2);
     ret = H5Fclose (file1);
     ret = H5Fclose (file2);
     ret = H5Sclose (mid1);

/***********************************************************************/
/*                                                                     */
/* Open both files and print the contents of the datasets.             */
/*                                                                     */
/***********************************************************************/

     file1 = H5Fopen (FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
     file2 = H5Fopen (FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
     dataset1 = H5Dopen (file1, "Copy1");
     dataset2 = H5Dopen (file2, "Copy2");

     ret = H5Dread (dataset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                    H5P_DEFAULT, bufnew);
     
     printf ("\nDataset 'Copy1' in file 'copy1.h5' contains: \n");
     for (i=0;i<DIM1; i++) {
        for (j=0;j<DIM2;j++) printf ("%3d  ", bufnew[i][j]);
        printf("\n");
     }

     printf ("\nDataset 'Copy2' in file 'copy2.h5' contains: \n");

     ret = H5Dread (dataset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                    H5P_DEFAULT, bufnew);

     for (i=0;i<DIM1; i++) {
        for (j=0;j<DIM2;j++) printf ("%3d  ", bufnew[i][j]);
        printf("\n");
     }
     ret = H5Dclose (dataset1);
     ret = H5Dclose (dataset2);
     ret = H5Fclose (file1);
     ret = H5Fclose (file2);

}
示例#4
0
void H5Attribute::copy(const hid_t src, const hid_t dest, const std::string & name)
{
    hid_t type, stype;
    hid_t space, sspace;
    char * data = 0;
    hsize_t size;
    hsize_t * dims = 0;
    hsize_t ndims;

    sspace = H5Aget_space(src);
    if (sspace < 0)
    {
        throw H5Exception(__LINE__, __FILE__, _("Cannot copy the attribute"));
    }
    space = H5Scopy(sspace);
    H5Sclose(sspace);

    stype = H5Aget_type(src);
    if (stype < 0)
    {
        H5Sclose(space);
        throw H5Exception(__LINE__, __FILE__, _("Cannot copy the attribute"));
    }
    type = H5Tcopy(stype);
    H5Tclose(stype);

    size = H5Tget_size(type);
    dims = new hsize_t[__SCILAB_HDF5_MAX_DIMS__];
    ndims = H5Sget_simple_extent_dims(space, dims, 0);
    for (unsigned int i = 0; i < ndims; i++)
    {
        size *= dims[i];
    }

    data = new char[size];
    if (H5Aread(src, type, data) < 0)
    {
        H5Sclose(space);
        H5Tclose(type);
        delete[] dims;
        delete[] data;

        throw H5Exception(__LINE__, __FILE__, _("Cannot read attribute data."));
    }

    try
    {
        hid_t attr = create(dest, name, type, type, space, space, data);
        H5Aclose(attr);
        H5Sclose(space);
        H5Tclose(type);
        delete[] dims;
        delete[] data;
    }
    catch (const H5Exception & /*e*/)
    {
        H5Sclose(space);
        H5Tclose(type);
        delete[] dims;
        delete[] data;
        throw;
    }
}
med_err _MEDselectAllEntitiesNoI(const med_idt          fid,
				 const med_int          nentity,
				 const med_int          nvaluesperentity,
				 const med_int          nconstituentpervalue,
				 const med_int          constituentselect,
				 med_filter*    const   filter) {

  med_idt    _memspace[1]={0},_diskspace[1]={0};
  med_size   _memspacesize[1],_diskspacesize[1];
  med_size   _start_mem[1]={0};
  med_err    _ret=-1;
  med_size   _onedimallvaluesdiskoffset[1]={0};
  med_size   _profilearraysize[1]={0};

  /*TODO : propager cette modif aux autres filtres*/
  if ( ! nentity) {
    _memspace[0]  = H5Screate(H5S_NULL);
    _diskspace[0] = H5Screate(H5S_NULL);
    goto _CREATE_FILTER;
  }

  _onedimallvaluesdiskoffset[0] = nentity*nvaluesperentity;
  _memspacesize[0]  = _onedimallvaluesdiskoffset[0]*nconstituentpervalue;
  _diskspacesize[0] = _memspacesize[0];

  if ( (_memspace[0] = H5Screate_simple (1, _memspacesize, NULL)) <0) {
    MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_MEMSPACE,MED_ERR_SIZE_MSG);
    ISCRUTE_size(*_memspacesize);
    ISCRUTE_int(_memspace[0]);
    goto ERROR;
  }


  if ( constituentselect != MED_ALL_CONSTITUENT) {
    _start_mem[0] = (constituentselect-1)*_onedimallvaluesdiskoffset[0];
    _profilearraysize[0]=_onedimallvaluesdiskoffset[0];
  } else {
    _start_mem[0]       =  0;
    _profilearraysize[0]=_memspacesize[0];
  };

  if ( H5Sselect_hyperslab (_memspace[0], H5S_SELECT_SET, _start_mem, NULL,
			    _profilearraysize, NULL) <0 ) {
    MED_ERR_(_ret,MED_ERR_SELECT,MED_ERR_MEMSPACE,MED_ERR_ID_MSG);
    ISCRUTE_id(_memspace[0]);
    goto ERROR;
  }
/*   ISCRUTE_int(_memspace[0]); */


  if ( (_diskspace[0] = H5Scopy(_memspace[0])) < 0) {
    MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_DISKSPACE,MED_ERR_SIZE_MSG);
    ISCRUTE_size(*_memspacesize);
    ISCRUTE_id(_memspace[0]);
    ISCRUTE_id(_diskspace[0]);
    goto ERROR;
  }

 _CREATE_FILTER:
  if ( _MEDsetFilter(1,_memspace, _diskspace, nentity,
		     nvaluesperentity, nconstituentpervalue,
		     constituentselect, MED_NO_INTERLACE,
		     MED_NO_FILTER_SIZE, MED_NO_PROFILE_SIZE,
		     MED_UNDEF_STMODE, MED_NO_PROFILE, filter ) <0) {
    MED_ERR_(_ret,MED_ERR_INIT,MED_ERR_FILTER,"");
    goto ERROR;
  }

  _ret = 0;

 ERROR:

  return _ret;
}
示例#6
0
/*-------------------------------------------------------------------------
 * Function:	test_3
 *
 * Purpose:	Tests writing to an external file set.
 *
 * Return:	Success:	0
 *
 * 		Failure:	number of errors
 *
 * Programmer:	Robb Matzke
 *              Wednesday, March  4, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_3 (hid_t fapl)
{
    hid_t	file=-1;		/*file to which to write	*/
    hid_t	dcpl=-1;		/*dataset creation properties	*/
    hid_t	mem_space=-1;		/*memory data space		*/
    hid_t	file_space=-1;		/*file data space		*/
    hid_t	dset=-1;		/*dataset			*/
    unsigned	i;			/*miscellaneous counters	*/
    int		fd;			/*external file descriptor	*/
    int	    part[25],whole[100];	/*raw data buffers		*/
    hsize_t	cur_size=100;		/*current data space size	*/
    hsize_t	max_size=200;		/*maximum data space size	*/
    hsize_t	hs_start=100;		/*hyperslab starting offset	*/
    hsize_t	hs_count=100;		/*hyperslab size		*/
    char	filename[1024];		/*file name			*/
	int temparray[10] = {0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f};

    TESTING("write external dataset");

    /* Create another file */
    h5_fixname(FILENAME[2], fapl, filename, sizeof filename);
    if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) {
	goto error;
    }

    /* Create the external file list */
    if((dcpl=H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
    if (H5Pset_external(dcpl, "extern_1b.raw", (off_t)0, (hsize_t)sizeof part) < 0 ||
	H5Pset_external(dcpl, "extern_2b.raw", (off_t)10, (hsize_t)sizeof part) < 0 ||
	H5Pset_external(dcpl, "extern_3b.raw", (off_t)20, (hsize_t)sizeof part) < 0 ||
	H5Pset_external(dcpl, "extern_4b.raw", (off_t)30, H5F_UNLIMITED) < 0)
	goto error;

    /* Make sure the output files are fresh*/
    for (i=1; i<=4; i++) {
	sprintf(filename, "extern_%db.raw", i);
	if ((fd= HDopen(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
	    H5_FAILED();
	    printf("    cannot open %s: %s\n", filename, strerror(errno));
	    goto error;
	}

	HDwrite(fd, temparray, (i-1)*10);
	HDclose(fd);
    }

    /* Create the dataset */
    if((mem_space = H5Screate_simple(1, &cur_size, &max_size)) < 0) goto error;
    if((file_space = H5Scopy(mem_space)) < 0) goto error;
    if((dset = H5Dcreate2(file, "dset1", H5T_NATIVE_INT, file_space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0)
	goto error;

    /* Write the entire dataset and compare with the original */
    for(i = 0; i < cur_size; i++)
        whole[i] = i;
    if(H5Dwrite(dset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, whole) < 0) goto error;
    for(i = 0; i < 4; i++) {
	char name1[64], name2[64];

	sprintf(name1, "extern_%da.raw", i + 1);
	sprintf(name2, "extern_%db.raw", i + 1);
	if(!same_contents(name1, name2)) {
	    H5_FAILED();
	    puts ("   Output differs from expected value.");
	    goto error;
	} /* end if */
    } /* end for */

    /* Extend the dataset by another 100 elements */
    if(H5Dset_extent(dset, &max_size) < 0) goto error;
    if(H5Sclose(file_space) < 0) goto error;
    if((file_space = H5Dget_space(dset)) < 0) goto error;

    /* Write second half of dataset */
    for(i = 0; i < hs_count; i++)
        whole[i] = 100 + i;
    if(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, &hs_start, NULL, &hs_count, NULL) < 0) goto error;
    if(H5Dwrite(dset, H5T_NATIVE_INT, mem_space, file_space, H5P_DEFAULT, whole) < 0) goto error;


    if(H5Dclose(dset) < 0) goto error;
    if(H5Pclose(dcpl) < 0) goto error;
    if(H5Sclose(mem_space) < 0) goto error;
    if(H5Sclose(file_space) < 0) goto error;
    if(H5Fclose(file) < 0) goto error;

    PASSED();
    return 0;

 error:
    H5E_BEGIN_TRY {
	H5Dclose(dset);
	H5Pclose(dcpl);
	H5Sclose(mem_space);
	H5Sclose(file_space);
	H5Fclose(file);
    } H5E_END_TRY;
    return 1;
}
示例#7
0
/*-------------------------------------------------------------------------
 * Function:	test_2
 *
 * Purpose:	Tests reading from an external file set.
 *
 * Return:	Success:	0
 *
 * 		Failure:	number of errors
 *
 * Programmer:	Robb Matzke
 *              Wednesday, March  4, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_2 (hid_t fapl)
{
    hid_t	file=-1;		/*file to write to		*/
    hid_t	dcpl=-1;		/*dataset creation properties	*/
    hid_t	space=-1;		/*data space			*/
    hid_t	dset=-1;		/*dataset			*/
    hid_t	grp=-1;			/*group to emit diagnostics	*/
    int		fd;			/*external file descriptors	*/
    size_t	i, j;			/*miscellaneous counters	*/
    hssize_t	n;			/*bytes of I/O			*/
    char	filename[1024];		/*file names			*/
    int		part[25], whole[100];	/*raw data buffers		*/
    hsize_t	cur_size;		/*current data space size	*/
    hid_t	hs_space;		/*hyperslab data space		*/
    hsize_t	hs_start = 30;		/*hyperslab starting offset	*/
    hsize_t	hs_count = 25;		/*hyperslab size		*/
	int temparray[10] = {0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f,0x0f0f0f0f};

    TESTING("read external dataset");

    /* Write the data to external files directly */
    for (i=0; i<4; i++) {
	for (j=0; j<25; j++) {
	    part[j] = (int)(i*25+j);
	}
	sprintf (filename, "extern_%lua.raw", (unsigned long)i+1);
	fd = HDopen(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
	assert (fd>=0);
/*	n = lseek (fd, (off_t)(i*10), SEEK_SET);
*/
	n = HDwrite(fd,temparray,(size_t)i*10);
	assert (n>=0 && (size_t)n==i*10);
	n = HDwrite(fd, part, sizeof(part));
	assert (n==sizeof(part));
	HDclose(fd);
    }

    /*
     * Create the file and an initial group.  This causes messages about
     * debugging to be emitted before we start playing games with what the
     * output looks like.
     */
    h5_fixname(FILENAME[1], fapl, filename, sizeof filename);
    if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) FAIL_STACK_ERROR
    if((grp = H5Gcreate2(file, "emit-diagnostics", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR
    if(H5Gclose(grp) < 0) FAIL_STACK_ERROR

    /* Create the dataset */
    if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error;
    if(H5Pset_external(dcpl, "extern_1a.raw",  (off_t)0, (hsize_t)sizeof part) < 0 ||
            H5Pset_external(dcpl, "extern_2a.raw", (off_t)10, (hsize_t)sizeof part) < 0 ||
            H5Pset_external(dcpl, "extern_3a.raw", (off_t)20, (hsize_t)sizeof part) < 0 ||
            H5Pset_external(dcpl, "extern_4a.raw", (off_t)30, (hsize_t)sizeof part) < 0)
	goto error;
    cur_size = 100;
    if((space = H5Screate_simple(1, &cur_size, NULL)) < 0) goto error;
    if((dset = H5Dcreate2(file, "dset1", H5T_NATIVE_INT, space, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) goto error;

    /*
     * Read the entire dataset and compare with the original
     */
    memset(whole, 0, sizeof(whole));
    if(H5Dread(dset, H5T_NATIVE_INT, space, space, H5P_DEFAULT, whole) < 0) goto error;
    for(i = 0; i < 100; i++)
	if(whole[i] != (signed)i) {
	    H5_FAILED();
	    puts("    Incorrect value(s) read.");
	    goto error;
	} /* end if */

    /*
     * Read the middle of the dataset
     */
    if((hs_space = H5Scopy(space)) < 0) goto error;
    if(H5Sselect_hyperslab(hs_space, H5S_SELECT_SET, &hs_start, NULL,
			    &hs_count, NULL) < 0) goto error;
    HDmemset(whole, 0, sizeof(whole));
    if(H5Dread(dset, H5T_NATIVE_INT, hs_space, hs_space, H5P_DEFAULT,
		 whole) < 0) goto error;
    if(H5Sclose(hs_space) < 0) goto error;
    for(i = hs_start; i<hs_start+hs_count; i++) {
	if(whole[i] != (signed)i) {
	    H5_FAILED();
	    puts("    Incorrect value(s) read.");
	    goto error;
	}
    }

    if (H5Dclose(dset) < 0) goto error;
    if (H5Pclose(dcpl) < 0) goto error;
    if (H5Sclose(space) < 0) goto error;
    if (H5Fclose(file) < 0) goto error;
    PASSED();
    return 0;

 error:
    H5E_BEGIN_TRY {
	H5Dclose(dset);
	H5Pclose(dcpl);
	H5Sclose(space);
	H5Fclose(file);
    } H5E_END_TRY;
    return 1;
}
示例#8
0
int
full_solve (hid_t fid, hid_t dataset, hid_t* routeDatasets, hid_t dataspace, hid_t routeDataspace, hid_t datatype, hid_t routeDatatype, int cell_index, const inp_t * input_params, SOURCE_MODE mode,
            const cell_table_t * cell, const net_t * network, const time_steps_t * ts,
            int verbose)
{

  double *abundances = NULL;
  alloc_abundances( network, &abundances ); // Allocate the abundances array; it contains all species.

  rout_t* routes = NULL;
  if (( routes =
        malloc (sizeof (rout_t) *  input_params->output.n_output_species * N_OUTPUT_ROUTES)) == NULL)
    {
      fprintf (stderr, "astrochem: %s:%d: routes allocation failed.\n",
               __FILE__, __LINE__);
      return EXIT_SUCCESS;
    }

  double* output_abundances = NULL;
  if (( output_abundances =
        malloc (sizeof (double) * input_params->output.n_output_species )) == NULL)
    {
      fprintf (stderr, "astrochem: %s:%d: array allocation failed.\n",
               __FILE__, __LINE__);
      return EXIT_FAILURE;
    }

#ifdef HAVE_OPENMP
              omp_set_lock(&lock);
#endif


  // Create the memory dataspace, selecting all output abundances
  hsize_t size = input_params->output.n_output_species;
  hid_t memDataspace = H5Screate_simple(1, &size, NULL);

  // Create the file dataspace, and prepare selection of a chunk of the file
  hid_t fileDataspace = H5Scopy(dataspace);
  hsize_t     count[3]={  1, 1,  input_params->output.n_output_species };

  hsize_t routeSize[2] = { input_params->output.n_output_species, N_OUTPUT_ROUTES };
  hsize_t     routeCount[4]={  1, 1,  input_params->output.n_output_species, N_OUTPUT_ROUTES };
  hid_t routeFileDataspace, routeMemDataspace;
  if (input_params->output.trace_routes)
    {
      // Create the route memory dataspace, selecting all output routes
      routeMemDataspace = H5Screate_simple(2, routeSize, NULL);
      // Create the route file dataspace, and prepare selection of a chunk of the file
      routeFileDataspace = H5Scopy(routeDataspace);
    }

#ifdef HAVE_OPENMP
              omp_unset_lock(&lock);
#endif


  // Initializing abundance
#if 0 //Ultra complicated code
  const species_name_t* species = malloc( input_params->abundances.n_initial_abundances * sizeof(*species));
  double *initial_abundances = malloc( input_params->abundances.n_initial_abundances * sizeof(double) );

  int i;
  for( i = 0; i <  input_params->abundances.n_initial_abundances ; i++ )
    {
      strcpy( network->species_names[input_params->abundances.initial_abundances[i].species_idx ] , species[i] );
      initial_abundances[i] = input_params->abundances.initial_abundances[i].abundance;
    }
  set_initial_abundances( species, 3, initial_abundances, &network, abundances); // Set initial abundances
#else // same thing , without using api
  int i;
  for( i = 0; i <  input_params->abundances.n_initial_abundances ; i++ )
    {
      abundances[ input_params->abundances.initial_abundances[i].species_idx ] = input_params->abundances.initial_abundances[i].abundance;
    }
    
    // Add grain abundances
    int g, gm, gp;
    double gabs;
    g = find_species ("grain", network);
    gm = find_species ("grain(-)", network);
    gp = find_species ("grain(+)", network);
    
    // Check if grain abundances have already been initialized one way or another
    gabs=0.0;
    if(g>=0) gabs += abundances[ g ];
    if(gm>=0) gabs += abundances[ gm ];
    if(gp>=0) gabs += abundances[ gp ];
    
    if(gabs == 0.0) {
    	// Grains have not been initialized
    	// Check that grains are defined in our network, and if so, set the grain abundance
    	if(g>=0)
    		abundances[ g ] = input_params->phys.grain_abundance;
    }
#endif


  double min_nh;                 /* Minimum density */

  /* Compute the minimum density to set the absolute tolerance of the
     solver */
  min_nh = cell->nh[0];
  if (mode == DYNAMIC)
    {
      int i;

      for (i = 1; i < ts->n_time_steps; i++)
        {
          if (cell->nh[i] < min_nh)
            {
              min_nh = cell->nh[i];
            }
        }
    }

  astrochem_mem_t astrochem_mem;
  cell_t cell_unik;
  cell_unik.av = cell->av[0];
  cell_unik.nh = cell->nh[0];
  cell_unik.tgas = cell->tgas[0];
  cell_unik.tdust = cell->tdust[0];
  if( solver_init( &cell_unik, network, &input_params->phys, abundances, min_nh, input_params->solver.abs_err,  input_params->solver.rel_err, &astrochem_mem ) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }
  else
    {
      int i, j;

      /* Solve the system for each time step. */
      for (i = 0; i < ts->n_time_steps; i++)
        {


          if (i!=0 && mode == DYNAMIC)
            {
              cell_unik.av = cell->av[i];
              cell_unik.nh = cell->nh[i];
              cell_unik.tgas = cell->tgas[i];
              cell_unik.tdust = cell->tdust[i];

              if( solve( &astrochem_mem, network, abundances,  ts->time_steps[i], &cell_unik, verbose ) != EXIT_SUCCESS )
                {
                  return EXIT_FAILURE;
                }
            }
          else
            {
              if( solve( &astrochem_mem, network, abundances,  ts->time_steps[i], NULL, verbose ) != EXIT_SUCCESS )
                {
                  return EXIT_FAILURE;
                }
            }


          /* Fill the array of abundances with the output species
             abundances. Ignore species that are not in the
             network. Abundance that are lower than MIN_ABUNDANCES are
             set to 0. */

          for (j = 0; j < input_params->output.n_output_species; j++)
            {
              if (mode == STATIC)
                {
                  output_abundances[j] =
                   (double) NV_Ith_S (astrochem_mem.y, input_params->output.output_species_idx[j]) / cell->nh[0];
                }
              else
                {
                  output_abundances[j] =
                   (double) NV_Ith_S (astrochem_mem.y, input_params->output.output_species_idx[j]) / cell->nh[i];
                }
              if (output_abundances[j] < MIN_ABUNDANCE)
                output_abundances[j] = 0.;

#ifdef HAVE_OPENMP
              omp_set_lock(&lock);
#endif
              // Select a chunk of the file
              hsize_t     start[3]={  cell_index, i, 0 };
              H5Sselect_hyperslab( fileDataspace, H5S_SELECT_SET, start, NULL, count , NULL );

              // Write the chunk
              H5Dwrite(dataset, datatype, memDataspace, fileDataspace, H5P_DEFAULT,
                       output_abundances );

#ifdef HAVE_OPENMP
              omp_unset_lock(&lock);
#endif

            }

          /* Compute the rate of each formation/destruction route for
             each output specie. */

          if (input_params->output.trace_routes)
            {
              for (j = 0; j < input_params->output.n_output_species; j++)
                {
                  int k;
                  int l;
                  for (l = 0; l < N_OUTPUT_ROUTES; l++)
                    {
                      routes[ j*N_OUTPUT_ROUTES + l ].formation.rate = 0;
                      routes[ j*N_OUTPUT_ROUTES + l ].destruction.rate = 0;
                    }
                  for (k = 0; k < network->n_reactions; k++)
                    {
                      /* If the species is a product of the
                         reaction then compute the formation
                         rate. If the rate is greater than the
                         smallest rate in the formation route
                         structure, we add the current reaction
                         number and rate to that structure. */

                      bool specie_in_products = false;
                      int p;
                      for( p = 0; p < MAX_PRODUCTS; p++ )
                        {
                          if( network->reactions[k].products[p] ==  input_params->output.output_species_idx[j])
                            {
                              specie_in_products = true;
                              break;
                            }
                        }
                      if( specie_in_products )
                        {
                          r_t formation_route;
                          double min_rate;
                          unsigned int min_rate_index;
                          if (network->reactions[k].reaction_type == 0)
                            {
                              formation_route.rate = astrochem_mem.params.reac_rates[k];
                              formation_route.rate *=
                               NV_Ith_S (astrochem_mem.y, network->reactions[k].reactants[0]);
                            }
                          else if (network->reactions[k].reaction_type == 23)
                            {
                              formation_route.rate = astrochem_mem.params.reac_rates[k];
                            }
                          else
                            {
                              formation_route.rate = astrochem_mem.params.reac_rates[k];
                              int r;
                              for( r = 0; r < MAX_REACTANTS; r++ )
                                {
                                  if( network->reactions[k].reactants[r] != -1 )
                                    {
                                      formation_route.rate *=
                                       NV_Ith_S (astrochem_mem.y, network->reactions[k].reactants[r]);
                                    }
                                }
                            }
                          formation_route.reaction_no =
                           network->reactions[k].reaction_no;
                          min_rate = routes[ j*N_OUTPUT_ROUTES  ].formation.rate;
                          min_rate_index = 0;
                          for (l = 1; l < N_OUTPUT_ROUTES; l++)
                            {
                              if (routes[ j*N_OUTPUT_ROUTES + l ].formation.rate <
                                  min_rate)
                                {
                                  min_rate =
                                   routes[ j*N_OUTPUT_ROUTES + l ].formation.rate;
                                  min_rate_index = (unsigned int) l;
                                }
                            }
                          if (formation_route.rate > min_rate)
                            {
                              routes[ j*N_OUTPUT_ROUTES + min_rate_index ].formation.rate = formation_route.rate;
                              routes[ j*N_OUTPUT_ROUTES + min_rate_index ].formation.reaction_no = formation_route.reaction_no;
                            }
                        }

                      /* If the species is reactant of the reaction
                         then compute the destruction rate. */
                      bool species_in_reactants = false;
                      int r;
                      for ( r = 0; r < MAX_REACTANTS; r++ )
                        {
                          if ( network->reactions[k].reactants[r] == input_params->output.output_species_idx[j])
                            {
                              species_in_reactants = true;
                              break;
                            }
                        }
                      if( species_in_reactants )
                        {
                          r_t destruction_route;
                          double min_rate;
                          unsigned int min_rate_index;

                          if (network->reactions[k].reaction_type == 0)
                            {
                              destruction_route.rate = astrochem_mem.params.reac_rates[k];
                              destruction_route.rate *=
                               NV_Ith_S (astrochem_mem.y, network->reactions[k].reactants[0]);
                            }
                          else if (network->reactions[k].reaction_type == 23)
                            {
                              destruction_route.rate = astrochem_mem.params.reac_rates[k];
                            }
                          else
                            {
                              destruction_route.rate = astrochem_mem.params.reac_rates[k];
                              for ( r = 0; r < MAX_REACTANTS; r++ )
                                {
                                  if (network->reactions[k].reactants[r] != -1)
                                    {
                                      destruction_route.rate *=
                                       NV_Ith_S (astrochem_mem.y, network->reactions[k].reactants[r]);
                                    }
                                }
                            }
                          destruction_route.reaction_no =
                           network->reactions[k].reaction_no;

                          min_rate = routes[ j*N_OUTPUT_ROUTES  ].destruction.rate;
                          min_rate_index = 0;
                          for (l = 1; l < N_OUTPUT_ROUTES; l++)
                            {
                              if (routes[ j*N_OUTPUT_ROUTES + l ].destruction.rate <
                                  min_rate)
                                {
                                  min_rate =
                                   routes[ j*N_OUTPUT_ROUTES + l ].destruction.rate;
                                  min_rate_index = (unsigned int) l;
                                }
                            }
                          if (destruction_route.rate > min_rate)
                            {
                              routes[ j*N_OUTPUT_ROUTES + min_rate_index ].destruction.rate = destruction_route.rate;
                              routes[ j*N_OUTPUT_ROUTES + min_rate_index ].destruction.reaction_no = destruction_route.reaction_no;
                            }
                        }
                    }
                }
#ifdef HAVE_OPENMP
              omp_set_lock(&lock);
#endif
              // Selecting a chunk of the file
              hsize_t     routeStart[4]={  cell_index, i, 0, 0 };
              H5Sselect_hyperslab( routeFileDataspace, H5S_SELECT_SET, routeStart, NULL, routeCount , NULL );

              int spec_idx;
              for( spec_idx = 0; spec_idx < input_params->output.n_output_species; spec_idx++ )
                {
                  // Writing in each route datasets
                  H5Dwrite( routeDatasets[ spec_idx ], routeDatatype, routeMemDataspace, routeFileDataspace, H5P_DEFAULT,
                            routes );
                }

#ifdef HAVE_OPENMP
              omp_unset_lock(&lock);
#endif
            }
        }

    }
#ifdef HAVE_OPENMP
  omp_set_lock(&lock);
#endif
  // Cleaning up hdf5
  H5Sclose(memDataspace);
  H5Sclose(fileDataspace);
  if (input_params->output.trace_routes)
    {
      H5Sclose(routeMemDataspace);
      H5Sclose(routeFileDataspace);
    }
#ifdef HAVE_OPENMP
  omp_unset_lock(&lock);
#endif
  // Free
  free( output_abundances );
  free( routes );
  free_abundances( abundances );
  solver_close( &astrochem_mem );
  return EXIT_SUCCESS;
}