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 "CodePatterns/Verbose.hpp"
|
---|
21 | #include "CodePatterns/Log.hpp"
|
---|
22 | #include "Helpers/helpers.hpp"
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 |
|
---|
26 | /********************************************** helpful functions *********************************/
|
---|
27 |
|
---|
28 |
|
---|
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) {
|
---|
35 | if (output) fprintf(stderr,"DEBUG: in %s at line %i: %s\n", file, line, output);
|
---|
36 | }
|
---|
37 | #else
|
---|
38 | void debug_in(const char *output, const char *file, const int line) {} // print nothing
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
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 | {
|
---|
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++;
|
---|
55 | //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
|
---|
56 | }
|
---|
57 | // allocate string
|
---|
58 | returnstring = new char[order + 2];
|
---|
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 | }
|
---|
66 | //Log() << Verbose(0) << returnstring << endl;
|
---|
67 | return returnstring;
|
---|
68 | };
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Calls exit(255).
|
---|
72 | */
|
---|
73 | void performCriticalExit() {
|
---|
74 | exit(255);
|
---|
75 | }
|
---|
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 | }
|
---|