1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
|
/////////////////////////////////////////////////////////////
// //
// Copyright (c) 2003-2017 by The University of Queensland //
// Centre for Geoscience Computing //
// http://earth.uq.edu.au/centre-geoscience-computing //
// //
// Primary Business: Brisbane, Queensland, Australia //
// Licensed under the Open Software License version 3.0 //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
/////////////////////////////////////////////////////////////
// --- TML includes ---
#include "tml/comm/comm.h"
// --- Project includes ---
#include "ParticleFieldMaster.h"
#include "field_const.h"
//--- IO includes ---
#include <iostream>
#include <fstream>
//--- STL inculdes ---
#include <string>
#include <map>
using std::cout;
using std::endl;
using std::ofstream;
using std::string;
using std::multimap;
//==== SCALAR PFM ===
/*!
Constructor. Set up the Master and broadcast parameters to the slaves.
\param comm the communicator
\param fieldname the name of the field to be saved
\param filename the name of the file to be saved into or the base for the generation of the filenames if the saving format requires multiple files
\param savetype the way to save data, currently supported are DX,SUM
\param t0 the first timestep to be saved
\param tend the last timestep to be saved
\param dt the interval between timesteps to be saving
*/
ScalarParticleFieldMaster::ScalarParticleFieldMaster(TML_Comm* comm,const string& fieldname,const string& filename,const string& savetype,int t0,int tend,int dt)
:AFieldMaster(comm,fieldname,filename,savetype,t0,tend,dt)
{
m_comm->broadcast_cont(fieldname);
m_comm->broadcast(m_id);
int is_tagged=0;
m_comm->broadcast(is_tagged);
}
/*!
Constructor. Set up the Master and broadcast parameters to the slaves.
\param comm the communicator
\param fieldname the name of the field to be saved
\param filename the name of the file to be saved into or the base for the generation of the filenames if the saving format requires multiple files
\param savetype the way to save data, currently supported are DX,SUM
\param t0 the first timestep to be saved
\param tend the last timestep to be saved
\param dt the interval between timesteps to be saving
\param tag the tag of the particles to be saved
\param mask the mask to be applied to the tag
*/
ScalarParticleFieldMaster::ScalarParticleFieldMaster(TML_Comm* comm,const string& fieldname,const string& filename,const string& savetype,int t0,int tend,int dt,int tag,int mask):AFieldMaster(comm,fieldname,filename,savetype,t0,tend,dt)
{
m_comm->broadcast_cont(fieldname);
m_comm->broadcast(m_id);
int is_tagged=1;
m_comm->broadcast(is_tagged);
m_comm->broadcast(tag);
m_comm->broadcast(mask);
}
void ScalarParticleFieldMaster::collect()
{
// send field id to slave
m_comm->broadcast(m_id);
if((m_write_type==WRITE_TYPE_SUM)||(m_write_type==WRITE_TYPE_MAX)) {
collectSum();
} else {
collectFull();
}
}
/*!
collect the full set of data, i.e. value, and posn,size of particles
*/
void ScalarParticleFieldMaster::collectFull()
{
multimap<int,pair<int,double> > temp_mm;
multimap<int,pair<int,Vec3> > temp_pos_mm;
multimap<int,pair<int,double> > temp_rad_mm;
// send type of collect to slaves
int coll_type=1;
m_comm->broadcast(coll_type);
// get data from slaves
m_comm->gather(temp_mm);
m_comm->gather(temp_pos_mm);
m_comm->gather(temp_rad_mm);
// collate receive data from multimap into single map
for(multimap<int,pair<int,double> >::iterator iter=temp_mm.begin();
iter!=temp_mm.end();
iter++){
m_save_map.insert(iter->second);
}
for(multimap<int,pair<int,Vec3> >::iterator iter=temp_pos_mm.begin();
iter!=temp_pos_mm.end();
iter++){
m_pos_map.insert(iter->second);
}
for(multimap<int,pair<int,double> >::iterator iter=temp_rad_mm.begin();
iter!=temp_rad_mm.end();
iter++){
m_rad_map.insert(iter->second);
}
}
/*!
collect sum of values only
*/
void ScalarParticleFieldMaster::collectSum()
{
multimap<int,double> temp_mm;
// send type of collect to slaves
m_comm->broadcast(m_write_type);
// get data from slaves
m_comm->gather(temp_mm);
// collate receive data from multimap into single map
for(multimap<int,double>::iterator iter=temp_mm.begin();
iter!=temp_mm.end();
iter++){
m_save_map.insert(*iter);
}
}
/*!
write data out as OpenDX compatible file
\todo desciption
*/
void ScalarParticleFieldMaster::writeAsDX()
{
//generate filename
string fn=makeFilename();
// write header
ofstream out_file(fn.c_str());
out_file << "points = " << m_save_map.size() << endl;
out_file << "format = ascii" << endl;
out_file << "dependency = positions, positions" << endl;
out_file << "interleaving = field" << endl;
out_file << "field = locations, " << m_field_name << endl;
out_file << "structure = 3-vector, scalar" << endl;
out_file << "type = float, float " << endl;
out_file << "header = marker \"Start\\n\"" << endl;
out_file << endl << "end" << endl;
out_file << "Start" << endl;
// write data
for(map<int,double>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << m_pos_map[iter->first] << " " << iter->second << endl;
}
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
m_rad_map.erase(m_rad_map.begin(),m_rad_map.end());
}
/*!
write data out as Povray(>=3.0) compatible file
\warning not fully impl.
*/
void ScalarParticleFieldMaster::writeAsPOV()
{
//generate filename
string fn=makeFilename();
// open file
ofstream out_file(fn.c_str());
// work out object size
Vec3 vmin,vmax;
for(map<int,Vec3>::iterator iter=m_pos_map.begin();
iter!=m_pos_map.end();
iter++){
vmin=cmin(vmin,iter->second);
vmax=cmax(vmax,iter->second);
}
// fit camera position
//Vec3 lookat=0.5*(vmin+vmax);
// write camera etc.
out_file << "#include \"colors.inc\"" << endl;
out_file << "background { color Black }" << endl;
out_file << " camera {" << endl;
out_file << "location <0, 2, -3>" << endl;
out_file << "look_at <0, 1, 2>" << endl;
out_file << "}" << endl<< endl;
out_file << "light_source { <2, 4, -3> color White}" << endl;
// write particles
for(map<int,double>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << "sphere { <" << m_pos_map[iter->first].X() << " , "
<< m_pos_map[iter->first].Y() << " , "
<< m_pos_map[iter->first].Z() << " > , "
<< m_rad_map[iter->first] << endl;
out_file << " texture { rgb < 1.0,0.5,0.5 > } }" << endl; //<< iter->second << endl;
}
// close file
out_file.close();
}
/*!
write data out as SILO file (if supported)
*/
void ScalarParticleFieldMaster::writeAsSILO()
{
#if HAVE_SILO
bool append;
DBfile* dbfile = openSiloFile(append);
if (!dbfile) {
console.Error() << "writeAsSILO not supported";
return;
}
// prepare data
std::vector<float> x, y, z, radius, data;
x.clear(); y.clear(); z.clear(); radius.clear(); data.clear();
for (map<int,double>::iterator iter=m_save_map.begin();
iter != m_save_map.end();
iter++) {
// the mesh is only needed if we are not appending
if (!append) {
const Vec3& v = m_pos_map[iter->first];
x.push_back(v.X());
y.push_back(v.Y());
z.push_back(v.Z());
}
radius.push_back(m_rad_map[iter->first]);
data.push_back(iter->second);
}
// write mesh first if needed
if (!append) {
float* coords[] = { &x[0], &y[0], &z[0] };
DBPutPointmesh(dbfile, "mesh", 3, coords, m_save_map.size(),
DB_FLOAT, NULL);
}
// and now the fields
DBPutPointvar1(dbfile, "radius", "mesh", &radius[0], radius.size(),
DB_FLOAT, NULL);
DBPutPointvar1(dbfile, m_field_name.c_str(), "mesh", &data[0],
data.size(), DB_FLOAT, NULL);
// close file
DBClose(dbfile);
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
m_rad_map.erase(m_rad_map.begin(),m_rad_map.end());
#else
console.Error() << "writeAsSILO not supported\n";
#endif
}
/*!
sum data and write them out into a single continuous file
\warning n
*/
void ScalarParticleFieldMaster::writeAsSUM()
{
// sum data
double sum_data=0.0;
for(map<int,double>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
sum_data+=iter->second;
}
// open file for appending
ofstream out_file(m_file_name.c_str(),ios::app);
// write data
out_file << sum_data << endl;
// close file
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
m_rad_map.erase(m_rad_map.begin(),m_rad_map.end());
}
/*!
find max datum and write it out into a single continuous file
*/
void ScalarParticleFieldMaster::writeAsMAX()
{
// get max data
double max_data=(m_save_map.begin())->second;
for(map<int,double>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
max_data=(iter->second > max_data) ? iter->second : max_data;
}
// open file for appending
ofstream out_file(m_file_name.c_str(),ios::app);
// write data
out_file << max_data << endl;
// close file
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
m_rad_map.erase(m_rad_map.begin(),m_rad_map.end());
}
/*!
write data as a raw series of values, one row of values per time step,
all timesteps into the same file
*/
void ScalarParticleFieldMaster::writeAsRAW_SERIES()
{
// open file
ofstream out_file(m_file_name.c_str(),ios::app);
// write data
for(map<int,double>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << iter->second << " ";
}
out_file << endl;
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
m_rad_map.erase(m_rad_map.begin(),m_rad_map.end());
}
/*!
write data as raw id, position, radius, value, one time step per file
*/
void ScalarParticleFieldMaster::writeAsRawWithPosID()
{
//generate filename
string fn=makeFilename();
// open file
ofstream out_file(fn.c_str());
// write data
for(map<int,double>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << iter->first << " " << m_pos_map[iter->first] << " " << m_rad_map[iter->first] << " "<< iter->second << endl;
}
// close file
out_file.close();
// clean up
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
m_rad_map.erase(m_rad_map.begin(),m_rad_map.end());
}
// === VECTOR PFM ===
/*!
Constructor. Set up the Master and broadcast parameters to the slaves.
\param comm the communicator
\param fieldname the name of the field to be saved
\param filename the name of the file to be saved into or the base for the generation of the filenames if the saving format requires multiple files
\param savetype the way to save data, currently supported are DX,SUM
\param t0 the first timestep to be saved
\param tend the last timestep to be saved
\param dt the interval between timesteps to be saving
*/
VectorParticleFieldMaster::VectorParticleFieldMaster(TML_Comm* comm,const string& fieldname,const string& filename,const string& savetype,int t0,int tend,int dt)
:AFieldMaster(comm,fieldname,filename,savetype,t0,tend,dt)
{
m_comm->broadcast_cont(fieldname);
m_comm->broadcast(m_id);
int is_tagged=0;
m_comm->broadcast(is_tagged);
}
/*!
Constructor. Set up the Master and broadcast parameters to the slaves.
\param comm the communicator
\param fieldname the name of the field to be saved
\param filename the name of the file to be saved into or the base for the generation of the filenames if the saving format requires multiple files
\param savetype the way to save data, currently supported are DX,SUM
\param t0 the first timestep to be saved
\param tend the last timestep to be saved
\param dt the interval between timesteps to be saving
\param tag the tag of the particles to be saved
\param mask the mask to be applied to the tag
*/
VectorParticleFieldMaster::VectorParticleFieldMaster(TML_Comm* comm,const string& fieldname,const string& filename,const string& savetype,int t0,int tend,int dt,int tag,int mask)
:AFieldMaster(comm,fieldname,filename,savetype,t0,tend,dt)
{
m_comm->broadcast_cont(fieldname);
m_comm->broadcast(m_id);
int is_tagged=1;
m_comm->broadcast(is_tagged);
m_comm->broadcast(tag);
m_comm->broadcast(mask);
}
void VectorParticleFieldMaster::collect()
{
multimap<int,pair<int,Vec3> > temp_mm;
multimap<int,pair<int,Vec3> > temp_pos_mm;
// send field id to slaves
m_comm->broadcast(m_id);
//receive data from fields
m_comm->gather(temp_mm);
m_comm->gather(temp_pos_mm);
// collate receive data from multimap into single map
for(multimap<int,pair<int,Vec3> >::iterator iter=temp_mm.begin();
iter!=temp_mm.end();
iter++){
m_save_map.insert(iter->second);
}
for(multimap<int,pair<int,Vec3> >::iterator iter=temp_pos_mm.begin();
iter!=temp_pos_mm.end();
iter++){
m_pos_map.insert(iter->second);
}
}
/*!
write data out as OpenDX compatible file
\todo desciption
*/
void VectorParticleFieldMaster::writeAsDX()
{
//generate filename
string fn=makeFilename();
// write header
ofstream out_file(fn.c_str());
out_file << "points = " << m_save_map.size() << endl;
out_file << "format = ascii" << endl;
out_file << "dependency = positions, positions" << endl;
out_file << "interleaving = field" << endl;
out_file << "field = locations, " << m_field_name << endl;
out_file << "structure = 3-vector, 3-vector" << endl;
out_file << "type = float, float " << endl;
out_file << "header = marker \"Start\\n\"" << endl;
out_file << endl << "end" << endl;
out_file << "Start" << endl;
// write data
for(map<int,Vec3>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << m_pos_map[iter->first] << " " << iter->second << endl;
}
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
}
/*!
write data out as Povray(>=3.0) compatible file
\warning not impl.
*/
void VectorParticleFieldMaster::writeAsPOV()
{
}
/*!
write data out as SILO file (if supported)
*/
void VectorParticleFieldMaster::writeAsSILO()
{
#if HAVE_SILO
bool append;
DBfile* dbfile = openSiloFile(append);
if (!dbfile) {
console.Error() << "writeAsSILO not supported";
return;
}
// prepare data
std::vector<float> x, y, z;
std::vector<float> vx, vy, vz;
x.clear(); y.clear(); z.clear();
vx.clear(); vy.clear(); vz.clear();
for (map<int,Vec3>::iterator iter=m_save_map.begin();
iter != m_save_map.end();
iter++) {
if (!append) {
const Vec3& m = m_pos_map[iter->first];
x.push_back(m.X());
y.push_back(m.Y());
z.push_back(m.Z());
}
const Vec3& v = iter->second;
vx.push_back(v.X());
vy.push_back(v.Y());
vz.push_back(v.Z());
}
// write mesh first if needed
if (!append) {
float* coords[] = { &x[0], &y[0], &z[0] };
DBPutPointmesh(dbfile, "mesh", 3, coords, m_save_map.size(),
DB_FLOAT, NULL);
}
float* vdata[] = { &vx[0], &vy[0], &vz[0] };
DBPutPointvar(dbfile, m_field_name.c_str(), "mesh", 3, vdata,
m_save_map.size(), DB_FLOAT, NULL);
// close file
DBClose(dbfile);
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
#else
console.Error() << "writeAsSILO not supported\n";
#endif
}
/*!
sum data and write them out into a single continuous file
\warning untested
*/
void VectorParticleFieldMaster::writeAsSUM()
{
Vec3 sum_data=Vec3(0.0,0.0,0.0);
for(map<int,Vec3>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
sum_data+=iter->second;
}
// open file for appending
ofstream out_file(m_file_name.c_str(),ios::app);
// write data
out_file << sum_data << endl;
// close file
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
}
/*!
get max datum and write it out into a single continuous file
\warning untested
*/
void VectorParticleFieldMaster::writeAsMAX()
{
Vec3 max_data=(m_save_map.begin())->second;
for(map<int,Vec3>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
max_data=(iter->second.norm2() > max_data.norm2()) ? iter->second : max_data;
}
// open file for appending
ofstream out_file(m_file_name.c_str(),ios::app);
// write data
out_file << max_data << endl;
// close file
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
}
/*!
write data as a raw series of values, one row of values per time step,
all timesteps into the same file
*/
void VectorParticleFieldMaster::writeAsRAW_SERIES()
{
// open file
ofstream out_file(m_file_name.c_str(),ios::app);
// write data
for(map<int,Vec3>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << iter->second << " ";
}
out_file << endl;
out_file.close();
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
}
/*!
write data as raw position, value pairs, one time step per file
*/
void VectorParticleFieldMaster::writeAsRAW2()
{
//generate filename
string fn=makeFilename();
// open file
ofstream out_file(fn.c_str());
// write data
for(map<int,Vec3>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << m_pos_map[iter->first] << " " << iter->second << endl;
}
// close file
out_file.close();
// clean up
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
}
/*!
write data as raw id, position, value triples, one time step per file
*/
void VectorParticleFieldMaster::writeAsRawWithID()
{
//generate filename
string fn=makeFilename();
// open file
ofstream out_file(fn.c_str());
// write data
for(map<int,Vec3>::iterator iter=m_save_map.begin();
iter!=m_save_map.end();
iter++){
out_file << iter->first << " " << m_pos_map[iter->first] << " " << iter->second << endl;
}
// close file
out_file.close();
// clean up
m_save_map.erase(m_save_map.begin(),m_save_map.end());
m_pos_map.erase(m_pos_map.begin(),m_pos_map.end());
}
|