[14de469] | 1 | /** \file helpers.cpp
|
---|
| 2 | *
|
---|
[6ac7ee] | 3 | * Implementation of some auxiliary functions for memory dis-/allocation and so on
|
---|
[14de469] | 4 | */
|
---|
| 5 |
|
---|
[bf3817] | 6 | // include config.h
|
---|
| 7 | #ifdef HAVE_CONFIG_H
|
---|
| 8 | #include <config.h>
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
[112b09] | 11 | #include "Helpers/MemDebug.hpp"
|
---|
[14de469] | 12 |
|
---|
[952f38] | 13 | #include "Helpers/helpers.hpp"
|
---|
[0a4f7f] | 14 | #include "Helpers/fast_functions.hpp"
|
---|
[952f38] | 15 | #include "Helpers/Verbose.hpp"
|
---|
| 16 | #include "Helpers/Log.hpp"
|
---|
[6cd79d] | 17 |
|
---|
[986ed3] | 18 | #include <iostream>
|
---|
| 19 |
|
---|
[14de469] | 20 | /********************************************** helpful functions *********************************/
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | /** Asks for a double value and checks input
|
---|
| 24 | * \param *text question
|
---|
| 25 | */
|
---|
| 26 | double ask_value(const char *text)
|
---|
| 27 | {
|
---|
[042f82] | 28 | double test = 0.1439851348959832147598734598273456723948652983045928346598365;
|
---|
| 29 | do {
|
---|
[a67d19] | 30 | DoLog(0) && (Log() << Verbose(0) << text);
|
---|
[042f82] | 31 | cin >> test;
|
---|
| 32 | } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
|
---|
| 33 | return test;
|
---|
[14de469] | 34 | };
|
---|
| 35 |
|
---|
[d3a46d] | 36 | /** Output of a debug message to stderr.
|
---|
| 37 | * \param *P Problem at hand, points to ParallelSimulationData#me
|
---|
| 38 | * \param output output string
|
---|
| 39 | */
|
---|
| 40 | #ifdef HAVE_DEBUG
|
---|
| 41 | void debug_in(const char *output, const char *file, const int line) {
|
---|
[042f82] | 42 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
|
---|
[d3a46d] | 43 | }
|
---|
| 44 | #else
|
---|
[042f82] | 45 | void debug_in(const char *output, const char *file, const int line) {} // print nothing
|
---|
[d3a46d] | 46 | #endif
|
---|
[14de469] | 47 |
|
---|
| 48 | /** modulo operator for doubles.
|
---|
| 49 | * \param *b pointer to double
|
---|
| 50 | * \param lower_bound lower bound
|
---|
| 51 | * \param upper_bound upper bound
|
---|
| 52 | */
|
---|
| 53 | void bound(double *b, double lower_bound, double upper_bound)
|
---|
| 54 | {
|
---|
[042f82] | 55 | double step = (upper_bound - lower_bound);
|
---|
| 56 | while (*b >= upper_bound)
|
---|
| 57 | *b -= step;
|
---|
| 58 | while (*b < lower_bound)
|
---|
| 59 | *b += step;
|
---|
[6ac7ee] | 60 | };
|
---|
[14de469] | 61 |
|
---|
[5034e1] | 62 | /** Counts lines in file.
|
---|
| 63 | * Note we are scanning lines from current position, not from beginning.
|
---|
| 64 | * \param InputFile file to be scanned.
|
---|
| 65 | */
|
---|
| 66 | int CountLinesinFile(ifstream &InputFile)
|
---|
| 67 | {
|
---|
[920c70] | 68 | char *buffer = new char[MAXSTRINGSIZE];
|
---|
[5034e1] | 69 | int lines=0;
|
---|
| 70 |
|
---|
| 71 | int PositionMarker = InputFile.tellg(); // not needed as Inputfile is copied, given by value, not by ref
|
---|
| 72 | // count the number of lines, i.e. the number of fragments
|
---|
| 73 | InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
|
---|
| 74 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 75 | while(!InputFile.eof()) {
|
---|
| 76 | InputFile.getline(buffer, MAXSTRINGSIZE);
|
---|
| 77 | lines++;
|
---|
| 78 | }
|
---|
| 79 | InputFile.seekg(PositionMarker, ios::beg);
|
---|
[920c70] | 80 | delete[](buffer);
|
---|
[5034e1] | 81 | return lines;
|
---|
| 82 | };
|
---|
| 83 |
|
---|
[14de469] | 84 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
|
---|
| 85 | * \param FragmentNumber total number of fragments to determine necessary number of digits
|
---|
| 86 | * \param digits number to create with 0 prefixed
|
---|
| 87 | * \return allocated(!) char array with number in digits, ten base.
|
---|
| 88 | */
|
---|
| 89 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
|
---|
| 90 | {
|
---|
[042f82] | 91 | char *returnstring;
|
---|
| 92 | int number = FragmentNumber;
|
---|
| 93 | int order = 0;
|
---|
| 94 | while (number != 0) { // determine number of digits needed
|
---|
| 95 | number = (int)floor(((double)number / 10.));
|
---|
| 96 | order++;
|
---|
[e138de] | 97 | //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
|
---|
[042f82] | 98 | }
|
---|
| 99 | // allocate string
|
---|
[920c70] | 100 | returnstring = new char[order + 2];
|
---|
[042f82] | 101 | // terminate and fill string array from end backward
|
---|
| 102 | returnstring[order] = '\0';
|
---|
| 103 | number = digits;
|
---|
| 104 | for (int i=order;i--;) {
|
---|
| 105 | returnstring[i] = '0' + (char)(number % 10);
|
---|
| 106 | number = (int)floor(((double)number / 10.));
|
---|
| 107 | }
|
---|
[e138de] | 108 | //Log() << Verbose(0) << returnstring << endl;
|
---|
[042f82] | 109 | return returnstring;
|
---|
[14de469] | 110 | };
|
---|
| 111 |
|
---|
[e198c7] | 112 | /** Tests whether a given string contains a valid number or not.
|
---|
| 113 | * \param *string
|
---|
| 114 | * \return true - is a number, false - is not a valid number
|
---|
| 115 | */
|
---|
[6ac7ee] | 116 | bool IsValidNumber( const char *string)
|
---|
[e198c7] | 117 | {
|
---|
[042f82] | 118 | int ptr = 0;
|
---|
| 119 | if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot
|
---|
| 120 | ptr++;
|
---|
| 121 | if ((string[ptr] >= '0') && (string[ptr] <= '9'))
|
---|
| 122 | return true;
|
---|
| 123 | return false;
|
---|
[e198c7] | 124 | };
|
---|
| 125 |
|
---|
[f66195] | 126 | /** Comparison function for GSL heapsort on distances in two molecules.
|
---|
| 127 | * \param *a
|
---|
| 128 | * \param *b
|
---|
| 129 | * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
|
---|
| 130 | */
|
---|
| 131 | int CompareDoubles (const void * a, const void * b)
|
---|
| 132 | {
|
---|
| 133 | if (*(double *)a > *(double *)b)
|
---|
| 134 | return -1;
|
---|
| 135 | else if (*(double *)a < *(double *)b)
|
---|
| 136 | return 1;
|
---|
| 137 | else
|
---|
| 138 | return 0;
|
---|
| 139 | };
|
---|
| 140 |
|
---|
| 141 |
|
---|
[21b9c3] | 142 | /**
|
---|
[920c70] | 143 | * Calls exit(255).
|
---|
[21b9c3] | 144 | */
|
---|
[6cd79d] | 145 | void performCriticalExit() {
|
---|
[21b9c3] | 146 | exit(255);
|
---|
| 147 | }
|
---|