source: src/helpers.hpp@ cd4ccc

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since cd4ccc was cd4ccc, checked in by Frederik Heber <heber@…>, 16 years ago

definitions for class element and verbose moved to their own header files and reducing contents of helpers.hpp

  • Property mode set to 100755
File size: 7.7 KB
Line 
1/** \file helpers.hpp
2 *
3 * Declaration of some auxiliary functions for memory dis-/allocation and so on
4 */
5
6#ifndef HELPERS_HPP_
7#define HELPERS_HPP_
8
9using namespace std;
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <iostream>
17#include <iomanip>
18#include <fstream>
19#include <sstream>
20#include <math.h>
21#include <string>
22#include <string.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <time.h>
26
27#include "defs.hpp"
28#include "verbose.hpp"
29
30/********************************************** helpful functions *********************************/
31
32// taken out of TREMOLO
33/*@-namechecks@*/
34#ifndef __GNUC__
35# undef __attribute__
36# define __attribute__(x)
37#endif
38/*@=namechecks@*/
39
40/* Behandelt aufgetretene Fehler. error ist der Fehlertyp(enum Errors)
41 void *SpecialData ist ein untypisierter Zeiger auf Spezielle Daten zur Fehlerbehandlung.
42 Man koennte auch noch einen Zeiger auf eine Funktion uebergeben */
43extern void /*@exits@*/ debug(const char *output);
44 //__attribute__ ((__return__));
45#define debug(data) debug_in((data), __FILE__, __LINE__)
46
47extern void /*@exits@*/ debug_in(const char *output,
48 const char *file, const int line);
49 //__attribute__ ((__return__));
50
51double ask_value(const char *text);
52bool check_bounds(double *x, double *cell_size);
53void bound(double *b, double lower_bound, double upper_bound);
54void flip(double *x, double *y);
55int pot(int base, int n);
56void * Malloc(size_t size, const char* output);
57void * Calloc(size_t size, const char* output);
58void * ReAlloc(void * OldPointer, size_t size, const char* output);
59char* MallocString(size_t size, const char* output);
60void Free(void ** buffer, const char* output);
61char *FixedDigitNumber(const int FragmentNumber, const int digits);
62bool IsValidNumber( const char *string);
63
64/********************************************** helpful template functions *********************************/
65
66/** Creates a lookup table for true father's Atom::Nr -> atom ptr.
67 * \param *out output stream for debugging
68 * \param *start begin of chain list
69 * \paran *end end of chain list
70 * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start)
71 * \param count optional predetermined count for table (otherwise we set the count to highest true father id)
72 * \return true - success, false - failure
73 */
74template <typename T> bool CreateFatherLookupTable(ofstream *out, T *start, T *end, T **&LookupTable, int count = 0)
75{
76 bool status = true;
77 T *Walker = NULL;
78 int AtomNo;
79
80 if (LookupTable != NULL) {
81 *out << "Pointer for Lookup table is not NULL! Aborting ..." <<endl;
82 return false;
83 }
84
85 // count them
86 if (count == 0) {
87 Walker = start;
88 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
89 Walker = Walker->next;
90 count = (count < Walker->GetTrueFather()->nr) ? Walker->GetTrueFather()->nr : count;
91 }
92 }
93 if (count <= 0) {
94 *out << "Count of lookup list is 0 or less." << endl;
95 return false;
96 }
97
98 // allocat and fill
99 LookupTable = (T **) Malloc(sizeof(T *)*count, "CreateFatherLookupTable - **LookupTable");
100 if (LookupTable == NULL) {
101 cerr << "LookupTable memory allocation failed!" << endl;
102 status = false;
103 } else {
104 for (int i=0;i<count;i++)
105 LookupTable[i] = NULL;
106 Walker = start;
107 while (Walker->next != end) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
108 Walker = Walker->next;
109 AtomNo = Walker->GetTrueFather()->nr;
110 if ((AtomNo >= 0) && (AtomNo < count)) {
111 //*out << "Setting LookupTable[" << AtomNo << "] to " << *Walker << endl;
112 LookupTable[AtomNo] = Walker;
113 } else {
114 *out << "Walker " << *Walker << " exceeded range of nuclear ids [0, " << count << ")." << endl;
115 status = false;
116 break;
117 }
118 }
119 }
120
121 return status;
122};
123
124/******************************** Some templates for list management ***********************************/
125
126/** Adds linking of an item to a list.
127 * \param *walker
128 * \return true - adding succeeded, false - error in list
129 */
130template <typename X> void link(X *walker, X *end)
131{
132 X *vorher = end->previous;
133 if (vorher != NULL)
134 vorher->next = walker;
135 end->previous = walker;
136 walker->previous = vorher;
137 walker->next = end;
138};
139
140/** Removes linking of an item in a list.
141 * \param *walker
142 * \return true - removing succeeded, false - given item not found in list
143 */
144template <typename X> void unlink(X *walker)
145{
146 if (walker->next != NULL)
147 walker->next->previous = walker->previous;
148 if (walker->previous != NULL)
149 walker->previous->next = walker->next;
150};
151
152/** Adds new item before an item \a *end in a list.
153 * \param *pointer item to be added
154 * \param *end end of list
155 * \return true - addition succeeded, false - unable to add item to list
156 */
157template <typename X> bool add(X *pointer, X *end)
158{
159 if (end != NULL) {
160 link(pointer, end);
161 } else {
162 pointer->previous = NULL;
163 pointer->next = NULL;
164 }
165 return true;
166};
167
168/** Finds item in list
169 * \param *suche search criteria
170 * \param *start begin of list
171 * \param *end end of list
172 * \return X - if found, NULL - if not found
173 */
174template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
175{
176 X *walker = start;
177 while (walker->next != end) { // go through list
178 walker = walker->next; // step onward beforehand
179 if (*walker->sort == *suche) return (walker);
180 }
181 return NULL;
182};
183
184/** Removes an item from the list without check.
185 * \param *walker item to be removed
186 * \return true - removing succeeded, false - given item not found in list
187 */
188template <typename X> void removewithoutcheck(X *walker)
189{
190 if (walker != NULL) {
191 unlink(walker);
192 delete(walker);
193 walker = NULL;
194 }
195};
196
197/** Removes an item from the list, checks if exists.
198 * Checks beforehand if atom is really within molecule list.
199 * \param *pointer item to be removed
200 * \param *start begin of list
201 * \param *end end of list
202 * \return true - removing succeeded, false - given item not found in list
203 */
204template <typename X> bool remove(X *pointer, X *start, X *end)
205{
206 X *walker = find (pointer->sort, start, end);
207/* while (walker->next != pointer) { // search through list
208 walker = walker->next;
209 if (walker == end) return false; // item not found in list
210 }*/
211 // atom found, now unlink
212 if (walker != NULL)
213 removewithoutcheck(walker);
214 else
215 return false;
216 return true;
217};
218
219/** Cleans the whole list.
220 * \param *start begin of list
221 * \param *end end of list
222 * \return true - list was cleaned successfully, false - error in list structure
223 */
224template <typename X> bool cleanup(X *start, X *end)
225{
226 X *pointer = start->next;
227 X *walker;
228 while (pointer != end) { // go through list
229 walker = pointer; // mark current
230 pointer = pointer->next; // step onward beforehand
231 // remove walker
232 unlink(walker);
233 delete(walker);
234 walker = NULL;
235 }
236 return true;
237};
238
239/** Returns the first marker in a chain list.
240 * \param *me one arbitrary item in chain list
241 * \return poiner to first marker
242 */
243template <typename X> X *GetFirst(X *me)
244{
245 X *Binder = me;
246 while(Binder->previous != NULL)
247 Binder = Binder->previous;
248 return Binder;
249};
250
251/** Returns the last marker in a chain list.
252 * \param *me one arbitrary item in chain list
253 * \return poiner to last marker
254 */
255template <typename X> X *GetLast(X *me)
256{
257 X *Binder = me;
258 while(Binder->next != NULL)
259 Binder = Binder->next;
260 return Binder;
261};
262
263/** Frees a two-dimensional array.
264 * \param *ptr pointer to array
265 * \param dim first dim of array
266 */
267template <typename X> void Free2DArray(X **ptr, int dim)
268{
269 int i;
270 if (ptr != NULL) {
271 for(i=dim;i--;)
272 if (ptr[i] != NULL)
273 free(ptr[i]);
274 free(ptr);
275 }
276};
277
278
279#endif /*HELPERS_HPP_*/
Note: See TracBrowser for help on using the repository browser.