source: src/helpers.hpp@ 87e2e39

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 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 87e2e39 was be90f1, checked in by Saskia Metzler <metzler@…>, 16 years ago

temp commit

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