void hdfFileSaver::saveNow(void)
{
    QDir prnt;
    herr_t status;
    hsize_t maxdims[1];
    hsize_t d_dims[1];

    int rank = 1;

    hsize_t datasetlength;
    size_t      memdims[1];
    hsize_t     count[1];              /* size of subset in the file */
      hsize_t     offset[1];             /* subset offset in the file */
      hsize_t     mcount[1];
      hsize_t     moffset[1];
hid_t prop;

    if (events==0)
        return;



        if (access(fname.toStdString().c_str(), F_OK) ==-1)
        {
            hfile = H5Fcreate(
                        fname.toStdString().c_str(),
                        H5F_ACC_EXCL ,
                        H5P_DEFAULT,
                        H5P_DEFAULT ) ;

            topgroup = H5Gcreate(
                        hfile,
                        "iqdata_raw",
                        H5P_DEFAULT,
                        H5P_DEFAULT,
                        H5P_DEFAULT);


            hid_t aid3  = H5Screate(H5S_SCALAR);
            hid_t  atype = H5Tcopy(H5T_C_S1);
            H5Tset_size(atype, 4);
            hid_t  attr3 = H5Acreate1(topgroup, "type", atype, aid3, H5P_DEFAULT);
            status = H5Awrite(attr3, atype,"dict");
            H5Aclose(attr3);
            H5Tclose(atype);
            H5Sclose(aid3);


        }
        else
        {
           //printf("File already exists, will append\n");
            hfile = H5Fopen(
                fname.toStdString().c_str(),
                H5F_ACC_RDWR,
                H5P_DEFAULT );


            topgroup = H5Gopen(hfile, "iqdata_raw", H5P_DEFAULT);

            //fprintf(stderr,"Bad Hdf file already existant, cannot open\n");
         }


        if (hfile <=0 || topgroup <=0)
        {
           fprintf(stderr,"Bad Hdf file, cannot open\n");
           return;
        }


        if (true)
        {


            //QHash<int,QHash<QString,QList<float> > > events;
            QList<int> keys1=(*events).keys();
            for (int i=0;i<keys1.length();i++)
            {
                int chan = keys1[i];

                if ((*events)[chan]["bin"].length() > 0)
                {
                    int bin = (int)((*events)[chan]["bin"][0]);
                    QString dirname = QString("keyint_%1").arg(chan);



                    //turn off errors when we query the group, using open
                    hid_t error_stack = H5Eget_current_stack();
                    H5E_auto2_t  oldfunc;
                    void *old_client_data;
                    H5Eget_auto(error_stack, &oldfunc, &old_client_data);
                    H5Eset_auto(error_stack, NULL, NULL);

                    channelgroup = H5Gopen(
                                topgroup,
                                dirname.toStdString().c_str(),
                                H5P_DEFAULT);

                    //turn errors back on.
                    H5Eset_auto(error_stack, oldfunc, old_client_data);

                    if (channelgroup<0)
                    {
                        channelgroup = H5Gcreate(
                                    topgroup,
                                    dirname.toStdString().c_str(),
                                    H5P_DEFAULT,
                                    H5P_DEFAULT,
                                    H5P_DEFAULT);



                        hid_t aid3  = H5Screate(H5S_SCALAR);
                        hid_t  atype = H5Tcopy(H5T_C_S1);
                        H5Tset_size(atype, 4);
                        hid_t  attr3 = H5Acreate1(channelgroup, "type", atype, aid3, H5P_DEFAULT);
                        status = H5Awrite(attr3, atype,"dict");
                        H5Aclose(attr3);
                        H5Tclose(atype);
                        H5Sclose(aid3);




                    }
                    if (channelgroup!=0)
                    {

                        QList<QString> keys2=(*events)[keys1[i]].keys();
                        for (int i2=0;i2<keys2.length();i2++)
                        {
                            QString dname = QString("keystr_%1").arg(keys2[i2]);
                            datasetlength = (*events)[keys1[i]][keys2[i2]].length();

                            if (datasetlength>0)
                            {

                                //Try to open dataset if it exists

                                //turn off errors when we query the group, using open
                                hid_t error_stack = H5Eget_current_stack();
                                H5E_auto2_t  oldfunc;
                                void *old_client_data;
                                H5Eget_auto(error_stack, &oldfunc, &old_client_data);
                                H5Eset_auto(error_stack, NULL, NULL);

                                //query or open dataset
                                curdataset = H5Dopen(channelgroup,dname.toStdString().c_str(),H5P_DEFAULT);

                                //turn errors back on.
                                H5Eset_auto(error_stack, oldfunc, old_client_data);

                                //if cannot open dataset, create it, and make it chunked.
                                if (curdataset<=0)
                                {

                                    //set up size info, chunks etc...

                                    maxdims[0]=H5S_UNLIMITED;
                                     rank = 1;
                                    d_dims[0]=datasetlength;

                                    curdataspace=  H5Screate_simple(rank, d_dims,maxdims);


                                     prop = H5Pcreate(H5P_DATASET_CREATE);
                                    status = H5Pset_chunk(prop, rank, d_dims);

                                    if (status) trap();

                                    curdataset = H5Dcreate(
                                        channelgroup,
                                        dname.toStdString().c_str(),
                                        H5T_NATIVE_FLOAT,
                                        curdataspace,
                                        H5P_DEFAULT,
                                        prop,
                                        H5P_DEFAULT);


                                    hid_t aid3  = H5Screate(H5S_SCALAR);
                                    hid_t  atype = H5Tcopy(H5T_C_S1);
                                    H5Tset_size(atype, 5);
                                    hid_t  attr3 = H5Acreate1(curdataset, "type", atype, aid3, H5P_DEFAULT);
                                    status = H5Awrite(attr3, atype,"array");
                                    H5Aclose(attr3);
                                    H5Tclose(atype);
                                    H5Sclose(aid3);





                                    H5Pclose(prop);

                                }//if (curdataset<=0)
                                else
                                {
                                    //get dataspace from exant dataset
                                    curdataspace = H5Dget_space(curdataset);
                                    H5Sget_simple_extent_dims(curdataspace, d_dims, maxdims);
                                    H5Sclose(curdataspace);
                                    //if dataset already exist, extend its size

                                    d_dims[0] = d_dims[0] + datasetlength;
                                    status = H5Dset_extent(curdataset, d_dims);


                                    if (status) trap();

                                    curdataspace = H5Dget_space(curdataset);
                                    H5Sget_simple_extent_dims(curdataspace, d_dims, maxdims);



                                }//else

                                if (curdataset>0)
                                {

                                  //
                                  // Set up stride, offset, count block in dastasets...
                                  //

                                 // stride[0]=1;//hop size between floats in dataset...
                                  //block[0]=1;//get 1 slab
                                  count[0]=datasetlength;//size of hyperslab or chunk of data
                                  offset[0]=d_dims[0] - datasetlength;//offset from beginning of file.


                                  status = H5Sselect_hyperslab(
                                    curdataspace,
                                    H5S_SELECT_SET,
                                    offset,
                                    NULL,
                                    count,
                                    NULL);

                                  if (status) trap();


                                  rank = 1;


                                 memdims[0]=datasetlength;

                                 memdataspace=  H5Screate_simple(rank, (const hsize_t*)memdims,NULL);

                                 mcount[0] = datasetlength;
                                 moffset[0] = 0;

                                 status = H5Sselect_hyperslab(
                                   memdataspace,
                                   H5S_SELECT_SET,
                                   moffset,
                                   NULL,
                                   mcount,
                                   NULL);

                                  if (status) trap();


                                  for (int mmm=0;mmm<datasetlength;mmm++)
                                      this->rawdata[mmm]=(*events)[keys1[i]][keys2[i2]][mmm];

                                  status = H5Dwrite(
                                   curdataset,
                                   H5T_NATIVE_FLOAT,
                                   memdataspace,
                                   curdataspace,
                                    H5P_DEFAULT,
                                        this->rawdata);

                                  if (status) trap();

                                 H5Sclose(memdataspace);
                                 H5Sclose(curdataspace);
                                 H5Dclose(curdataset);


                                } //if (curdataset>0)

                            }

                        }//for (int i2=0;i2<keys2.length();i2++)


                        status = H5Gclose(channelgroup);

                    }//if (channelgroup!=0)
                }//if ((*events)[chan]["bin"].length() > 0)
            }//for (int i=0;i<keys1.length();i++)
        }//if (true)

        status = H5Gclose(topgroup);
        status = H5Fclose(hfile);

}
Beispiel #2
0
int gal2gad2(NbodyModel *theModel, const char * prefix, const char * path,
         double length, double mass, double velocity) {
#ifdef HAS_HDF5
    int n=theModel->n;
    hid_t       file_id,group_id,header_id; 
    hid_t       hdf5_dataspace,hdf5_attribute;
    hsize_t     pdims[2];
    hsize_t     mdims[1];
    hsize_t     tdims[1];
    hsize_t     fdims[1];
    float       *positions;
    float       *velocities;
    int         *IDs;
    int         nFiles=1;
    int         Npart[6]={0,0,0,0,n,0};
    int         Npart_hw[6]={0,0,0,0,0,0};
    float       *masses;
    herr_t      status;
    int         i_zero=0;
    double      d_zero=0.0;
    double      d_one=1.0;
    int i;
    char hdf5file[100];
    char paramfile[100];
    FILE * pfile;

    sprintf(hdf5file,"%s%s.hdf5",path,prefix);
    sprintf(paramfile,"%s%s.param",path,prefix);
    positions = (float *)malloc(sizeof(float)*3*n);
    velocities = (float *)malloc(sizeof(float)*3*n);
    masses = (float *)malloc(sizeof(float)*n);
    IDs = (int *)malloc(sizeof(int)*n);

    printf("HDF5FILE BEING GENERATED\n");

    for(i=0;i<n;i++) {
        positions[i*3+0] = (float)theModel->x[i];
        positions[i*3+1] = (float)theModel->y[i];
        positions[i*3+2] = (float)theModel->z[i];
        velocities[i*3+0] = (float)theModel->vx[i];
        velocities[i*3+1] = (float)theModel->vy[i];
        velocities[i*3+2] = (float)theModel->vz[i];
        masses[i] = (float)theModel->mass[i];
        IDs[i]=i;
    }

    file_id = H5Fcreate (hdf5file,
        H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); 
    group_id = H5Gcreate1(file_id, "PartType4", 0);
    header_id = H5Gcreate1(file_id, "Header", 0);
    pdims[0] = n;
    pdims[1] = 3;
    mdims[0] = n;
    tdims[0] = 6;
    fdims[0] = 1;

    hdf5_dataspace = H5Screate(H5S_SIMPLE);
    H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL);
    hdf5_attribute = H5Acreate1(header_id, "NumPart_ThisFile",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, Npart);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SIMPLE);
    H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL);
    hdf5_attribute = H5Acreate1(header_id, "NumPart_Total",
        H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);
      
    hdf5_dataspace = H5Screate(H5S_SIMPLE);
    H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL);
    hdf5_attribute = H5Acreate1(header_id, "NumPart_Total_HW",
        H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart_hw);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Time",
        H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &time);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);


    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Redshift",
        H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "BoxSize",
        H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);
      
    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "NumFilesPerSnapshot",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &nFiles);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Omega0",
        H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "OmegaLambda",
        H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "HubbleParam",
        H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_one);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Flag_Sfr",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Flag_Cooling",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Flag_StellarAge",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Flag_Metals",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);

    hdf5_dataspace = H5Screate(H5S_SCALAR);
    hdf5_attribute = H5Acreate1(header_id, "Flag_Feedback",
        H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);
      
    hdf5_dataspace = H5Screate(H5S_SIMPLE);
    H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL);
    hdf5_attribute = H5Acreate1(header_id, "Flag_Entropy_ICs",
        H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT);
    H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart_hw);
    H5Aclose(hdf5_attribute);
    H5Sclose(hdf5_dataspace);
      
    status = H5LTmake_dataset(file_id,"PartType4/Coordinates",2,
        pdims,H5T_NATIVE_FLOAT,positions);
    status = H5LTmake_dataset(file_id,"PartType4/ParticleIDs",1,
        mdims,H5T_NATIVE_UINT,IDs);
    status = H5LTmake_dataset(file_id,"PartType4/Velocities",2,
        pdims,H5T_NATIVE_FLOAT,velocities);
    status = H5LTmake_dataset(file_id,"PartType4/Masses",1,
        mdims,H5T_NATIVE_FLOAT,masses);

    status = H5Fclose (file_id);

    free(positions);
    free(velocities);
    free(masses);
    free(IDs);

    
    pfile = fopen(paramfile,"w");
    fprintf(pfile,"InitCondFile\t%s\n",prefix);
    fprintf(pfile,"OutputDir\tout\n");
    fprintf(pfile,"EnergyFile\tenergy.txt\n");
    fprintf(pfile,"InfoFile\tinfo.txt\n");
    fprintf(pfile,"TimingsFile\ttimings.txt\n");
    fprintf(pfile,"CpuFile\tcpu.txt\n");
    fprintf(pfile,"RestartFile\trestart\n");
    fprintf(pfile,"SnapshotFileBase\tsnapshot\n");
    fprintf(pfile,"OutputListFilename\toutput_list.txt\n");
    fprintf(pfile,"ICFormat\t3\n");
    fprintf(pfile,"SnapFormat\t3\n");
    fprintf(pfile,"TypeOfTimestepCriterion\t0\n");
    fprintf(pfile,"OutputListOn\t0\n");
    fprintf(pfile,"PeriodicBoundariesOn\t0\n");
    fprintf(pfile,"TimeBegin\t0.0\n");
    fprintf(pfile,"TimeMax\t%le\n",theModel->tFinal);
    fprintf(pfile,"Omega0\t0\n");
    fprintf(pfile,"OmegaLambda\t0\n");
    fprintf(pfile,"OmegaBaryon\t0\n");
    fprintf(pfile,"HubbleParam\t1.0\n");
    fprintf(pfile,"BoxSize\t0\n");
    fprintf(pfile,"TimeBetSnapshot\t%le\n",theModel->tFinal/100); //change this
    fprintf(pfile,"TimeOfFirstSnapshot\t0\n");
    fprintf(pfile,"CpuTimeBetRestartFile\t300.0\n");
    fprintf(pfile,"TimeBetStatistics\t0.1\n");
    fprintf(pfile,"NumFilesPerSnapshot\t1\n");
    fprintf(pfile,"NumFilesWrittenInParallel\t1\n");
    fprintf(pfile,"ErrTolIntAccuracy\t0.0025\n");
    fprintf(pfile,"CourantFac\t0.15\n");
    fprintf(pfile,"MaxSizeTimestep\t0.01\n");
    fprintf(pfile,"MinSizeTimestep\t0.0\n");
    fprintf(pfile,"ErrTolTheta\t0.05\n");
    fprintf(pfile,"TypeOfOpeningCriterion\t1\n");
    fprintf(pfile,"ErrTolForceAcc\t0.0005\n");
    fprintf(pfile,"TreeDomainUpdateFrequency\t0.01\n");
    fprintf(pfile,"DesNumNgb\t50\n");
    fprintf(pfile,"MaxNumNgbDeviation\t2\n");
    fprintf(pfile,"ArtBulkViscConst\t0.8\n");
    fprintf(pfile,"InitGasTemp\t0\n");
    fprintf(pfile,"MinGasTemp\t0\n");
    fprintf(pfile,"PartAllocFactor\t1.5\n");
    fprintf(pfile,"TreeAllocFactor\t0.8\n");
    fprintf(pfile,"BufferSize\t25\n");
    fprintf(pfile,"UnitLength_in_cm\t%le  \n",length);
    fprintf(pfile,"UnitMass_in_g\t%le    \n",mass);
    fprintf(pfile,"UnitVelocity_in_cm_per_s\t%le  \n",velocity);
    fprintf(pfile,"GravityConstantInternal\t0\n");
    fprintf(pfile,"MinGasHsmlFractional\t0.25\n");
    fprintf(pfile,"SofteningGas\t0\n");
    fprintf(pfile,"SofteningHalo\t1.0\n");
    fprintf(pfile,"SofteningDisk\t0.4\n");
    fprintf(pfile,"SofteningBulge\t0\n");
    fprintf(pfile,"SofteningStars\t1.0e-2\n");
    fprintf(pfile,"SofteningBndry\t0\n");
    fprintf(pfile,"SofteningGasMaxPhys\t0\n");
    fprintf(pfile,"SofteningHaloMaxPhys\t1.0\n");
    fprintf(pfile,"SofteningDiskMaxPhys\t0.4\n");
    fprintf(pfile,"SofteningBulgeMaxPhys\t0\n");
    fprintf(pfile,"SofteningStarsMaxPhys\t1.0e-2\n");
    fprintf(pfile,"SofteningBndryMaxPhys\t0\n");
    fprintf(pfile,"MaxRMSDisplacementFac\t0.2\n");
    fprintf(pfile,"TimeLimitCPU\t36000\n");
    fprintf(pfile,"ResubmitOn\t0\n");
    fprintf(pfile,"ResubmitCommand\tmy-scriptfile\n");
    fprintf(pfile,"ComovingIntegrationOn\t0\n");
    fclose(pfile);
    return 0;
#else
    printf("ATTEMTPING TO RUN GAL2GAD2 ROUTINE WITHOUT HDF5 SUPPORT!\n");
    printf("EXITING!\n");
    exit(0);
    return 1;
#endif
}