[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 |
|
---|
[ad011c] | 18 | #include "CodePatterns/MemDebug.hpp"
|
---|
[14de469] | 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/Verbose.hpp"
|
---|
| 21 | #include "CodePatterns/Log.hpp"
|
---|
[06aedc] | 22 | #include "Helpers/helpers.hpp"
|
---|
[6cd79d] | 23 |
|
---|
[986ed3] | 24 | #include <iostream>
|
---|
| 25 |
|
---|
[14de469] | 26 | /********************************************** helpful functions *********************************/
|
---|
| 27 |
|
---|
| 28 |
|
---|
[d3a46d] | 29 | /** Output of a debug message to stderr.
|
---|
| 30 | * \param *P Problem at hand, points to ParallelSimulationData#me
|
---|
| 31 | * \param output output string
|
---|
| 32 | */
|
---|
| 33 | #ifdef HAVE_DEBUG
|
---|
| 34 | void debug_in(const char *output, const char *file, const int line) {
|
---|
[042f82] | 35 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
|
---|
[d3a46d] | 36 | }
|
---|
| 37 | #else
|
---|
[042f82] | 38 | void debug_in(const char *output, const char *file, const int line) {} // print nothing
|
---|
[d3a46d] | 39 | #endif
|
---|
[14de469] | 40 |
|
---|
[5034e1] | 41 |
|
---|
[14de469] | 42 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
|
---|
| 43 | * \param FragmentNumber total number of fragments to determine necessary number of digits
|
---|
| 44 | * \param digits number to create with 0 prefixed
|
---|
| 45 | * \return allocated(!) char array with number in digits, ten base.
|
---|
| 46 | */
|
---|
| 47 | char *FixedDigitNumber(const int FragmentNumber, const int digits)
|
---|
| 48 | {
|
---|
[042f82] | 49 | char *returnstring;
|
---|
| 50 | int number = FragmentNumber;
|
---|
| 51 | int order = 0;
|
---|
| 52 | while (number != 0) { // determine number of digits needed
|
---|
| 53 | number = (int)floor(((double)number / 10.));
|
---|
| 54 | order++;
|
---|
[e138de] | 55 | //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
|
---|
[042f82] | 56 | }
|
---|
| 57 | // allocate string
|
---|
[920c70] | 58 | returnstring = new char[order + 2];
|
---|
[042f82] | 59 | // terminate and fill string array from end backward
|
---|
| 60 | returnstring[order] = '\0';
|
---|
| 61 | number = digits;
|
---|
| 62 | for (int i=order;i--;) {
|
---|
| 63 | returnstring[i] = '0' + (char)(number % 10);
|
---|
| 64 | number = (int)floor(((double)number / 10.));
|
---|
| 65 | }
|
---|
[e138de] | 66 | //Log() << Verbose(0) << returnstring << endl;
|
---|
[042f82] | 67 | return returnstring;
|
---|
[14de469] | 68 | };
|
---|
| 69 |
|
---|
[21b9c3] | 70 | /**
|
---|
[920c70] | 71 | * Calls exit(255).
|
---|
[21b9c3] | 72 | */
|
---|
[6cd79d] | 73 | void performCriticalExit() {
|
---|
[21b9c3] | 74 | exit(255);
|
---|
| 75 | }
|
---|
[f80e20] | 76 |
|
---|
| 77 | sign_t sign(double value){
|
---|
| 78 | if(fabs(value)<MYEPSILON){
|
---|
| 79 | return Zero;
|
---|
| 80 | }
|
---|
| 81 | if(value<0)
|
---|
| 82 | return Minus;
|
---|
| 83 | else
|
---|
| 84 | return Plus;
|
---|
| 85 | }
|
---|