| 1 | gezelter | 507 | /* | 
| 2 | chuckv | 1436 | * Copyright (c) 2005, 2010 The University of Notre Dame. All Rights Reserved. | 
| 3 | gezelter | 246 | * | 
| 4 |  |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  |  | * non-exclusive, royalty free, license to use, modify and | 
| 6 |  |  | * redistribute this software in source and binary code form, provided | 
| 7 |  |  | * that the following conditions are met: | 
| 8 |  |  | * | 
| 9 | gezelter | 1390 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | gezelter | 246 | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 | gezelter | 1390 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | gezelter | 246 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  |  | *    documentation and/or other materials provided with the | 
| 15 |  |  | *    distribution. | 
| 16 |  |  | * | 
| 17 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 18 |  |  | * kind. All express or implied conditions, representations and | 
| 19 |  |  | * warranties, including any implied warranty of merchantability, | 
| 20 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 23 |  |  | * using, modifying or distributing the software or its | 
| 24 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 25 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 27 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 28 |  |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  |  | * such damages. | 
| 31 | gezelter | 1390 | * | 
| 32 |  |  | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 |  |  | * research, please cite the appropriate papers when you publish your | 
| 34 |  |  | * work.  Good starting points are: | 
| 35 |  |  | * | 
| 36 |  |  | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 |  |  | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 |  |  | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). | 
| 39 |  |  | * [4]  Vardeman & Gezelter, in progress (2009). | 
| 40 | gezelter | 246 | */ | 
| 41 |  |  |  | 
| 42 | gezelter | 2 | #define _LARGEFILE_SOURCE64 | 
| 43 |  |  | #define _FILE_OFFSET_BITS 64 | 
| 44 |  |  |  | 
| 45 | gezelter | 246 | #include "io/StatWriter.hpp" | 
| 46 | tim | 3 | #include "utils/simError.h" | 
| 47 | gezelter | 2 |  | 
| 48 | gezelter | 1437 | using namespace std; | 
| 49 |  |  |  | 
| 50 | gezelter | 1390 | namespace OpenMD { | 
| 51 | tim | 681 | StatsBitSet parseStatFileFormat(const std::string& format) { | 
| 52 |  |  | StringTokenizer tokenizer(format, " ,;|\t\n\r"); | 
| 53 |  |  | StatsBitSet mask; | 
| 54 |  |  | while(tokenizer.hasMoreTokens()) { | 
| 55 |  |  | std::string token(tokenizer.nextToken()); | 
| 56 |  |  | toUpper(token); | 
| 57 |  |  | Stats::StatsMapType::iterator i = Stats::statsMap.find(token); | 
| 58 |  |  | if (i != Stats::statsMap.end()) { | 
| 59 |  |  | mask.set(i->second); | 
| 60 |  |  | } else { | 
| 61 |  |  | sprintf( painCave.errMsg, | 
| 62 |  |  | "%s is not a valid statFileFormat keyword.\n", token.c_str() ); | 
| 63 |  |  | painCave.isFatal = 0; | 
| 64 | gezelter | 1390 | painCave.severity = OPENMD_ERROR; | 
| 65 | tim | 681 | simError(); | 
| 66 |  |  | } | 
| 67 |  |  | } | 
| 68 |  |  |  | 
| 69 |  |  | return mask; | 
| 70 |  |  | } | 
| 71 |  |  |  | 
| 72 | gezelter | 507 | StatWriter::StatWriter( const std::string& filename, const StatsBitSet& mask) : mask_(mask){ | 
| 73 | gezelter | 2 |  | 
| 74 |  |  | #ifdef IS_MPI | 
| 75 | gezelter | 507 | if(worldRank == 0 ){ | 
| 76 | gezelter | 2 | #endif // is_mpi | 
| 77 |  |  |  | 
| 78 | gezelter | 507 | statfile_.open(filename.c_str(), std::ios::out | std::ios::trunc ); | 
| 79 | gezelter | 2 |  | 
| 80 | gezelter | 507 | if( !statfile_ ){ | 
| 81 | gezelter | 2 |  | 
| 82 | gezelter | 507 | sprintf( painCave.errMsg, | 
| 83 |  |  | "Could not open \"%s\" for stat output.\n", | 
| 84 |  |  | filename.c_str()); | 
| 85 |  |  | painCave.isFatal = 1; | 
| 86 |  |  | simError(); | 
| 87 |  |  | } | 
| 88 | gezelter | 2 |  | 
| 89 | gezelter | 507 | writeTitle(); | 
| 90 | gezelter | 2 |  | 
| 91 |  |  | #ifdef IS_MPI | 
| 92 | gezelter | 507 | } | 
| 93 | gezelter | 2 |  | 
| 94 | gezelter | 507 | sprintf( checkPointMsg, | 
| 95 |  |  | "Sucessfully opened output file for stating.\n"); | 
| 96 | gezelter | 1241 | errorCheckPoint(); | 
| 97 | gezelter | 2 | #endif // is_mpi | 
| 98 |  |  |  | 
| 99 | gezelter | 507 | } | 
| 100 | gezelter | 2 |  | 
| 101 | gezelter | 507 | StatWriter::~StatWriter( ){ | 
| 102 | gezelter | 2 |  | 
| 103 |  |  | #ifdef IS_MPI | 
| 104 | gezelter | 507 | if(worldRank == 0 ){ | 
| 105 | gezelter | 2 | #endif // is_mpi | 
| 106 |  |  |  | 
| 107 | gezelter | 507 | statfile_.close(); | 
| 108 | gezelter | 2 |  | 
| 109 |  |  | #ifdef IS_MPI | 
| 110 | gezelter | 507 | } | 
| 111 |  |  | #endif // is_mpi | 
| 112 | gezelter | 2 | } | 
| 113 |  |  |  | 
| 114 | gezelter | 507 | void StatWriter::writeTitle() { | 
| 115 | gezelter | 2 |  | 
| 116 |  |  |  | 
| 117 | gezelter | 246 | #ifdef IS_MPI | 
| 118 |  |  | if(worldRank == 0 ){ | 
| 119 |  |  | #endif // is_mpi | 
| 120 | gezelter | 2 |  | 
| 121 | gezelter | 507 | //write title | 
| 122 |  |  | statfile_ << "#"; | 
| 123 |  |  | for (int i =0; i < mask_.size(); ++i) { | 
| 124 |  |  | if (mask_[i]) { | 
| 125 |  |  | statfile_ << "\t" << Stats::getTitle(i) << "(" << Stats::getUnits(i) << ")"; | 
| 126 |  |  | } | 
| 127 |  |  | } | 
| 128 |  |  | statfile_ << std::endl; | 
| 129 | gezelter | 246 |  | 
| 130 | gezelter | 2 | #ifdef IS_MPI | 
| 131 | gezelter | 507 | } | 
| 132 |  |  | #endif // is_mpi | 
| 133 | gezelter | 246 | } | 
| 134 |  |  |  | 
| 135 | gezelter | 507 | void StatWriter::writeStat(const Stats& s){ | 
| 136 | gezelter | 246 |  | 
| 137 |  |  | #ifdef IS_MPI | 
| 138 |  |  | if(worldRank == 0 ){ | 
| 139 | gezelter | 2 | #endif // is_mpi | 
| 140 |  |  |  | 
| 141 | gezelter | 507 | statfile_.precision(8); | 
| 142 |  |  | for (int i =0; i < mask_.size(); ++i) { | 
| 143 |  |  | if (mask_[i]) { | 
| 144 | gezelter | 1437 | if (! isinf(s[i]) && ! isnan(s[i])){ | 
| 145 |  |  | statfile_ << "\t" << s[i]; | 
| 146 | chuckv | 1436 | } | 
| 147 |  |  | else{ | 
| 148 |  |  | sprintf( painCave.errMsg, | 
| 149 | gezelter | 1437 | "StatWriter detected a numerical error writing: %s ", | 
| 150 | chuckv | 1436 | Stats::getTitle(i).c_str()); | 
| 151 |  |  | painCave.isFatal = 1; | 
| 152 |  |  | simError(); | 
| 153 | gezelter | 1437 | } | 
| 154 | gezelter | 507 | } | 
| 155 |  |  | } | 
| 156 |  |  | statfile_ << std::endl; | 
| 157 | gezelter | 2 |  | 
| 158 | gezelter | 507 | statfile_.flush(); | 
| 159 | gezelter | 2 |  | 
| 160 |  |  | #ifdef IS_MPI | 
| 161 | gezelter | 246 | } | 
| 162 | chuckv | 1436 | errorCheckPoint(); | 
| 163 | gezelter | 2 | #endif // is_mpi | 
| 164 | gezelter | 507 | } | 
| 165 | gezelter | 2 |  | 
| 166 | gezelter | 246 | } |