source: src/ConfigFileBuffer.cpp@ 586055

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

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ConfigFileBuffer.cpp
10 *
11 * Created on: 12.06.2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "Helpers/MemDebug.hpp"
21
22#include "ConfigFileBuffer.hpp"
23#include "Helpers/helpers.hpp"
24#include "lists.hpp"
25#include "Helpers/Verbose.hpp"
26#include "Helpers/Log.hpp"
27#include "World.hpp"
28
29/******************************** Functions for class ConfigFileBuffer **********************/
30
31/** Structure containing compare function for Ion_Type sorting.
32 */
33struct IonTypeCompare {
34 bool operator()(const char* s1, const char *s2) const {
35 char number1[8];
36 char number2[8];
37 const char *dummy1, *dummy2;
38 //Log() << Verbose(0) << s1 << " " << s2 << endl;
39 dummy1 = strchr(s1, '_')+sizeof(char)*5; // go just after "Ion_Type"
40 dummy2 = strchr(dummy1, '_');
41 strncpy(number1, dummy1, dummy2-dummy1); // copy the number
42 number1[dummy2-dummy1]='\0';
43 dummy1 = strchr(s2, '_')+sizeof(char)*5; // go just after "Ion_Type"
44 dummy2 = strchr(dummy1, '_');
45 strncpy(number2, dummy1, dummy2-dummy1); // copy the number
46 number2[dummy2-dummy1]='\0';
47 if (atoi(number1) != atoi(number2))
48 return (atoi(number1) < atoi(number2));
49 else {
50 dummy1 = strchr(s1, '_')+sizeof(char);
51 dummy1 = strchr(dummy1, '_')+sizeof(char);
52 dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
53 strncpy(number1, dummy1, dummy2-dummy1); // copy the number
54 number1[dummy2-dummy1]='\0';
55 dummy1 = strchr(s2, '_')+sizeof(char);
56 dummy1 = strchr(dummy1, '_')+sizeof(char);
57 dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
58 strncpy(number2, dummy1, dummy2-dummy1); // copy the number
59 number2[dummy2-dummy1]='\0';
60 return (atoi(number1) < atoi(number2));
61 }
62 }
63};
64
65/** Constructor for ConfigFileBuffer class.
66 */
67ConfigFileBuffer::ConfigFileBuffer() :
68 buffer(NULL),
69 LineMapping(NULL),
70 CurrentLine(0),
71 NoLines(0)
72{
73};
74
75/** Constructor for ConfigFileBuffer class with filename to be parsed.
76 * \param *filename file name
77 */
78ConfigFileBuffer::ConfigFileBuffer(const char * const filename) :
79 buffer(NULL),
80 LineMapping(NULL),
81 CurrentLine(0),
82 NoLines(0)
83{
84 InitFileBuffer(filename);
85}
86
87void ConfigFileBuffer::InitFileBuffer(const char * const filename)
88{
89 ifstream *file= new ifstream(filename);
90 InitFileBuffer(file);
91}
92
93void ConfigFileBuffer::InitFileBuffer(istream *file)
94{
95 char line[MAXSTRINGSIZE];
96
97 RemoveMapping();
98
99 // prescan number of lines
100 if (file->fail()) {
101 DoeLog(1) && (eLog()<< Verbose(1) << "config file missing!" << endl);
102 return;
103 }
104 NoLines = 0; // we're overcounting by one
105 long file_position = file->tellg(); // mark current position
106 do {
107 file->getline(line, MAXSTRINGSIZE-1);
108 NoLines++;
109 } while (!file->eof());
110 file->clear();
111 file->seekg(file_position, ios::beg);
112 DoLog(1) && (Log() << Verbose(1) << NoLines-1 << " lines were recognized." << endl);
113
114 // allocate buffer's 1st dimension
115 if (buffer != NULL) {
116 DoeLog(1) && (eLog()<< Verbose(1) << "FileBuffer->buffer is not NULL!" << endl);
117 return;
118 } else
119 buffer = new char *[NoLines];
120
121 // scan each line and put into buffer
122 int lines=0;
123 int i;
124 do {
125 buffer[lines] = new char[MAXSTRINGSIZE];
126 file->getline(buffer[lines], MAXSTRINGSIZE-1);
127 i = strlen(buffer[lines]);
128 buffer[lines][i] = '\n';
129 buffer[lines][i+1] = '\0';
130 lines++;
131 } while((!file->eof()) && (lines < NoLines));
132 DoLog(1) && (Log() << Verbose(1) << lines-1 << " lines were read into the buffer." << endl);
133 file->clear();
134 file->seekg(file_position, ios::beg);
135
136 InitMapping();
137}
138
139/** Destructor for ConfigFileBuffer class.
140 */
141ConfigFileBuffer::~ConfigFileBuffer()
142{
143 RemoveBuffer();
144 RemoveMapping();
145}
146
147
148/** Create trivial mapping.
149 */
150void ConfigFileBuffer::InitMapping()
151{
152 LineMapping = new int[NoLines];
153 for (int i=0;i<NoLines;i++)
154 LineMapping[i] = i;
155 MappingAllocated = true;
156}
157
158/** Remove allocated mapping.
159 */
160void ConfigFileBuffer::RemoveMapping()
161{
162 delete[](LineMapping);
163 MappingAllocated = false;
164}
165
166/** Remove allocated mapping.
167 */
168void ConfigFileBuffer::RemoveBuffer()
169{
170 for(int i=0;i<NoLines;++i)
171 delete[](buffer[i]);
172 delete[](buffer);
173}
174
175
176/** Creates a mapping for the \a *FileBuffer's lines containing the Ion_Type keyword such that they are sorted.
177 * \a *map on return contains a list of NoAtom entries such that going through the list, yields indices to the
178 * lines in \a *FileBuffer in a sorted manner of the Ion_Type?_? keywords. We assume that ConfigFileBuffer::CurrentLine
179 * points to first Ion_Type entry.
180 * \param *FileBuffer pointer to buffer structure
181 * \param NoAtoms of subsequent lines to look at
182 */
183void ConfigFileBuffer::MapIonTypesInBuffer(const int NoAtoms)
184{
185 map<const char *, int, IonTypeCompare> IonTypeLineMap;
186 if (!MappingAllocated) {
187 InitMapping();
188 }
189
190 // put all into hashed map
191 for (int i=0; i<NoAtoms; ++i) {
192 IonTypeLineMap.insert(pair<const char *, int> (buffer[CurrentLine+i], CurrentLine+i));
193 }
194
195 // fill map
196 int nr=0;
197 for (map<const char *, int, IonTypeCompare>::iterator runner = IonTypeLineMap.begin(); runner != IonTypeLineMap.end(); ++runner) {
198 if (CurrentLine+nr < NoLines)
199 LineMapping[CurrentLine+(nr++)] = runner->second;
200 else {
201 DoeLog(0) && (eLog()<< Verbose(0) << "config::MapIonTypesInBuffer - NoAtoms is wrong: We are past the end of the file!" << endl);
202 performCriticalExit();
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.