| [14de469] | 1 | /** \file helpers.cpp | 
|---|
|  | 2 | * | 
|---|
| [6ac7ee] | 3 | * Implementation of some auxiliary functions for memory dis-/allocation and so on | 
|---|
| [14de469] | 4 | */ | 
|---|
|  | 5 |  | 
|---|
|  | 6 |  | 
|---|
|  | 7 | #include "helpers.hpp" | 
|---|
|  | 8 |  | 
|---|
|  | 9 | /********************************************** helpful functions *********************************/ | 
|---|
|  | 10 |  | 
|---|
|  | 11 |  | 
|---|
|  | 12 | /** Asks for a double value and checks input | 
|---|
|  | 13 | * \param *text question | 
|---|
|  | 14 | */ | 
|---|
|  | 15 | double ask_value(const char *text) | 
|---|
|  | 16 | { | 
|---|
| [042f82] | 17 | double test = 0.1439851348959832147598734598273456723948652983045928346598365; | 
|---|
|  | 18 | do { | 
|---|
|  | 19 | cout << Verbose(0) << text; | 
|---|
|  | 20 | cin >> test; | 
|---|
|  | 21 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365); | 
|---|
|  | 22 | return test; | 
|---|
| [14de469] | 23 | }; | 
|---|
|  | 24 |  | 
|---|
| [d3a46d] | 25 | /** Output of a debug message to stderr. | 
|---|
|  | 26 | * \param *P Problem at hand, points to ParallelSimulationData#me | 
|---|
|  | 27 | * \param output output string | 
|---|
|  | 28 | */ | 
|---|
|  | 29 | #ifdef HAVE_DEBUG | 
|---|
|  | 30 | void debug_in(const char *output, const char *file, const int line) { | 
|---|
| [042f82] | 31 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output); | 
|---|
| [d3a46d] | 32 | } | 
|---|
|  | 33 | #else | 
|---|
| [042f82] | 34 | void debug_in(const char *output, const char *file, const int line) {}  // print nothing | 
|---|
| [d3a46d] | 35 | #endif | 
|---|
| [14de469] | 36 |  | 
|---|
|  | 37 | /** modulo operator for doubles. | 
|---|
|  | 38 | * \param *b pointer to double | 
|---|
|  | 39 | * \param lower_bound lower bound | 
|---|
|  | 40 | * \param upper_bound upper bound | 
|---|
|  | 41 | */ | 
|---|
|  | 42 | void bound(double *b, double lower_bound, double upper_bound) | 
|---|
|  | 43 | { | 
|---|
| [042f82] | 44 | double step = (upper_bound - lower_bound); | 
|---|
|  | 45 | while (*b >= upper_bound) | 
|---|
|  | 46 | *b -= step; | 
|---|
|  | 47 | while (*b < lower_bound) | 
|---|
|  | 48 | *b += step; | 
|---|
| [6ac7ee] | 49 | }; | 
|---|
| [14de469] | 50 |  | 
|---|
|  | 51 | /** Flips two doubles. | 
|---|
|  | 52 | * \param *x pointer to first double | 
|---|
|  | 53 | * \param *y pointer to second double | 
|---|
|  | 54 | */ | 
|---|
|  | 55 | void flip(double *x, double *y) | 
|---|
|  | 56 | { | 
|---|
| [042f82] | 57 | double tmp; | 
|---|
|  | 58 | tmp = *x; | 
|---|
|  | 59 | *x = *y; | 
|---|
|  | 60 | *y = tmp; | 
|---|
| [14de469] | 61 | }; | 
|---|
|  | 62 |  | 
|---|
|  | 63 | /** Returns the power of \a n with respect to \a base. | 
|---|
|  | 64 | * \param base basis | 
|---|
|  | 65 | * \param n power | 
|---|
|  | 66 | * \return \f$base^n\f$ | 
|---|
|  | 67 | */ | 
|---|
|  | 68 | int pot(int base, int n) | 
|---|
|  | 69 | { | 
|---|
| [042f82] | 70 | int res = 1; | 
|---|
|  | 71 | int j; | 
|---|
|  | 72 | for (j=n;j--;) | 
|---|
|  | 73 | res *= base; | 
|---|
|  | 74 | return res; | 
|---|
| [14de469] | 75 | }; | 
|---|
|  | 76 |  | 
|---|
| [5034e1] | 77 | /** Counts lines in file. | 
|---|
|  | 78 | * Note we are scanning lines from current position, not from beginning. | 
|---|
|  | 79 | * \param InputFile file to be scanned. | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | int CountLinesinFile(ifstream &InputFile) | 
|---|
|  | 82 | { | 
|---|
|  | 83 | char *buffer = Malloc<char>(MAXSTRINGSIZE, "CountLinesinFile: *buffer"); | 
|---|
|  | 84 | int lines=0; | 
|---|
|  | 85 |  | 
|---|
|  | 86 | int PositionMarker = InputFile.tellg();  // not needed as Inputfile is copied, given by value, not by ref | 
|---|
|  | 87 | // count the number of lines, i.e. the number of fragments | 
|---|
|  | 88 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines | 
|---|
|  | 89 | InputFile.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 90 | while(!InputFile.eof()) { | 
|---|
|  | 91 | InputFile.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 92 | lines++; | 
|---|
|  | 93 | } | 
|---|
|  | 94 | InputFile.seekg(PositionMarker, ios::beg); | 
|---|
|  | 95 | Free(&buffer); | 
|---|
|  | 96 | return lines; | 
|---|
|  | 97 | }; | 
|---|
|  | 98 |  | 
|---|
| [14de469] | 99 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits. | 
|---|
|  | 100 | * \param FragmentNumber total number of fragments to determine necessary number of digits | 
|---|
|  | 101 | * \param digits number to create with 0 prefixed | 
|---|
|  | 102 | * \return allocated(!) char array with number in digits, ten base. | 
|---|
|  | 103 | */ | 
|---|
|  | 104 | char *FixedDigitNumber(const int FragmentNumber, const int digits) | 
|---|
|  | 105 | { | 
|---|
| [042f82] | 106 | char *returnstring; | 
|---|
|  | 107 | int number = FragmentNumber; | 
|---|
|  | 108 | int order = 0; | 
|---|
|  | 109 | while (number != 0) { // determine number of digits needed | 
|---|
|  | 110 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 111 | order++; | 
|---|
|  | 112 | //cout << "Number is " << number << ", order is " << order << "." << endl; | 
|---|
|  | 113 | } | 
|---|
|  | 114 | // allocate string | 
|---|
| [29812d] | 115 | returnstring = Malloc<char>(order + 2, "FixedDigitNumber: *returnstring"); | 
|---|
| [042f82] | 116 | // terminate  and fill string array from end backward | 
|---|
|  | 117 | returnstring[order] = '\0'; | 
|---|
|  | 118 | number = digits; | 
|---|
|  | 119 | for (int i=order;i--;) { | 
|---|
|  | 120 | returnstring[i] = '0' + (char)(number % 10); | 
|---|
|  | 121 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 122 | } | 
|---|
|  | 123 | //cout << returnstring << endl; | 
|---|
|  | 124 | return returnstring; | 
|---|
| [14de469] | 125 | }; | 
|---|
|  | 126 |  | 
|---|
| [e198c7] | 127 | /** Tests whether a given string contains a valid number or not. | 
|---|
|  | 128 | * \param *string | 
|---|
|  | 129 | * \return true - is a number, false - is not a valid number | 
|---|
|  | 130 | */ | 
|---|
| [6ac7ee] | 131 | bool IsValidNumber( const char *string) | 
|---|
| [e198c7] | 132 | { | 
|---|
| [042f82] | 133 | int ptr = 0; | 
|---|
|  | 134 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot | 
|---|
|  | 135 | ptr++; | 
|---|
|  | 136 | if ((string[ptr] >= '0') && (string[ptr] <= '9')) | 
|---|
|  | 137 | return true; | 
|---|
|  | 138 | return false; | 
|---|
| [e198c7] | 139 | }; | 
|---|
|  | 140 |  | 
|---|
| [f66195] | 141 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix. | 
|---|
|  | 142 | * \param *symm 6-dim array of unique symmetric matrix components | 
|---|
|  | 143 | * \return allocated NDIM*NDIM array with the symmetric matrix | 
|---|
|  | 144 | */ | 
|---|
|  | 145 | double * ReturnFullMatrixforSymmetric(double *symm) | 
|---|
|  | 146 | { | 
|---|
|  | 147 | double *matrix = Malloc<double>(NDIM * NDIM, "molecule::ReturnFullMatrixforSymmetric: *matrix"); | 
|---|
|  | 148 | matrix[0] = symm[0]; | 
|---|
|  | 149 | matrix[1] = symm[1]; | 
|---|
|  | 150 | matrix[2] = symm[3]; | 
|---|
|  | 151 | matrix[3] = symm[1]; | 
|---|
|  | 152 | matrix[4] = symm[2]; | 
|---|
|  | 153 | matrix[5] = symm[4]; | 
|---|
|  | 154 | matrix[6] = symm[3]; | 
|---|
|  | 155 | matrix[7] = symm[4]; | 
|---|
|  | 156 | matrix[8] = symm[5]; | 
|---|
|  | 157 | return matrix; | 
|---|
|  | 158 | }; | 
|---|
|  | 159 |  | 
|---|
|  | 160 | /** Comparison function for GSL heapsort on distances in two molecules. | 
|---|
|  | 161 | * \param *a | 
|---|
|  | 162 | * \param *b | 
|---|
|  | 163 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b | 
|---|
|  | 164 | */ | 
|---|
|  | 165 | int CompareDoubles (const void * a, const void * b) | 
|---|
|  | 166 | { | 
|---|
|  | 167 | if (*(double *)a > *(double *)b) | 
|---|
|  | 168 | return -1; | 
|---|
|  | 169 | else if (*(double *)a < *(double *)b) | 
|---|
|  | 170 | return 1; | 
|---|
|  | 171 | else | 
|---|
|  | 172 | return 0; | 
|---|
|  | 173 | }; | 
|---|
|  | 174 |  | 
|---|
|  | 175 |  | 
|---|
|  | 176 | /** Allocates a memory range using malloc(). | 
|---|
| [29812d] | 177 | * Prints the provided error message in case of a failure. | 
|---|
|  | 178 | * | 
|---|
|  | 179 | * \param number of memory slices of type X to allocate | 
|---|
|  | 180 | * \param failure message which is printed if the allocation fails | 
|---|
|  | 181 | * \return pointer to the allocated memory range, will be NULL if a failure occurred | 
|---|
|  | 182 | */ | 
|---|
|  | 183 | template <> char* Malloc<char>(size_t size, const char* output) | 
|---|
|  | 184 | { | 
|---|
|  | 185 | char* buffer = NULL; | 
|---|
|  | 186 | buffer = (char*) malloc(sizeof(char) * (size + 1)); | 
|---|
|  | 187 | for (size_t i = size; i--;) | 
|---|
|  | 188 | buffer[i] = (i % 2 == 0) ? 'p': 'c'; | 
|---|
|  | 189 | buffer[size] = '\0'; | 
|---|
|  | 190 |  | 
|---|
| [c30180] | 191 | if (buffer != NULL) { | 
|---|
|  | 192 | MemoryUsageObserver::getInstance()->addMemory(buffer, size); | 
|---|
|  | 193 | } else { | 
|---|
| [29812d] | 194 | cout << Verbose(0) << "Malloc for datatype " << typeid(char).name() | 
|---|
|  | 195 | << " failed - pointer is NULL: " << output << endl; | 
|---|
| [c30180] | 196 | } | 
|---|
| [29812d] | 197 |  | 
|---|
|  | 198 | return buffer; | 
|---|
|  | 199 | }; | 
|---|
|  | 200 |  | 
|---|
| [21b9c3] | 201 | /** | 
|---|
|  | 202 | * Frees all memory registered by the memory observer and calls exit(225) afterwards. | 
|---|
|  | 203 | */ | 
|---|
| [c111db] | 204 | static void performCriticalExit() { | 
|---|
| [21b9c3] | 205 | map<void*, size_t> pointers = MemoryUsageObserver::getInstance()->getPointersToAllocatedMemory(); | 
|---|
|  | 206 | for (map<void*, size_t>::iterator runner = pointers.begin(); runner != pointers.end(); runner++) { | 
|---|
|  | 207 | Free(((void**) &runner->first)); | 
|---|
|  | 208 | } | 
|---|
| [e198c7] | 209 |  | 
|---|
| [21b9c3] | 210 | exit(255); | 
|---|
|  | 211 | } | 
|---|