Commit 4e5d76eb authored by jclamber's avatar jclamber

writing gadget2 file: fix bug when all same data not save for all components

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@136 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 20a211c7
......@@ -1982,6 +1982,29 @@ int CSnapshotGadgetOut::writeData(char * ptr,const size_t size_bytes,const int i
return 1;
}
// ============================================================================
// writeDataZero:
// perform Write operation on Data with value
template <class T> int CSnapshotGadgetOut::writeDataValue(T value,const size_t size_bytes,const int items)
{
bytes_counter += (size_bytes*items);
char * buffer=new char[size_bytes*items];
for (unsigned int i=0;i<(size_bytes*items);i+=sizeof(T)) {
char * p = (char *) &value;
for (unsigned int j=0;j<sizeof(T);j++) {
buffer[i]=*p; // init to value
p++;
}
}
// Save Data to file
out.write(buffer,size_bytes*items);
assert(out.good());
delete [] buffer;
return 1;
}
// ============================================================================
// setupHeader:
void CSnapshotGadgetOut::setupHeader(bool check)
{
......@@ -1998,7 +2021,7 @@ void CSnapshotGadgetOut::setupHeader(bool check)
// we fill header.mass[] array
ntot_withmasses=0;
for(int k=0;k<6;k++) { // loop an component
for(int k=0;k<6;k++) { // loop on components
if (header.npart[k] ) {
//assert(mass[k]!=NULL);
float mass0=0.0;
......@@ -2017,12 +2040,17 @@ void CSnapshotGadgetOut::setupHeader(bool check)
equal=false;
}
}
if (equal) { // same masses for the component
if (verbose) std::cerr <<"CSnapshotGadgetOut::setupHeader => same Mass["<<k<<"]="<<mass0<<"\n";
header.mass[k] = mass0;
} else {
header.mass[k] = 0.0;
ntot_withmasses+=header.npart[k];
if (mass[k]) {
if (equal) { // same masses for the component
if (verbose) std::cerr <<"CSnapshotGadgetOut::setupHeader => same Mass["<<k<<"]="<<mass0<<"\n";
header.mass[k] = mass0;
} else {
header.mass[k] = 0.0;
ntot_withmasses+=header.npart[k];
}
} else { // mass has not been specify for 'k' component
// we assume it's volontary, and we set mass = -666
header.mass[k] = -666.;
}
//std::cerr << "index before ="<< index << "\n";
//index += header.npart[k]; // next index in mass array
......@@ -2095,8 +2123,11 @@ int CSnapshotGadgetOut::write()
writeFRecord(blk);
for(int k=0;k<6;k++) {
if (header.npart[k]) { // pos exist for the component
assert(pos[k]!=NULL);
writeData((char *) pos[k], sizeof(float)*3, header.npart[k]);
//assert(pos[k]!=NULL);
if (pos[k])
writeData((char *) pos[k], sizeof(float)*3, header.npart[k]);
else
writeDataValue(0.0,sizeof(float)*3, header.npart[k]);
}
}
writeFRecord(blk);
......@@ -2107,8 +2138,12 @@ int CSnapshotGadgetOut::write()
writeBlockName("VEL ",blk);
writeFRecord(blk);
for(int k=0;k<6;k++)
if (header.npart[k]) // vel exist for the component
writeData((char *) vel[k], sizeof(float)*3, header.npart[k]);
if (header.npart[k]) { // vel exist for the component
if (vel[k])
writeData((char *) vel[k], sizeof(float)*3, header.npart[k]);
else
writeDataValue(0.0,sizeof(float)*3, header.npart[k]);
}
writeFRecord(blk);
}
// ID
......@@ -2126,8 +2161,12 @@ int CSnapshotGadgetOut::write()
delete [] iid;
} else { // There are IDs
for(int k=0;k<6;k++)
if (header.npart[k]) // id exist for the component
writeData((char *) id[k], sizeof(int), header.npart[k]);
if (header.npart[k]) {// id exist for the component
if (id[k])
writeData((char *) id[k], sizeof(int), header.npart[k]);
else
writeDataValue(0,sizeof(int), header.npart[k]);
}
}
writeFRecord(blk);
......@@ -2176,8 +2215,10 @@ int CSnapshotGadgetOut::write()
writeFRecord(blk);
for(int k=0;k<6;k++) {
if (header.npart[k]) { // pos exist for the component
assert(pot[k]!=NULL);
writeData((char *) pot[k], sizeof(float), header.npart[k]);
if (pot[k])
writeData((char *) pot[k], sizeof(float), header.npart[k]);
else
writeDataValue(0.0,sizeof(float), header.npart[k]);
}
}
writeFRecord(blk);
......@@ -2188,8 +2229,12 @@ int CSnapshotGadgetOut::write()
writeBlockName("ACCE",blk);
writeFRecord(blk);
for(int k=0;k<6;k++)
if (header.npart[k]) // acc exist for the component
writeData((char *) acc[k], sizeof(float)*3, header.npart[k]);
if (header.npart[k]) { // acc exist for the component
if (acc[k])
writeData((char *) acc[k], sizeof(float)*3, header.npart[k]);
else
writeDataValue(0.0,sizeof(float)*3, header.npart[k]);
}
writeFRecord(blk);
}
// Temp
......@@ -2289,4 +2334,10 @@ std::vector<double> CSnapshotGadgetOut::moveToCom()
}
return com;
}
// templates
template int CSnapshotGadgetOut::writeDataValue(int value,const size_t size_bytes,const int items);
template int CSnapshotGadgetOut::writeDataValue(float value,const size_t size_bytes,const int items);
template int CSnapshotGadgetOut::writeDataValue(double value,const size_t size_bytes,const int items);
} // end of namespace
......@@ -301,6 +301,7 @@ typedef struct particle_data_lite
int version;
int setHeader(t_io_header_1 *);
int writeData(char * ptr,const size_t size_bytes,const int items);
template <class T> int writeDataValue(T value, const size_t size_bytes,const int items);
void saveFile();
void setupHeader(bool check=false);
int writeHeader();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment