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