| [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" | 
|---|
| [0a4f7f] | 8 | #include "Helpers/fast_functions.hpp" | 
|---|
| [e138de] | 9 | #include "log.hpp" | 
|---|
| [6cd79d] | 10 | #include "memoryusageobserver.hpp" | 
|---|
|  | 11 |  | 
|---|
| [14de469] | 12 | /********************************************** helpful functions *********************************/ | 
|---|
|  | 13 |  | 
|---|
|  | 14 |  | 
|---|
|  | 15 | /** Asks for a double value and checks input | 
|---|
|  | 16 | * \param *text question | 
|---|
|  | 17 | */ | 
|---|
|  | 18 | double ask_value(const char *text) | 
|---|
|  | 19 | { | 
|---|
| [042f82] | 20 | double test = 0.1439851348959832147598734598273456723948652983045928346598365; | 
|---|
|  | 21 | do { | 
|---|
| [a67d19] | 22 | DoLog(0) && (Log() << Verbose(0) << text); | 
|---|
| [042f82] | 23 | cin >> test; | 
|---|
|  | 24 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365); | 
|---|
|  | 25 | return test; | 
|---|
| [14de469] | 26 | }; | 
|---|
|  | 27 |  | 
|---|
| [d3a46d] | 28 | /** Output of a debug message to stderr. | 
|---|
|  | 29 | * \param *P Problem at hand, points to ParallelSimulationData#me | 
|---|
|  | 30 | * \param output output string | 
|---|
|  | 31 | */ | 
|---|
|  | 32 | #ifdef HAVE_DEBUG | 
|---|
|  | 33 | void debug_in(const char *output, const char *file, const int line) { | 
|---|
| [042f82] | 34 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output); | 
|---|
| [d3a46d] | 35 | } | 
|---|
|  | 36 | #else | 
|---|
| [042f82] | 37 | void debug_in(const char *output, const char *file, const int line) {}  // print nothing | 
|---|
| [d3a46d] | 38 | #endif | 
|---|
| [14de469] | 39 |  | 
|---|
|  | 40 | /** modulo operator for doubles. | 
|---|
|  | 41 | * \param *b pointer to double | 
|---|
|  | 42 | * \param lower_bound lower bound | 
|---|
|  | 43 | * \param upper_bound upper bound | 
|---|
|  | 44 | */ | 
|---|
|  | 45 | void bound(double *b, double lower_bound, double upper_bound) | 
|---|
|  | 46 | { | 
|---|
| [042f82] | 47 | double step = (upper_bound - lower_bound); | 
|---|
|  | 48 | while (*b >= upper_bound) | 
|---|
|  | 49 | *b -= step; | 
|---|
|  | 50 | while (*b < lower_bound) | 
|---|
|  | 51 | *b += step; | 
|---|
| [6ac7ee] | 52 | }; | 
|---|
| [14de469] | 53 |  | 
|---|
| [5034e1] | 54 | /** Counts lines in file. | 
|---|
|  | 55 | * Note we are scanning lines from current position, not from beginning. | 
|---|
|  | 56 | * \param InputFile file to be scanned. | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | int CountLinesinFile(ifstream &InputFile) | 
|---|
|  | 59 | { | 
|---|
| [920c70] | 60 | char *buffer = new char[MAXSTRINGSIZE]; | 
|---|
| [5034e1] | 61 | int lines=0; | 
|---|
|  | 62 |  | 
|---|
|  | 63 | int PositionMarker = InputFile.tellg();  // not needed as Inputfile is copied, given by value, not by ref | 
|---|
|  | 64 | // count the number of lines, i.e. the number of fragments | 
|---|
|  | 65 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines | 
|---|
|  | 66 | InputFile.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 67 | while(!InputFile.eof()) { | 
|---|
|  | 68 | InputFile.getline(buffer, MAXSTRINGSIZE); | 
|---|
|  | 69 | lines++; | 
|---|
|  | 70 | } | 
|---|
|  | 71 | InputFile.seekg(PositionMarker, ios::beg); | 
|---|
| [920c70] | 72 | delete[](buffer); | 
|---|
| [5034e1] | 73 | return lines; | 
|---|
|  | 74 | }; | 
|---|
|  | 75 |  | 
|---|
| [14de469] | 76 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits. | 
|---|
|  | 77 | * \param FragmentNumber total number of fragments to determine necessary number of digits | 
|---|
|  | 78 | * \param digits number to create with 0 prefixed | 
|---|
|  | 79 | * \return allocated(!) char array with number in digits, ten base. | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | char *FixedDigitNumber(const int FragmentNumber, const int digits) | 
|---|
|  | 82 | { | 
|---|
| [042f82] | 83 | char *returnstring; | 
|---|
|  | 84 | int number = FragmentNumber; | 
|---|
|  | 85 | int order = 0; | 
|---|
|  | 86 | while (number != 0) { // determine number of digits needed | 
|---|
|  | 87 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 88 | order++; | 
|---|
| [e138de] | 89 | //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl; | 
|---|
| [042f82] | 90 | } | 
|---|
|  | 91 | // allocate string | 
|---|
| [920c70] | 92 | returnstring = new char[order + 2]; | 
|---|
| [042f82] | 93 | // terminate  and fill string array from end backward | 
|---|
|  | 94 | returnstring[order] = '\0'; | 
|---|
|  | 95 | number = digits; | 
|---|
|  | 96 | for (int i=order;i--;) { | 
|---|
|  | 97 | returnstring[i] = '0' + (char)(number % 10); | 
|---|
|  | 98 | number = (int)floor(((double)number / 10.)); | 
|---|
|  | 99 | } | 
|---|
| [e138de] | 100 | //Log() << Verbose(0) << returnstring << endl; | 
|---|
| [042f82] | 101 | return returnstring; | 
|---|
| [14de469] | 102 | }; | 
|---|
|  | 103 |  | 
|---|
| [e198c7] | 104 | /** Tests whether a given string contains a valid number or not. | 
|---|
|  | 105 | * \param *string | 
|---|
|  | 106 | * \return true - is a number, false - is not a valid number | 
|---|
|  | 107 | */ | 
|---|
| [6ac7ee] | 108 | bool IsValidNumber( const char *string) | 
|---|
| [e198c7] | 109 | { | 
|---|
| [042f82] | 110 | int ptr = 0; | 
|---|
|  | 111 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot | 
|---|
|  | 112 | ptr++; | 
|---|
|  | 113 | if ((string[ptr] >= '0') && (string[ptr] <= '9')) | 
|---|
|  | 114 | return true; | 
|---|
|  | 115 | return false; | 
|---|
| [e198c7] | 116 | }; | 
|---|
|  | 117 |  | 
|---|
| [f66195] | 118 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix. | 
|---|
|  | 119 | * \param *symm 6-dim array of unique symmetric matrix components | 
|---|
|  | 120 | * \return allocated NDIM*NDIM array with the symmetric matrix | 
|---|
|  | 121 | */ | 
|---|
| [99593f] | 122 | double * ReturnFullMatrixforSymmetric(const double * const symm) | 
|---|
| [f66195] | 123 | { | 
|---|
| [920c70] | 124 | double *matrix = new double[NDIM * NDIM]; | 
|---|
| [f66195] | 125 | matrix[0] = symm[0]; | 
|---|
|  | 126 | matrix[1] = symm[1]; | 
|---|
|  | 127 | matrix[2] = symm[3]; | 
|---|
|  | 128 | matrix[3] = symm[1]; | 
|---|
|  | 129 | matrix[4] = symm[2]; | 
|---|
|  | 130 | matrix[5] = symm[4]; | 
|---|
|  | 131 | matrix[6] = symm[3]; | 
|---|
|  | 132 | matrix[7] = symm[4]; | 
|---|
|  | 133 | matrix[8] = symm[5]; | 
|---|
|  | 134 | return matrix; | 
|---|
|  | 135 | }; | 
|---|
|  | 136 |  | 
|---|
| [99593f] | 137 | /** Calculate the inverse of a 3x3 matrix. | 
|---|
|  | 138 | * \param *matrix NDIM_NDIM array | 
|---|
|  | 139 | */ | 
|---|
|  | 140 | double * InverseMatrix( const double * const A) | 
|---|
|  | 141 | { | 
|---|
| [920c70] | 142 | double *B = new double[NDIM * NDIM]; | 
|---|
| [99593f] | 143 | double detA = RDET3(A); | 
|---|
|  | 144 | double detAReci; | 
|---|
|  | 145 |  | 
|---|
|  | 146 | for (int i=0;i<NDIM*NDIM;++i) | 
|---|
|  | 147 | B[i] = 0.; | 
|---|
|  | 148 | // calculate the inverse B | 
|---|
|  | 149 | if (fabs(detA) > MYEPSILON) {;  // RDET3(A) yields precisely zero if A irregular | 
|---|
|  | 150 | detAReci = 1./detA; | 
|---|
|  | 151 | B[0] =  detAReci*RDET2(A[4],A[5],A[7],A[8]);    // A_11 | 
|---|
|  | 152 | B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]);    // A_12 | 
|---|
|  | 153 | B[2] =  detAReci*RDET2(A[1],A[2],A[4],A[5]);    // A_13 | 
|---|
|  | 154 | B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]);    // A_21 | 
|---|
|  | 155 | B[4] =  detAReci*RDET2(A[0],A[2],A[6],A[8]);    // A_22 | 
|---|
|  | 156 | B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]);    // A_23 | 
|---|
|  | 157 | B[6] =  detAReci*RDET2(A[3],A[4],A[6],A[7]);    // A_31 | 
|---|
|  | 158 | B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]);    // A_32 | 
|---|
|  | 159 | B[8] =  detAReci*RDET2(A[0],A[1],A[3],A[4]);    // A_33 | 
|---|
|  | 160 | } | 
|---|
|  | 161 | return B; | 
|---|
|  | 162 | }; | 
|---|
|  | 163 |  | 
|---|
|  | 164 |  | 
|---|
|  | 165 |  | 
|---|
| [f66195] | 166 | /** Comparison function for GSL heapsort on distances in two molecules. | 
|---|
|  | 167 | * \param *a | 
|---|
|  | 168 | * \param *b | 
|---|
|  | 169 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b | 
|---|
|  | 170 | */ | 
|---|
|  | 171 | int CompareDoubles (const void * a, const void * b) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | if (*(double *)a > *(double *)b) | 
|---|
|  | 174 | return -1; | 
|---|
|  | 175 | else if (*(double *)a < *(double *)b) | 
|---|
|  | 176 | return 1; | 
|---|
|  | 177 | else | 
|---|
|  | 178 | return 0; | 
|---|
|  | 179 | }; | 
|---|
|  | 180 |  | 
|---|
|  | 181 |  | 
|---|
| [21b9c3] | 182 | /** | 
|---|
| [920c70] | 183 | * Calls exit(255). | 
|---|
| [21b9c3] | 184 | */ | 
|---|
| [6cd79d] | 185 | void performCriticalExit() { | 
|---|
| [21b9c3] | 186 | exit(255); | 
|---|
|  | 187 | } | 
|---|