source: src/helpers.hpp@ 09af1b

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 09af1b was 6ac7ee, checked in by Frederik Heber <heber@…>, 16 years ago

Merge branch 'ConcaveHull' of ../espack2 into ConcaveHull

Conflicts:

molecuilder/src/boundary.cpp
molecuilder/src/boundary.hpp
molecuilder/src/builder.cpp
molecuilder/src/linkedcell.cpp
molecuilder/src/linkedcell.hpp
molecuilder/src/vector.cpp
molecuilder/src/vector.hpp
util/src/NanoCreator.c

Basically, this resulted from a lot of conversions two from spaces to one tab, which is my standard indentation. The mess was caused by eclipse auto-indenting. And in espack2:ConcaveHull was the new stuff, so all from ConcaveHull was replaced in case of doubt.
Additionally, vector had ofstream << operator instead ostream << ...

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