source: molecuilder/src/helpers.hpp@ 7b991d

Last change on this file since 7b991d was 3d4969, checked in by Frederik Heber <heber@…>, 16 years ago

Fixing ticket #18.

  • each eLog() << Verbose(0) is now followed by performCriticalExit().
  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[a0bcf1]1/** \file helpers.hpp
2 *
[e08f45]3 * Declaration of some auxiliary functions for memory dis-/allocation and so on
[a0bcf1]4 */
5
6#ifndef HELPERS_HPP_
7#define HELPERS_HPP_
8
9using namespace std;
10
[17b3a5c]11/*********************************************** includes ***********************************/
12
[4dca8e]13// include config.h
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
[a0bcf1]18#include <fstream>
19
[20895b]20#include "defs.hpp"
[543ce4]21#include "log.hpp"
[390a2b]22#include "memoryallocator.hpp"
[77da65]23
[20895b]24/********************************************** definitions *********************************/
25
26// some algebraic matrix stuff
27double RDET3(const double a[NDIM*NDIM]);
28double RDET2(const double a[4]);
29double RDET2(const double a0, const double a1, const double a2, const double a3);
30
[a0bcf1]31/********************************************** helpful functions *********************************/
32
[da83f8]33// taken out of TREMOLO
34/*@-namechecks@*/
35#ifndef __GNUC__
36# undef __attribute__
37# define __attribute__(x)
38#endif
39/*@=namechecks@*/
40
41/* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors)
[a048fa]42 void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
43 Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
[da83f8]44extern void /*@exits@*/ debug(const char *output);
[a048fa]45 //__attribute__ ((__return__));
[da83f8]46#define debug(data) debug_in((data), __FILE__, __LINE__)
47
48extern void /*@exits@*/ debug_in(const char *output,
[a048fa]49 const char *file, const int line);
50 //__attribute__ ((__return__));
[da83f8]51
[a0bcf1]52double ask_value(const char *text);
53bool check_bounds(double *x, double *cell_size);
54void bound(double *b, double lower_bound, double upper_bound);
55int pot(int base, int n);
[ff9879]56int CountLinesinFile(ifstream &InputFile);
[a0bcf1]57char *FixedDigitNumber(const int FragmentNumber, const int digits);
[45105b]58bool IsValidNumber( const char *string);
[17b3a5c]59int CompareDoubles (const void * a, const void * b);
[20895b]60double * ReturnFullMatrixforSymmetric(const double * const cell_size);
61double * InverseMatrix(const double * const A);
[a2db10]62void performCriticalExit();
[a0bcf1]63
[d50d2a]64/********************************************** helpful template functions *********************************/
65
[543794]66/** Flips two values.
[921097]67 * \param x first value
68 * \param y second value
[543794]69 */
[921097]70template <typename T> void flip(T &x, T &y)
[543794]71{
72 T tmp;
[921097]73 tmp = x;
74 x = y;
75 y = tmp;
[543794]76};
77
[c901e3]78/** Creates a lookup table for true father's Atom::Nr -> atom ptr.
79 * \param *start begin of chain list
80 * \paran *end end of chain list
81 * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start)
[c1b4a4]82 * \param count optional predetermined size for table (otherwise we set the count to highest true father id)
[c901e3]83 * \return true - success, false - failure
84 */
[543ce4]85template <typename T> bool CreateFatherLookupTable(T *start, T *end, T **&LookupTable, int count = 0)
[c901e3]86{
[a048fa]87 bool status = true;
88 T *Walker = NULL;
89 int AtomNo;
90
91 if (LookupTable != NULL) {
[543ce4]92 Log() << Verbose(0) << "Pointer for Lookup table is not NULL! Aborting ..." <<endl;
[a048fa]93 return false;
94 }
95
96 // count them
97 if (count == 0) {
98 Walker = start;
99 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
100 Walker = Walker->next;
101 count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count;
102 }
103 }
104 if (count <= 0) {
[543ce4]105 Log() << Verbose(0) << "Count of lookup list is 0 or less." << endl;
[a048fa]106 return false;
107 }
108
[8bc524]109 // allocate and fill
110 LookupTable = Calloc<T*>(count, "CreateFatherLookupTable - **LookupTable");
[a048fa]111 if (LookupTable == NULL) {
[543ce4]112 eLog() << Verbose(0) << "LookupTable memory allocation failed!" << endl;
[3d4969]113 performCriticalExit();
[a048fa]114 status = false;
115 } else {
116 Walker = start;
117 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
118 Walker = Walker->next;
119 AtomNo = Walker->GetTrueFather()->nr;
120 if ((AtomNo >= 0) && (AtomNo < count)) {
121 //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl;
122 LookupTable[AtomNo] = Walker;
123 } else {
[543ce4]124 Log() << Verbose(0) << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl;
[a048fa]125 status = false;
126 break;
127 }
128 }
129 }
130
131 return status;
[c901e3]132};
133
[d50d2a]134/** Frees a two-dimensional array.
135 * \param *ptr pointer to array
136 * \param dim first dim of array
137 */
138template <typename X> void Free2DArray(X **ptr, int dim)
139{
[a048fa]140 int i;
141 if (ptr != NULL) {
142 for(i=dim;i--;)
143 if (ptr[i] != NULL)
144 free(ptr[i]);
145 free(ptr);
146 }
[d50d2a]147};
[a0bcf1]148
[ff9879]149template <typename T> void Increment(T *value, T *inc)
[8ffe32]150{
[ff9879]151 *value += *inc;
[8ffe32]152};
153
[ff9879]154template <typename T> void AbsoluteValue(T *value, T *abs)
[8ffe32]155{
[ff9879]156 *value = *abs;
[8ffe32]157};
158
[ff9879]159template <typename T> void IncrementalAbsoluteValue(T *value, T *abs)
160{
161 *value = *abs;
162 (*abs) += 1;
163};
[a0bcf1]164
[390a2b]165
[17b3a5c]166
[a0bcf1]167#endif /*HELPERS_HPP_*/
Note: See TracBrowser for help on using the repository browser.