source: src/helpers.cpp@ ba9f5b

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 ba9f5b was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

  • Property mode set to 100755
File size: 5.5 KB
Line 
1/** \file helpers.cpp
2 *
3 * Implementation of some auxiliary functions for memory dis-/allocation and so on
4 */
5
6#include "Helpers/MemDebug.hpp"
7
8#include "helpers.hpp"
9#include "Helpers/fast_functions.hpp"
10#include "log.hpp"
11#include "memoryusageobserver.hpp"
12
13/********************************************** helpful functions *********************************/
14
15
16/** Asks for a double value and checks input
17 * \param *text question
18 */
19double ask_value(const char *text)
20{
21 double test = 0.1439851348959832147598734598273456723948652983045928346598365;
22 do {
23 DoLog(0) && (Log() << Verbose(0) << text);
24 cin >> test;
25 } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
26 return test;
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
34void 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
38void debug_in(const char *output, const char *file, const int line) {} // print nothing
39#endif
40
41/** modulo operator for doubles.
42 * \param *b pointer to double
43 * \param lower_bound lower bound
44 * \param upper_bound upper bound
45 */
46void bound(double *b, double lower_bound, double upper_bound)
47{
48 double step = (upper_bound - lower_bound);
49 while (*b >= upper_bound)
50 *b -= step;
51 while (*b < lower_bound)
52 *b += step;
53};
54
55/** Counts lines in file.
56 * Note we are scanning lines from current position, not from beginning.
57 * \param InputFile file to be scanned.
58 */
59int CountLinesinFile(ifstream &InputFile)
60{
61 char *buffer = new char[MAXSTRINGSIZE];
62 int lines=0;
63
64 int PositionMarker = InputFile.tellg(); // not needed as Inputfile is copied, given by value, not by ref
65 // count the number of lines, i.e. the number of fragments
66 InputFile.getline(buffer, MAXSTRINGSIZE); // skip comment lines
67 InputFile.getline(buffer, MAXSTRINGSIZE);
68 while(!InputFile.eof()) {
69 InputFile.getline(buffer, MAXSTRINGSIZE);
70 lines++;
71 }
72 InputFile.seekg(PositionMarker, ios::beg);
73 delete[](buffer);
74 return lines;
75};
76
77/** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
78 * \param FragmentNumber total number of fragments to determine necessary number of digits
79 * \param digits number to create with 0 prefixed
80 * \return allocated(!) char array with number in digits, ten base.
81 */
82char *FixedDigitNumber(const int FragmentNumber, const int digits)
83{
84 char *returnstring;
85 int number = FragmentNumber;
86 int order = 0;
87 while (number != 0) { // determine number of digits needed
88 number = (int)floor(((double)number / 10.));
89 order++;
90 //Log() << Verbose(0) << "Number is " << number << ", order is " << order << "." << endl;
91 }
92 // allocate string
93 returnstring = new char[order + 2];
94 // terminate and fill string array from end backward
95 returnstring[order] = '\0';
96 number = digits;
97 for (int i=order;i--;) {
98 returnstring[i] = '0' + (char)(number % 10);
99 number = (int)floor(((double)number / 10.));
100 }
101 //Log() << Verbose(0) << returnstring << endl;
102 return returnstring;
103};
104
105/** Tests whether a given string contains a valid number or not.
106 * \param *string
107 * \return true - is a number, false - is not a valid number
108 */
109bool IsValidNumber( const char *string)
110{
111 int ptr = 0;
112 if ((string[ptr] == '.') || (string[ptr] == '-')) // number may be negative or start with dot
113 ptr++;
114 if ((string[ptr] >= '0') && (string[ptr] <= '9'))
115 return true;
116 return false;
117};
118
119/** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
120 * \param *symm 6-dim array of unique symmetric matrix components
121 * \return allocated NDIM*NDIM array with the symmetric matrix
122 */
123double * ReturnFullMatrixforSymmetric(const double * const symm)
124{
125 double *matrix = new double[NDIM * NDIM];
126 matrix[0] = symm[0];
127 matrix[1] = symm[1];
128 matrix[2] = symm[3];
129 matrix[3] = symm[1];
130 matrix[4] = symm[2];
131 matrix[5] = symm[4];
132 matrix[6] = symm[3];
133 matrix[7] = symm[4];
134 matrix[8] = symm[5];
135 return matrix;
136};
137
138/** Calculate the inverse of a 3x3 matrix.
139 * \param *matrix NDIM_NDIM array
140 */
141double * InverseMatrix( const double * const A)
142{
143 double *B = new double[NDIM * NDIM];
144 double detA = RDET3(A);
145 double detAReci;
146
147 for (int i=0;i<NDIM*NDIM;++i)
148 B[i] = 0.;
149 // calculate the inverse B
150 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
151 detAReci = 1./detA;
152 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
153 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
154 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
155 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
156 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
157 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
158 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
159 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
160 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
161 }
162 return B;
163};
164
165
166
167/** Comparison function for GSL heapsort on distances in two molecules.
168 * \param *a
169 * \param *b
170 * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
171 */
172int CompareDoubles (const void * a, const void * b)
173{
174 if (*(double *)a > *(double *)b)
175 return -1;
176 else if (*(double *)a < *(double *)b)
177 return 1;
178 else
179 return 0;
180};
181
182
183/**
184 * Calls exit(255).
185 */
186void performCriticalExit() {
187 exit(255);
188}
Note: See TracBrowser for help on using the repository browser.