source: src/helpers.cpp@ 14de469

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 14de469 was 14de469, checked in by Frederik Heber <heber@…>, 17 years ago

-initial commit
-Minimum set of files needed from ESPACK SVN repository
-Switch to three tantamount package parts instead of all relating to pcp (as at some time Ralf's might find inclusion as well)

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/** \file helpers.cpp
2 *
3 * Implementation of some auxiliary functions for memory dis-/allocation and so on
4 */
5
6
7#include "helpers.hpp"
8
9/********************************************** helpful functions *********************************/
10
11
12/** Asks for a double value and checks input
13 * \param *text question
14 */
15double ask_value(const char *text)
16{
17 double test = 0.1439851348959832147598734598273456723948652983045928346598365;
18 do {
19 cout << Verbose(0) << text;
20 cin >> test;
21 } while (test == 0.1439851348959832147598734598273456723948652983045928346598365);
22 return test;
23};
24
25
26/** Wrapper for allocating'ing a memory range with *output if it fails.
27 * \param size number of chars to alloc for \a *buffer
28 * \param *output message if malloc fails
29 * \return pointer to memory range
30 */
31void * Malloc(size_t size, const char* output)
32{
33 void *buffer = NULL;
34 buffer = (void *)malloc(size); // alloc
35 if (buffer == NULL)
36 cout << Verbose(0) << "Malloc failed - pointer is NULL: " << output << endl;
37 return(buffer);
38};
39
40/** Wrapper for allocating'ing a memory range with *output if it fails.
41 * \param size number of chars to alloc for \a *buffer
42 * \param *output message if malloc fails
43 * \return pointer to memory range
44 */
45void * Calloc(size_t size, const char* output)
46{
47 void *buffer = NULL;
48 buffer = (void *)calloc(size, (size_t)0); // alloc
49 if (buffer == NULL)
50 cout << Verbose(0) << "Calloc failed - pointer is NULL: " << output << endl;
51 return(buffer);
52};
53
54/** Wrapper for reallocating'ing a memory range with *output if it fails.
55 * \param *OldPointer pointer to old memory range
56 * \param size number of chars to alloc for \a *buffer
57 * \param *output message if malloc fails
58 * \return pointer to memory range
59 */
60void * ReAlloc(void * OldPointer, size_t size, const char* output)
61{
62 void *buffer = NULL;
63 if (OldPointer == NULL)
64 cout << Verbose(0) << "ReAlloc impossible - old is NULL: " << output << endl;
65 buffer = (void *)realloc(OldPointer, size); // alloc
66 if (buffer == NULL)
67 cout << Verbose(0) << "ReAlloc failed - new is NULL: " << output << endl;
68 return(buffer);
69};
70
71/** Wrapper for free'ing an allocated memory range with *output if it fails.
72 * \param *buffer pointer to buffer to be free'd
73 * \param *output message if free fails
74 */
75void Free(void ** buffer, const char* output)
76{
77 if (*buffer == NULL) {
78 //cout << Verbose(5) << "Free not necesary: " << output << endl;
79 } else {
80 free(*buffer);
81 }
82 *buffer = NULL;
83};
84
85/** Malloc string array and set its length to the allocated length.
86 * \param *output message if malloc fails
87 * \param size number of chars to alloc for \a *buffer
88 * \return pointer to string array
89 */
90char* MallocString(size_t size, const char* output)
91{
92 size_t i;
93 char *buffer;
94 buffer = (char *)malloc(sizeof(char) * (size+1)); // alloc
95 if (buffer == NULL)
96 cout << Verbose(0) << output << endl;
97 for (i=0;i<size;i++) // reset
98 buffer[i] = i % 2 == 0 ? 'p': 'c';
99 buffer[size] = '\0'; // and set length marker on its end
100 return(buffer);
101}
102
103/** modulo operator for doubles.
104 * \param *b pointer to double
105 * \param lower_bound lower bound
106 * \param upper_bound upper bound
107 */
108void bound(double *b, double lower_bound, double upper_bound)
109{
110 double step = (upper_bound - lower_bound);
111 while (*b >= upper_bound)
112 *b -= step;
113 while (*b < lower_bound)
114 *b += step;
115};
116
117/** Flips two doubles.
118 * \param *x pointer to first double
119 * \param *y pointer to second double
120 */
121void flip(double *x, double *y)
122{
123 double tmp;
124 tmp = *x;
125 *x = *y;
126 *y = tmp;
127};
128
129/** Returns the power of \a n with respect to \a base.
130 * \param base basis
131 * \param n power
132 * \return \f$base^n\f$
133 */
134int pot(int base, int n)
135{
136 int res = 1;
137 int j;
138 for (j=0;j<n;j++)
139 res *= base;
140 return res;
141};
142
143/** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
144 * \param FragmentNumber total number of fragments to determine necessary number of digits
145 * \param digits number to create with 0 prefixed
146 * \return allocated(!) char array with number in digits, ten base.
147 */
148char *FixedDigitNumber(const int FragmentNumber, const int digits)
149{
150 char *returnstring;
151 int number = FragmentNumber;
152 int order = 0;
153 while (number != 0) { // determine number of digits needed
154 number = (int)floor(((double)number / 10.));
155 order++;
156 //cout << "Number is " << number << ", order is " << order << "." << endl;
157 }
158 // allocate string
159 returnstring = (char *) Malloc(sizeof(char)*(order+2), "MoleculeListClass::CreateFragmentNumberForOutput: *returnstring");
160 // terminate and fill string array from end backward
161 returnstring[order] = '\0';
162 number = digits;
163 for (int i=order-1;i>=0;i--){
164 returnstring[i] = '0' + (char)(number % 10);
165 number = (int)floor(((double)number / 10.));
166 }
167 return returnstring;
168};
169
Note: See TracBrowser for help on using the repository browser.