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