source: src/ConfigFileBuffer.cpp@ 43dad6

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 Candidate_v1.7.0 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 43dad6 was 43dad6, checked in by Frederik Heber <heber@…>, 15 years ago

Introducing (but not yet incorporated) FormatParser specializations for MPQC and PCP config files.

  • new modules MpqcParser.[ch]pp and PcpParser.[ch]pp.
  • class MpqcParser:
    • just a skeleton so far
  • class PcpParser:
    • load and save basically adapted from config::Load and config::Save.
    • new functions OutputAtoms() and OutputElements() from molecule::Output() and molecule::CheckOut().
    • ne function ParseThermostats() - as the parsing is specific to the format it should be handled by the FormatParser, not bz ThermoStatContainer.
    • most of the variables from config placed into struct to put them into groups (for later splitting up load&save into subfunctions).
  • new overload ConfigFileBuffer::InitFileBuffer() accepting streams (FormatParser works on streams, config still with filenames).
  • World: new member Thermostats which contains all thermostat parameters with getter function.
  • ThermoStatContainer::ParseThermostats() moved over to PcpParser and re-introduced in config.
  • atom::OutputArrayIndexed() now works on ostream, not ofstream.
  • molecule::Output() adapted to ostream change.
  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * ConfigFileBuffer.cpp
3 *
4 * Created on: 12.06.2010
5 * Author: heber
6 */
7
8#include "ConfigFileBuffer.hpp"
9#include "helpers.hpp"
10#include "lists.hpp"
11#include "log.hpp"
12#include "World.hpp"
13
14/******************************** Functions for class ConfigFileBuffer **********************/
15
16/** Structure containing compare function for Ion_Type sorting.
17 */
18struct IonTypeCompare {
19 bool operator()(const char* s1, const char *s2) const {
20 char number1[8];
21 char number2[8];
22 const char *dummy1, *dummy2;
23 //Log() << Verbose(0) << s1 << " " << s2 << endl;
24 dummy1 = strchr(s1, '_')+sizeof(char)*5; // go just after "Ion_Type"
25 dummy2 = strchr(dummy1, '_');
26 strncpy(number1, dummy1, dummy2-dummy1); // copy the number
27 number1[dummy2-dummy1]='\0';
28 dummy1 = strchr(s2, '_')+sizeof(char)*5; // go just after "Ion_Type"
29 dummy2 = strchr(dummy1, '_');
30 strncpy(number2, dummy1, dummy2-dummy1); // copy the number
31 number2[dummy2-dummy1]='\0';
32 if (atoi(number1) != atoi(number2))
33 return (atoi(number1) < atoi(number2));
34 else {
35 dummy1 = strchr(s1, '_')+sizeof(char);
36 dummy1 = strchr(dummy1, '_')+sizeof(char);
37 dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
38 strncpy(number1, dummy1, dummy2-dummy1); // copy the number
39 number1[dummy2-dummy1]='\0';
40 dummy1 = strchr(s2, '_')+sizeof(char);
41 dummy1 = strchr(dummy1, '_')+sizeof(char);
42 dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
43 strncpy(number2, dummy1, dummy2-dummy1); // copy the number
44 number2[dummy2-dummy1]='\0';
45 return (atoi(number1) < atoi(number2));
46 }
47 }
48};
49
50/** Constructor for ConfigFileBuffer class.
51 */
52ConfigFileBuffer::ConfigFileBuffer() : buffer(NULL), LineMapping(NULL), CurrentLine(0), NoLines(0)
53{
54};
55
56/** Constructor for ConfigFileBuffer class with filename to be parsed.
57 * \param *filename file name
58 */
59ConfigFileBuffer::ConfigFileBuffer(const char * const filename) : buffer(NULL), LineMapping(NULL), CurrentLine(0), NoLines(0)
60{
61 InitFileBuffer(filename);
62}
63
64void ConfigFileBuffer::InitFileBuffer(const char * const filename)
65{
66 ifstream *file= new ifstream(filename);
67 InitFileBuffer(file);
68}
69
70void ConfigFileBuffer::InitFileBuffer(istream *file)
71{
72 char line[MAXSTRINGSIZE];
73
74 RemoveMapping();
75
76 // prescan number of lines
77 if (file->fail()) {
78 DoeLog(1) && (eLog()<< Verbose(1) << "config file missing!" << endl);
79 return;
80 }
81 NoLines = 0; // we're overcounting by one
82 long file_position = file->tellg(); // mark current position
83 do {
84 file->getline(line, 256);
85 NoLines++;
86 } while (!file->eof());
87 file->clear();
88 file->seekg(file_position, ios::beg);
89 DoLog(1) && (Log() << Verbose(1) << NoLines-1 << " lines were recognized." << endl);
90
91 // allocate buffer's 1st dimension
92 if (buffer != NULL) {
93 DoeLog(1) && (eLog()<< Verbose(1) << "FileBuffer->buffer is not NULL!" << endl);
94 return;
95 } else
96 buffer = new char *[NoLines];
97
98 // scan each line and put into buffer
99 int lines=0;
100 int i;
101 do {
102 buffer[lines] = new char[MAXSTRINGSIZE];
103 file->getline(buffer[lines], MAXSTRINGSIZE-1);
104 i = strlen(buffer[lines]);
105 buffer[lines][i] = '\n';
106 buffer[lines][i+1] = '\0';
107 lines++;
108 } while((!file->eof()) && (lines < NoLines));
109 DoLog(1) && (Log() << Verbose(1) << lines-1 << " lines were read into the buffer." << endl);
110
111 InitMapping();
112}
113
114/** Destructor for ConfigFileBuffer class.
115 */
116ConfigFileBuffer::~ConfigFileBuffer()
117{
118 RemoveBuffer();
119 RemoveMapping();
120}
121
122
123/** Create trivial mapping.
124 */
125void ConfigFileBuffer::InitMapping()
126{
127 LineMapping = new int[NoLines];
128 for (int i=0;i<NoLines;i++)
129 LineMapping[i] = i;
130 MappingAllocated = true;
131}
132
133/** Remove allocated mapping.
134 */
135void ConfigFileBuffer::RemoveMapping()
136{
137 delete[](LineMapping);
138 MappingAllocated = false;
139}
140
141/** Remove allocated mapping.
142 */
143void ConfigFileBuffer::RemoveBuffer()
144{
145 for(int i=0;i<NoLines;++i)
146 delete[](buffer[i]);
147 delete[](buffer);
148}
149
150
151/** Creates a mapping for the \a *FileBuffer's lines containing the Ion_Type keyword such that they are sorted.
152 * \a *map on return contains a list of NoAtom entries such that going through the list, yields indices to the
153 * lines in \a *FileBuffer in a sorted manner of the Ion_Type?_? keywords. We assume that ConfigFileBuffer::CurrentLine
154 * points to first Ion_Type entry.
155 * \param *FileBuffer pointer to buffer structure
156 * \param NoAtoms of subsequent lines to look at
157 */
158void ConfigFileBuffer::MapIonTypesInBuffer(const int NoAtoms)
159{
160 map<const char *, int, IonTypeCompare> IonTypeLineMap;
161 if (!MappingAllocated) {
162 InitMapping();
163 }
164
165 // put all into hashed map
166 for (int i=0; i<NoAtoms; ++i) {
167 IonTypeLineMap.insert(pair<const char *, int> (buffer[CurrentLine+i], CurrentLine+i));
168 }
169
170 // fill map
171 int nr=0;
172 for (map<const char *, int, IonTypeCompare>::iterator runner = IonTypeLineMap.begin(); runner != IonTypeLineMap.end(); ++runner) {
173 if (CurrentLine+nr < NoLines)
174 LineMapping[CurrentLine+(nr++)] = runner->second;
175 else {
176 DoeLog(0) && (eLog()<< Verbose(0) << "config::MapIonTypesInBuffer - NoAtoms is wrong: We are past the end of the file!" << endl);
177 performCriticalExit();
178 }
179 }
180}
Note: See TracBrowser for help on using the repository browser.