Index: src/config.cpp
===================================================================
--- src/config.cpp	(revision 6cb412a55f11f6c6a632854832fd61aa21fb5d9e)
+++ src/config.cpp	(revision d1df9b520a18b7600eb18c25dc98b318ecd308be)
@@ -6,4 +6,154 @@
 
 #include "molecules.hpp"
+
+/******************************** Functions for class ConfigFileBuffer **********************/
+
+/** Structure containing compare function for Ion_Type sorting.
+ */
+struct IonTypeCompare {
+  bool operator()(const char* s1, const char *s2) const {
+    char number1[8];
+    char number2[8];
+    char *dummy1, *dummy2;
+    //cout << s1 << "  " << s2 << endl;
+    dummy1 = strchr(s1, '_')+sizeof(char)*5;  // go just after "Ion_Type"
+    dummy2 = strchr(dummy1, '_');
+    strncpy(number1, dummy1, dummy2-dummy1); // copy the number
+    number1[dummy2-dummy1]='\0';
+    dummy1 = strchr(s2, '_')+sizeof(char)*5;  // go just after "Ion_Type"
+    dummy2 = strchr(dummy1, '_');
+    strncpy(number2, dummy1, dummy2-dummy1); // copy the number
+    number2[dummy2-dummy1]='\0';
+    if (atoi(number1) != atoi(number2))
+      return (atoi(number1) < atoi(number2));
+    else {
+      dummy1 = strchr(s1, '_')+sizeof(char);
+      dummy1 = strchr(dummy1, '_')+sizeof(char);
+      dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
+      strncpy(number1, dummy1, dummy2-dummy1); // copy the number
+      number1[dummy2-dummy1]='\0';
+      dummy1 = strchr(s2, '_')+sizeof(char);
+      dummy1 = strchr(dummy1, '_')+sizeof(char);
+      dummy2 = strchr(dummy1, ' ') < strchr(dummy1, '\t') ? strchr(dummy1, ' ') : strchr(dummy1, '\t');
+      strncpy(number2, dummy1, dummy2-dummy1); // copy the number
+      number2[dummy2-dummy1]='\0';
+      return (atoi(number1) < atoi(number2));
+    }
+  }
+};
+
+/** Constructor for ConfigFileBuffer class.
+ */
+ConfigFileBuffer::ConfigFileBuffer()
+{
+  NoLines = 0;
+  CurrentLine = 0;
+  buffer = NULL;
+  LineMapping = NULL;
+}
+
+/** Constructor for ConfigFileBuffer class with filename to be parsed.
+ * \param *filename file name
+ */
+ConfigFileBuffer::ConfigFileBuffer(char *filename)
+{
+  NoLines = 0;
+  CurrentLine = 0;
+  buffer = NULL;
+  LineMapping = NULL;
+  ifstream *file = NULL;
+  char line[MAXSTRINGSIZE];
+
+  // prescan number of lines
+  file= new ifstream(filename);
+  if (file == NULL) {
+    cerr << "ERROR: config file " << filename << " missing!" << endl;
+    return;
+  }
+  NoLines = 0; // we're overcounting by one
+  long file_position = file->tellg(); // mark current position
+  do {
+    file->getline(line, 256);
+    NoLines++;
+  } while (!file->eof());
+  file->clear();
+  file->seekg(file_position, ios::beg);
+  cout << Verbose(1) << NoLines-1 << " lines were recognized." << endl;
+
+  // allocate buffer's 1st dimension
+  if (buffer != NULL) {
+    cerr << "ERROR: FileBuffer->buffer is not NULL!" << endl;
+    return;
+  } else
+    buffer = (char **) Malloc(sizeof(char *)*NoLines, "ConfigFileBuffer::ConfigFileBuffer: **buffer");
+
+  // scan each line and put into buffer
+  int lines=0;
+  int i;
+  do {
+    buffer[lines] = (char *) Malloc(sizeof(char)*MAXSTRINGSIZE, "ConfigFileBuffer::ConfigFileBuffer: *buffer[]");
+    file->getline(buffer[lines], MAXSTRINGSIZE-1);
+    i = strlen(buffer[lines]);
+    buffer[lines][i] = '\n';
+    buffer[lines][i+1] = '\0';
+    lines++;
+  } while((!file->eof()) && (lines < NoLines));
+  cout << Verbose(1) << lines-1 << " lines were read into the buffer." << endl;
+
+  // close and exit
+  file->close();
+  file->clear();
+  delete(file);
+}
+
+/** Destructor for ConfigFileBuffer class.
+ */
+ConfigFileBuffer::~ConfigFileBuffer()
+{
+  for(int i=0;i<NoLines;++i)
+    Free((void **)&buffer[i], "ConfigFileBuffer::~ConfigFileBuffer: *buffer[]");
+  Free((void **)&buffer, "ConfigFileBuffer::~ConfigFileBuffer: **buffer");
+  Free((void **)&LineMapping, "ConfigFileBuffer::~ConfigFileBuffer: *LineMapping");
+}
+
+
+/** Create trivial mapping.
+ */
+void ConfigFileBuffer::InitMapping()
+{
+  LineMapping = (int *) Malloc(sizeof(int)*NoLines, "ConfigFileBuffer::InitMapping: *LineMapping");
+  for (int i=0;i<NoLines;i++)
+    LineMapping[i] = i;
+}
+
+/** Creates a mapping for the \a *FileBuffer's lines containing the Ion_Type keyword such that they are sorted.
+ * \a *map on return contains a list of NoAtom entries such that going through the list, yields indices to the
+ * lines in \a *FileBuffer in a sorted manner of the Ion_Type?_? keywords. We assume that ConfigFileBuffer::CurrentLine
+ * points to first Ion_Type entry.
+ * \param *FileBuffer pointer to buffer structure
+ * \param NoAtoms of subsequent lines to look at
+ */
+void ConfigFileBuffer::MapIonTypesInBuffer(int NoAtoms)
+{
+  map<const char *, int, IonTypeCompare> LineList;
+  if (LineMapping == NULL) {
+    cerr << "map pointer is NULL: " << LineMapping << endl;
+    return;
+  }
+
+  // put all into hashed map
+  for (int i=0; i<NoAtoms; ++i) {
+    LineList.insert(pair<const char *, int> (buffer[CurrentLine+i], CurrentLine+i));
+  }
+
+  // fill map
+  int nr=0;
+  for (map<const char *, int, IonTypeCompare>::iterator runner = LineList.begin(); runner != LineList.end(); ++runner) {
+    if (CurrentLine+nr < NoLines)
+      LineMapping[CurrentLine+(nr++)] = runner->second;
+    else
+      cerr << "config::MapIonTypesInBuffer - NoAtoms is wrong: We are past the end of the file!" << endl;
+  }
+}
 
 /************************************* Functions for class config ***************************/
@@ -447,11 +597,9 @@
 void config::Load(char *filename, periodentafel *periode, molecule *mol)
 {
-	ifstream *file = new ifstream(filename);
-	if (file == NULL) {
-		cerr << "ERROR: config file " << filename << " missing!" << endl;
-		return;
-	}
 	RetrieveConfigPathAndName(filename);
-	// ParseParameters
+
+	// ParseParameterFile
+  struct ConfigFileBuffer *FileBuffer = new ConfigFileBuffer(filename);
+	FileBuffer->InitMapping();
 
 	/* Oeffne Hauptparameterdatei */
@@ -469,14 +617,14 @@
 	/* Namen einlesen */
 
-	ParseForParameter(verbose,file, "mainname", 0, 1, 1, string_type, (config::mainname), 1, critical);
-	ParseForParameter(verbose,file, "defaultpath", 0, 1, 1, string_type, (config::defaultpath), 1, critical);
-	ParseForParameter(verbose,file, "pseudopotpath", 0, 1, 1, string_type, (config::pseudopotpath), 1, critical);
-	ParseForParameter(verbose,file,"ProcPEGamma", 0, 1, 1, int_type, &(config::ProcPEGamma), 1, critical);
-	ParseForParameter(verbose,file,"ProcPEPsi", 0, 1, 1, int_type, &(config::ProcPEPsi), 1, critical);
-
-	if (!ParseForParameter(verbose,file,"Seed", 0, 1, 1, int_type, &(config::Seed), 1, optional))
+	ParseForParameter(verbose,FileBuffer, "mainname", 0, 1, 1, string_type, (config::mainname), 1, critical);
+	ParseForParameter(verbose,FileBuffer, "defaultpath", 0, 1, 1, string_type, (config::defaultpath), 1, critical);
+	ParseForParameter(verbose,FileBuffer, "pseudopotpath", 0, 1, 1, string_type, (config::pseudopotpath), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"ProcPEGamma", 0, 1, 1, int_type, &(config::ProcPEGamma), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"ProcPEPsi", 0, 1, 1, int_type, &(config::ProcPEPsi), 1, critical);
+
+	if (!ParseForParameter(verbose,FileBuffer,"Seed", 0, 1, 1, int_type, &(config::Seed), 1, optional))
 		config::Seed = 1;
 
-	if(!ParseForParameter(verbose,file,"DoOutOrbitals", 0, 1, 1, int_type, &(config::DoOutOrbitals), 1, optional)) {
+	if(!ParseForParameter(verbose,FileBuffer,"DoOutOrbitals", 0, 1, 1, int_type, &(config::DoOutOrbitals), 1, optional)) {
 		config::DoOutOrbitals = 0;
 	} else {
@@ -484,22 +632,22 @@
 		if (config::DoOutOrbitals > 1) config::DoOutOrbitals = 1;
 	}
-	ParseForParameter(verbose,file,"DoOutVis", 0, 1, 1, int_type, &(config::DoOutVis), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"DoOutVis", 0, 1, 1, int_type, &(config::DoOutVis), 1, critical);
 	if (config::DoOutVis < 0) config::DoOutVis = 0;
 	if (config::DoOutVis > 1) config::DoOutVis = 1;
-	if (!ParseForParameter(verbose,file,"VectorPlane", 0, 1, 1, int_type, &(config::VectorPlane), 1, optional))
+	if (!ParseForParameter(verbose,FileBuffer,"VectorPlane", 0, 1, 1, int_type, &(config::VectorPlane), 1, optional))
 		config::VectorPlane = -1;
-	if (!ParseForParameter(verbose,file,"VectorCut", 0, 1, 1, double_type, &(config::VectorCut), 1, optional))
+	if (!ParseForParameter(verbose,FileBuffer,"VectorCut", 0, 1, 1, double_type, &(config::VectorCut), 1, optional))
 		config::VectorCut = 0.;
-	ParseForParameter(verbose,file,"DoOutMes", 0, 1, 1, int_type, &(config::DoOutMes), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"DoOutMes", 0, 1, 1, int_type, &(config::DoOutMes), 1, critical);
 	if (config::DoOutMes < 0) config::DoOutMes = 0;
 	if (config::DoOutMes > 1) config::DoOutMes = 1;
-	if (!ParseForParameter(verbose,file,"DoOutCurr", 0, 1, 1, int_type, &(config::DoOutCurrent), 1, optional))
+	if (!ParseForParameter(verbose,FileBuffer,"DoOutCurr", 0, 1, 1, int_type, &(config::DoOutCurrent), 1, optional))
 		config::DoOutCurrent = 0;
 	if (config::DoOutCurrent < 0) config::DoOutCurrent = 0;
 	if (config::DoOutCurrent > 1) config::DoOutCurrent = 1;
-	ParseForParameter(verbose,file,"AddGramSch", 0, 1, 1, int_type, &(config::UseAddGramSch), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"AddGramSch", 0, 1, 1, int_type, &(config::UseAddGramSch), 1, critical);
 	if (config::UseAddGramSch < 0) config::UseAddGramSch = 0;
 	if (config::UseAddGramSch > 2) config::UseAddGramSch = 2;
-	if(!ParseForParameter(verbose,file,"DoWannier", 0, 1, 1, int_type, &(config::DoWannier), 1, optional)) {
+	if(!ParseForParameter(verbose,FileBuffer,"DoWannier", 0, 1, 1, int_type, &(config::DoWannier), 1, optional)) {
 		config::DoWannier = 0;
 	} else {
@@ -507,5 +655,5 @@
 		if (config::DoWannier > 1) config::DoWannier = 1;
 	}
-	if(!ParseForParameter(verbose,file,"CommonWannier", 0, 1, 1, int_type, &(config::CommonWannier), 1, optional)) {
+	if(!ParseForParameter(verbose,FileBuffer,"CommonWannier", 0, 1, 1, int_type, &(config::CommonWannier), 1, optional)) {
 		config::CommonWannier = 0;
 	} else {
@@ -513,5 +661,5 @@
 		if (config::CommonWannier > 4) config::CommonWannier = 4;
 	}
-	if(!ParseForParameter(verbose,file,"SawtoothStart", 0, 1, 1, double_type, &(config::SawtoothStart), 1, optional)) {
+	if(!ParseForParameter(verbose,FileBuffer,"SawtoothStart", 0, 1, 1, double_type, &(config::SawtoothStart), 1, optional)) {
 		config::SawtoothStart = 0.01;
 	} else {
@@ -520,33 +668,33 @@
 	}
 
-	ParseForParameter(verbose,file,"MaxOuterStep", 0, 1, 1, int_type, &(config::MaxOuterStep), 1, critical);
-	if (!ParseForParameter(verbose,file,"Deltat", 0, 1, 1, double_type, &(config::Deltat), 1, optional))
+	ParseForParameter(verbose,FileBuffer,"MaxOuterStep", 0, 1, 1, int_type, &(config::MaxOuterStep), 1, critical);
+	if (!ParseForParameter(verbose,FileBuffer,"Deltat", 0, 1, 1, double_type, &(config::Deltat), 1, optional))
 		config::Deltat = 1;
-	ParseForParameter(verbose,file,"OutVisStep", 0, 1, 1, int_type, &(config::OutVisStep), 1, optional);
-	ParseForParameter(verbose,file,"OutSrcStep", 0, 1, 1, int_type, &(config::OutSrcStep), 1, optional);
-	ParseForParameter(verbose,file,"TargetTemp", 0, 1, 1, double_type, &(config::TargetTemp), 1, optional);
-	//ParseForParameter(verbose,file,"Thermostat", 0, 1, 1, int_type, &(config::ScaleTempStep), 1, optional);
-	if (!ParseForParameter(verbose,file,"EpsWannier", 0, 1, 1, double_type, &(config::EpsWannier), 1, optional))
+	ParseForParameter(verbose,FileBuffer,"OutVisStep", 0, 1, 1, int_type, &(config::OutVisStep), 1, optional);
+	ParseForParameter(verbose,FileBuffer,"OutSrcStep", 0, 1, 1, int_type, &(config::OutSrcStep), 1, optional);
+	ParseForParameter(verbose,FileBuffer,"TargetTemp", 0, 1, 1, double_type, &(config::TargetTemp), 1, optional);
+	//ParseForParameter(verbose,FileBuffer,"Thermostat", 0, 1, 1, int_type, &(config::ScaleTempStep), 1, optional);
+	if (!ParseForParameter(verbose,FileBuffer,"EpsWannier", 0, 1, 1, double_type, &(config::EpsWannier), 1, optional))
 		config::EpsWannier = 1e-8;
 
 	// stop conditions
 	//if (config::MaxOuterStep <= 0) config::MaxOuterStep = 1;
-	ParseForParameter(verbose,file,"MaxPsiStep", 0, 1, 1, int_type, &(config::MaxPsiStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxPsiStep", 0, 1, 1, int_type, &(config::MaxPsiStep), 1, critical);
 	if (config::MaxPsiStep <= 0) config::MaxPsiStep = 3;
 
-	ParseForParameter(verbose,file,"MaxMinStep", 0, 1, 1, int_type, &(config::MaxMinStep), 1, critical);
-	ParseForParameter(verbose,file,"RelEpsTotalE", 0, 1, 1, double_type, &(config::RelEpsTotalEnergy), 1, critical);
-	ParseForParameter(verbose,file,"RelEpsKineticE", 0, 1, 1, double_type, &(config::RelEpsKineticEnergy), 1, critical);
-	ParseForParameter(verbose,file,"MaxMinStopStep", 0, 1, 1, int_type, &(config::MaxMinStopStep), 1, critical);
-	ParseForParameter(verbose,file,"MaxMinGapStopStep", 0, 1, 1, int_type, &(config::MaxMinGapStopStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxMinStep", 0, 1, 1, int_type, &(config::MaxMinStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"RelEpsTotalE", 0, 1, 1, double_type, &(config::RelEpsTotalEnergy), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"RelEpsKineticE", 0, 1, 1, double_type, &(config::RelEpsKineticEnergy), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxMinStopStep", 0, 1, 1, int_type, &(config::MaxMinStopStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxMinGapStopStep", 0, 1, 1, int_type, &(config::MaxMinGapStopStep), 1, critical);
 	if (config::MaxMinStep <= 0) config::MaxMinStep = config::MaxPsiStep;
 	if (config::MaxMinStopStep < 1) config::MaxMinStopStep = 1;
 	if (config::MaxMinGapStopStep < 1) config::MaxMinGapStopStep = 1;
 
-	ParseForParameter(verbose,file,"MaxInitMinStep", 0, 1, 1, int_type, &(config::MaxInitMinStep), 1, critical);
-	ParseForParameter(verbose,file,"InitRelEpsTotalE", 0, 1, 1, double_type, &(config::InitRelEpsTotalEnergy), 1, critical);
-	ParseForParameter(verbose,file,"InitRelEpsKineticE", 0, 1, 1, double_type, &(config::InitRelEpsKineticEnergy), 1, critical);
-	ParseForParameter(verbose,file,"InitMaxMinStopStep", 0, 1, 1, int_type, &(config::InitMaxMinStopStep), 1, critical);
-	ParseForParameter(verbose,file,"InitMaxMinGapStopStep", 0, 1, 1, int_type, &(config::InitMaxMinGapStopStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxInitMinStep", 0, 1, 1, int_type, &(config::MaxInitMinStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"InitRelEpsTotalE", 0, 1, 1, double_type, &(config::InitRelEpsTotalEnergy), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"InitRelEpsKineticE", 0, 1, 1, double_type, &(config::InitRelEpsKineticEnergy), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"InitMaxMinStopStep", 0, 1, 1, int_type, &(config::InitMaxMinStopStep), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"InitMaxMinGapStopStep", 0, 1, 1, int_type, &(config::InitMaxMinGapStopStep), 1, critical);
 	if (config::MaxInitMinStep <= 0) config::MaxInitMinStep = config::MaxPsiStep;
 	if (config::InitMaxMinStopStep < 1) config::InitMaxMinStopStep = 1;
@@ -554,5 +702,5 @@
 
 	// Unit cell and magnetic field
-	ParseForParameter(verbose,file, "BoxLength", 0, 3, 3, lower_trigrid, BoxLength, 1, critical); /* Lattice->RealBasis */
+	ParseForParameter(verbose,FileBuffer, "BoxLength", 0, 3, 3, lower_trigrid, BoxLength, 1, critical); /* Lattice->RealBasis */
 	mol->cell_size[0] = BoxLength[0];
 	mol->cell_size[1] = BoxLength[3];
@@ -561,9 +709,9 @@
 	mol->cell_size[4] = BoxLength[7];
 	mol->cell_size[5] = BoxLength[8];
-	if (1) fprintf(stderr,"\n");
-
-	ParseForParameter(verbose,file,"DoPerturbation", 0, 1, 1, int_type, &(config::DoPerturbation), 1, optional);
-	ParseForParameter(verbose,file,"DoOutNICS", 0, 1, 1, int_type, &(config::DoOutNICS), 1, optional);
-	if (!ParseForParameter(verbose,file,"DoFullCurrent", 0, 1, 1, int_type, &(config::DoFullCurrent), 1, optional))
+	//if (1) fprintf(stderr,"\n");
+
+	ParseForParameter(verbose,FileBuffer,"DoPerturbation", 0, 1, 1, int_type, &(config::DoPerturbation), 1, optional);
+	ParseForParameter(verbose,FileBuffer,"DoOutNICS", 0, 1, 1, int_type, &(config::DoOutNICS), 1, optional);
+	if (!ParseForParameter(verbose,FileBuffer,"DoFullCurrent", 0, 1, 1, int_type, &(config::DoFullCurrent), 1, optional))
 		config::DoFullCurrent = 0;
 	if (config::DoFullCurrent < 0) config::DoFullCurrent = 0;
@@ -576,11 +724,11 @@
 	}
 
-	ParseForParameter(verbose,file,"ECut", 0, 1, 1, double_type, &(config::ECut), 1, critical);
-	ParseForParameter(verbose,file,"MaxLevel", 0, 1, 1, int_type, &(config::MaxLevel), 1, critical);
-	ParseForParameter(verbose,file,"Level0Factor", 0, 1, 1, int_type, &(config::Lev0Factor), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"ECut", 0, 1, 1, double_type, &(config::ECut), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxLevel", 0, 1, 1, int_type, &(config::MaxLevel), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"Level0Factor", 0, 1, 1, int_type, &(config::Lev0Factor), 1, critical);
 	if (config::Lev0Factor < 2) {
 		config::Lev0Factor = 2;
 	}
-	ParseForParameter(verbose,file,"RiemannTensor", 0, 1, 1, int_type, &di, 1, critical);
+	ParseForParameter(verbose,FileBuffer,"RiemannTensor", 0, 1, 1, int_type, &di, 1, critical);
 	if (di >= 0 && di < 2) {
 		config::RiemannTensor = di;
@@ -601,5 +749,5 @@
 				config::MaxLevel = 3;
 			}
-			ParseForParameter(verbose,file,"RiemannLevel", 0, 1, 1, int_type, &(config::RiemannLevel), 1, critical);
+			ParseForParameter(verbose,FileBuffer,"RiemannLevel", 0, 1, 1, int_type, &(config::RiemannLevel), 1, critical);
 			if (config::RiemannLevel < 2) {
 				config::RiemannLevel = 2;
@@ -608,5 +756,5 @@
 				config::RiemannLevel = config::MaxLevel-1;
 			}
-			ParseForParameter(verbose,file,"LevRFactor", 0, 1, 1, int_type, &(config::LevRFactor), 1, critical);
+			ParseForParameter(verbose,FileBuffer,"LevRFactor", 0, 1, 1, int_type, &(config::LevRFactor), 1, critical);
 			if (config::LevRFactor < 2) {
 				config::LevRFactor = 2;
@@ -616,5 +764,5 @@
 			break;
 	}
-	ParseForParameter(verbose,file,"PsiType", 0, 1, 1, int_type, &di, 1, critical);
+	ParseForParameter(verbose,FileBuffer,"PsiType", 0, 1, 1, int_type, &di, 1, critical);
 	if (di >= 0 && di < 2) {
 		config::PsiType = di;
@@ -625,12 +773,12 @@
 	switch (config::PsiType) {
 	case 0: // SpinDouble
-		ParseForParameter(verbose,file,"MaxPsiDouble", 0, 1, 1, int_type, &(config::MaxPsiDouble), 1, critical);
-		ParseForParameter(verbose,file,"AddPsis", 0, 1, 1, int_type, &(config::AddPsis), 1, optional);
+		ParseForParameter(verbose,FileBuffer,"MaxPsiDouble", 0, 1, 1, int_type, &(config::MaxPsiDouble), 1, critical);
+		ParseForParameter(verbose,FileBuffer,"AddPsis", 0, 1, 1, int_type, &(config::AddPsis), 1, optional);
 		break;
 	case 1: // SpinUpDown
 		if (config::ProcPEGamma % 2) config::ProcPEGamma*=2;
-		ParseForParameter(verbose,file,"PsiMaxNoUp", 0, 1, 1, int_type, &(config::PsiMaxNoUp), 1, critical);
-		ParseForParameter(verbose,file,"PsiMaxNoDown", 0, 1, 1, int_type, &(config::PsiMaxNoDown), 1, critical);
-		ParseForParameter(verbose,file,"AddPsis", 0, 1, 1, int_type, &(config::AddPsis), 1, optional);
+		ParseForParameter(verbose,FileBuffer,"PsiMaxNoUp", 0, 1, 1, int_type, &(config::PsiMaxNoUp), 1, critical);
+		ParseForParameter(verbose,FileBuffer,"PsiMaxNoDown", 0, 1, 1, int_type, &(config::PsiMaxNoDown), 1, critical);
+		ParseForParameter(verbose,FileBuffer,"AddPsis", 0, 1, 1, int_type, &(config::AddPsis), 1, optional);
 		break;
 	}
@@ -638,10 +786,10 @@
 	// IonsInitRead
 
-	ParseForParameter(verbose,file,"RCut", 0, 1, 1, double_type, &(config::RCut), 1, critical);
-	ParseForParameter(verbose,file,"IsAngstroem", 0, 1, 1, int_type, &(config::IsAngstroem), 1, critical);
-	ParseForParameter(verbose,file,"MaxTypes", 0, 1, 1, int_type, &(config::MaxTypes), 1, critical);
-	if (!ParseForParameter(verbose,file,"RelativeCoord", 0, 1, 1, int_type, &(config::RelativeCoord) , 1, optional))
+	ParseForParameter(verbose,FileBuffer,"RCut", 0, 1, 1, double_type, &(config::RCut), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"IsAngstroem", 0, 1, 1, int_type, &(config::IsAngstroem), 1, critical);
+	ParseForParameter(verbose,FileBuffer,"MaxTypes", 0, 1, 1, int_type, &(config::MaxTypes), 1, critical);
+	if (!ParseForParameter(verbose,FileBuffer,"RelativeCoord", 0, 1, 1, int_type, &(config::RelativeCoord) , 1, optional))
 		config::RelativeCoord = 0;
-	if (!ParseForParameter(verbose,file,"StructOpt", 0, 1, 1, int_type, &(config::StructOpt), 1, optional))
+	if (!ParseForParameter(verbose,FileBuffer,"StructOpt", 0, 1, 1, int_type, &(config::StructOpt), 1, optional))
 		config::StructOpt = 0;
 	if (MaxTypes == 0) {
@@ -650,14 +798,30 @@
 		// prescan number of ions per type
 		cout << Verbose(0) << "Prescanning ions per type: " << endl;
+		int NoAtoms = 0;
 		for (int i=0; i < config::MaxTypes; i++) {
 			sprintf(name,"Ion_Type%i",i+1);
-			ParseForParameter(verbose,file, (const char*)name, 0, 1, 1, int_type, &No[i], 1, critical);
-			ParseForParameter(verbose,file, name, 0, 2, 1, int_type, &Z, 1, critical);
+			ParseForParameter(verbose,FileBuffer, (const char*)name, 0, 1, 1, int_type, &No[i], 1, critical);
+			ParseForParameter(verbose,FileBuffer, name, 0, 2, 1, int_type, &Z, 1, critical);
 			elementhash[i] = periode->FindElement(Z);
 			cout << Verbose(1) << i << ". Z = " << elementhash[i]->Z << " with " << No[i] << " ions." << endl;
+			NoAtoms += No[i];
 		}
 		int repetition = 0; // which repeated keyword shall be read
 
+		// sort the lines via the LineMapping
+    sprintf(name,"Ion_Type%i",config::MaxTypes);
+		if (!ParseForParameter(verbose,FileBuffer, (const char*)name, 1, 1, 1, int_type, &value[0], 1, critical)) {
+		  cerr << "There are no atoms in the config file!" << endl;
+      return;
+    }
+		FileBuffer->CurrentLine++;
+		//cout << FileBuffer->buffer[ FileBuffer->LineMapping[FileBuffer->CurrentLine]];
+		FileBuffer->MapIonTypesInBuffer(NoAtoms);
+		//for (int i=0; i<(NoAtoms < 100 ? NoAtoms : 100 < 100 ? NoAtoms : 100);++i) {
+		//	cout << FileBuffer->buffer[ FileBuffer->LineMapping[FileBuffer->CurrentLine+i]];
+		//}
+
 		map<int, atom *> AtomList[config::MaxTypes];
+		map<int, atom *> LinearList;
 		if (!FastParsing) {
 			// parse in trajectories
@@ -673,13 +837,13 @@
 							neues = new atom();
 							AtomList[i][j] = neues;
+							LinearList[FileBuffer->CurrentLine] = neues;
 							neues->type = elementhash[i]; // find element type
-							mol->AddAtom(neues);
 						} else
 							neues = AtomList[i][j];
-						status = (status &&
-										ParseForParameter(verbose,file, keyword, 0, 1, 1, double_type, &neues->x.x[0], 1, (repetition == 0) ? critical : optional) &&
-										ParseForParameter(verbose,file, keyword, 0, 2, 1, double_type, &neues->x.x[1], 1, (repetition == 0) ? critical : optional) &&
-										ParseForParameter(verbose,file, keyword, 0, 3, 1, double_type, &neues->x.x[2], 1, (repetition == 0) ? critical : optional) &&
-										ParseForParameter(verbose,file, keyword, 0, 4, 1, int_type, &neues->FixedIon, 1, (repetition == 0) ? critical : optional));
+            status = (status &&
+										ParseForParameter(verbose,FileBuffer, keyword, 0, 1, 1, double_type, &neues->x.x[0], 1, (repetition == 0) ? critical : optional) &&
+										ParseForParameter(verbose,FileBuffer, keyword, 0, 2, 1, double_type, &neues->x.x[1], 1, (repetition == 0) ? critical : optional) &&
+										ParseForParameter(verbose,FileBuffer, keyword, 0, 3, 1, double_type, &neues->x.x[2], 1, (repetition == 0) ? critical : optional) &&
+										ParseForParameter(verbose,FileBuffer, keyword, 0, 4, 1, int_type, &neues->FixedIon, 1, (repetition == 0) ? critical : optional));
 						if (!status) break;
 
@@ -697,9 +861,9 @@
 
 						// parse velocities if present
-						if(!ParseForParameter(verbose,file, keyword, 0, 5, 1, double_type, &neues->v.x[0], 1,optional))
+						if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 5, 1, double_type, &neues->v.x[0], 1,optional))
 							neues->v.x[0] = 0.;
-						if(!ParseForParameter(verbose,file, keyword, 0, 6, 1, double_type, &neues->v.x[1], 1,optional))
+						if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 6, 1, double_type, &neues->v.x[1], 1,optional))
 							neues->v.x[1] = 0.;
-						if(!ParseForParameter(verbose,file, keyword, 0, 7, 1, double_type, &neues->v.x[2], 1,optional))
+						if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 7, 1, double_type, &neues->v.x[2], 1,optional))
 							neues->v.x[2] = 0.;
 						for (int d=0;d<NDIM;d++)
@@ -707,9 +871,9 @@
 
 						// parse forces if present
-						if(!ParseForParameter(verbose,file, keyword, 0, 8, 1, double_type, &value[0], 1,optional))
+						if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 8, 1, double_type, &value[0], 1,optional))
 							value[0] = 0.;
-						if(!ParseForParameter(verbose,file, keyword, 0, 9, 1, double_type, &value[1], 1,optional))
+						if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 9, 1, double_type, &value[1], 1,optional))
 							value[1] = 0.;
-						if(!ParseForParameter(verbose,file, keyword, 1, 10, 1, double_type, &value[2], 1,optional))
+						if(!ParseForParameter(verbose,FileBuffer, keyword, 1, 10, 1, double_type, &value[2], 1,optional))
 							value[2] = 0.;
 						for (int d=0;d<NDIM;d++)
@@ -730,4 +894,8 @@
 				repetition++;
 			}
+			// put atoms into the molecule in their original order
+			for(map<int, atom*>::iterator runner = LinearList.begin(); runner != LinearList.end(); ++runner) {
+				mol->AddAtom(runner->second);
+			}
 			repetition--;
 			cout << "Found " << repetition << " trajectory steps." << endl;
@@ -736,7 +904,7 @@
 			// find the maximum number of MD steps so that we may parse last one (Ion_Type1_1 must always be present, because is the first atom)
 			repetition = 0;
-			while ( ParseForParameter(verbose,file, "Ion_Type1_1", 0, 1, 1, double_type, &value[0], repetition, (repetition == 0) ? critical : optional) &&
-							ParseForParameter(verbose,file, "Ion_Type1_1", 0, 2, 1, double_type, &value[1], repetition, (repetition == 0) ? critical : optional) &&
-							ParseForParameter(verbose,file, "Ion_Type1_1", 0, 3, 1, double_type, &value[2], repetition, (repetition == 0) ? critical : optional))
+			while ( ParseForParameter(verbose,FileBuffer, "Ion_Type1_1", 0, 1, 1, double_type, &value[0], repetition, (repetition == 0) ? critical : optional) &&
+							ParseForParameter(verbose,FileBuffer, "Ion_Type1_1", 0, 2, 1, double_type, &value[1], repetition, (repetition == 0) ? critical : optional) &&
+							ParseForParameter(verbose,FileBuffer, "Ion_Type1_1", 0, 3, 1, double_type, &value[2], repetition, (repetition == 0) ? critical : optional))
 				repetition++;
 			cout << "I found " << repetition << " times the keyword Ion_Type1_1." << endl;
@@ -748,13 +916,13 @@
 					atom *neues = new atom();
 					// then parse for each atom the coordinates as often as present
-					ParseForParameter(verbose,file, keyword, 0, 1, 1, double_type, &neues->x.x[0], repetition,critical);
-					ParseForParameter(verbose,file, keyword, 0, 2, 1, double_type, &neues->x.x[1], repetition,critical);
-					ParseForParameter(verbose,file, keyword, 0, 3, 1, double_type, &neues->x.x[2], repetition,critical);
-					ParseForParameter(verbose,file, keyword, 0, 4, 1, int_type, &neues->FixedIon, repetition,critical);
-					if(!ParseForParameter(verbose,file, keyword, 0, 5, 1, double_type, &neues->v.x[0], repetition,optional))
+					ParseForParameter(verbose,FileBuffer, keyword, 0, 1, 1, double_type, &neues->x.x[0], repetition,critical);
+					ParseForParameter(verbose,FileBuffer, keyword, 0, 2, 1, double_type, &neues->x.x[1], repetition,critical);
+					ParseForParameter(verbose,FileBuffer, keyword, 0, 3, 1, double_type, &neues->x.x[2], repetition,critical);
+					ParseForParameter(verbose,FileBuffer, keyword, 0, 4, 1, int_type, &neues->FixedIon, repetition,critical);
+					if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 5, 1, double_type, &neues->v.x[0], repetition,optional))
 						neues->v.x[0] = 0.;
-					if(!ParseForParameter(verbose,file, keyword, 0, 6, 1, double_type, &neues->v.x[1], repetition,optional))
+					if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 6, 1, double_type, &neues->v.x[1], repetition,optional))
 						neues->v.x[1] = 0.;
-					if(!ParseForParameter(verbose,file, keyword, 0, 7, 1, double_type, &neues->v.x[2], repetition,optional))
+					if(!ParseForParameter(verbose,FileBuffer, keyword, 0, 7, 1, double_type, &neues->v.x[2], repetition,optional))
 						neues->v.x[2] = 0.;
 					// here we don't care if forces are present (last in trajectories is always equal to current position)
@@ -765,6 +933,5 @@
 		}
 	}
-	file->close();
-	delete(file);
+	delete(FileBuffer);
 };
 
@@ -1203,5 +1370,5 @@
  * \warning value is modified (both in contents and position)!
  * \param verbose 1 - print found value to stderr, 0 - don't
- * \param file file to be parsed
+ * \param *file file to be parsed
  * \param name Name of value in file (at least 3 chars!)
  * \param sequential 1 - do not reset file pointer to begin of file, 0 - set to beginning
@@ -1432,2 +1599,211 @@
 	return (found); // true if found, false if not
 }
+
+
+/** Reads parameter from a parsed file.
+ * The file is either parsed for a certain keyword or if null is given for
+ * the value in row yth and column xth. If the keyword was necessity#critical,
+ * then an error is thrown and the programme aborted.
+ * \warning value is modified (both in contents and position)!
+ * \param verbose 1 - print found value to stderr, 0 - don't
+ * \param *FileBuffer pointer to buffer structure
+ * \param name Name of value in file (at least 3 chars!)
+ * \param sequential 1 - do not reset file pointer to begin of file, 0 - set to beginning
+ *        (if file is sequentially parsed this can be way faster! However, beware of multiple values per line, as whole line is read -
+ *         best approach: 0 0 0 1 (not resetted only on last value of line) - and of yth, which is now
+ *         counted from this unresetted position!)
+ * \param xth Which among a number of parameters it is (in grid case it's row number as grid is read as a whole!)
+ * \param yth In grid case specifying column number, otherwise the yth \a name matching line
+ * \param type Type of the Parameter to be read
+ * \param value address of the value to be read (must have been allocated)
+ * \param repetition determines, if the keyword appears multiply in the config file, which repetition shall be parsed, i.e. 1 if not multiply
+ * \param critical necessity of this keyword being specified (optional, critical)
+ * \return 1 - found, 0 - not found
+ * \note Routine is taken from the pcp project and hack-a-slack adapted to C++
+ */
+int config::ParseForParameter(int verbose, struct ConfigFileBuffer *FileBuffer, const char *name, int sequential, int const xth, int const yth, int type, void *value, int repetition, int critical) {
+  int i,j;  // loop variables
+  int length = 0, maxlength = -1;
+  int OldCurrentLine = FileBuffer->CurrentLine;
+  char *dummy1, *dummy;    // pointers in the line that is read in per step
+
+  //fprintf(stderr,"Parsing for %s\n",name);
+  if (repetition == 0)
+    //Error(SomeError, "ParseForParameter(): argument repetition must not be 0!");
+    return 0;
+
+  int line = 0; // marks line where parameter was found
+  int found = (type >= grid) ? 0 : (-yth + 1);  // marks if yth parameter name was found
+  while((found != repetition)) {
+    dummy1 = dummy = NULL;
+    do {
+      dummy1 = FileBuffer->buffer[ FileBuffer->LineMapping[FileBuffer->CurrentLine++] ];
+      if (FileBuffer->CurrentLine >= FileBuffer->NoLines) {
+        if ((critical) && (found == 0)) {
+          //Error(InitReading, name);
+          fprintf(stderr,"Error:InitReading, critical %s not found\n", name);
+          exit(255);
+        } else {
+          FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
+          return 0;
+        }
+      }
+      if (dummy1 == NULL) {
+        if (verbose) fprintf(stderr,"Error reading line %i\n",line);
+      } else {
+        //fprintf(stderr,"Now parsing the line %i: %s\n", line, dummy1);
+      }
+      line++;
+    } while (dummy1 != NULL && ((dummy1[0] == '#') || (dummy1[0] == '\0'))); // skip commentary and empty lines
+
+    // Seek for possible end of keyword on line if given ...
+    if (name != NULL) {
+      dummy = strchr(dummy1,'\t');  // set dummy on first tab or space which ever's nearer
+      if (dummy == NULL) {
+        dummy = strchr(dummy1, ' ');  // if not found seek for space
+        while ((dummy != NULL) && ((*dummy == '\t') || (*dummy == ' ')))    // skip some more tabs and spaces if necessary
+          dummy++;
+      }
+      if (dummy == NULL) {
+        dummy = strchr(dummy1, '\n'); // set on line end then (whole line = keyword)
+        //fprintf(stderr,"Error: Cannot find tabs or spaces on line %i in search for %s\n", line, name);
+        //Free((void **)&free_dummy);
+        //Error(FileOpenParams, NULL);
+      } else {
+        //fprintf(stderr,"found tab at %i\n",(char *)dummy-(char *)dummy1);
+      }
+    } else dummy = dummy1;
+    // ... and check if it is the keyword!
+    //fprintf(stderr,"name %p, dummy %i/%c, dummy1 %i/%c, strlen(name) %i\n", &name, dummy, *dummy, dummy1, *dummy1, strlen(name));
+    if ((name == NULL) || (((dummy-dummy1 >= 3) && (strncmp(dummy1, name, strlen(name)) == 0)) && ((unsigned int)(dummy-dummy1) == strlen(name)))) {
+      found++; // found the parameter!
+      //fprintf(stderr,"found %s at line %i between %i and %i\n", name, line, dummy1, dummy);
+
+      if (found == repetition) {
+        for (i=0;i<xth;i++) { // i = rows
+          if (type >= grid) {
+            // grid structure means that grid starts on the next line, not right after keyword
+            dummy1 = dummy = NULL;
+            do {
+              dummy1 = FileBuffer->buffer[ FileBuffer->LineMapping[ FileBuffer->CurrentLine++] ];
+              if (FileBuffer->CurrentLine >= FileBuffer->NoLines) {
+                if ((critical) && (found == 0)) {
+                  //Error(InitReading, name);
+                  fprintf(stderr,"Error:InitReading, critical %s not found\n", name);
+                  exit(255);
+                } else {
+                  FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
+                  return 0;
+                }
+              }
+              if (dummy1 == NULL) {
+                if (verbose) fprintf(stderr,"Error reading line %i\n", line);
+              } else {
+                //fprintf(stderr,"Reading next line %i: %s\n", line, dummy1);
+              }
+              line++;
+            } while (dummy1 != NULL && (dummy1[0] == '#') || (dummy1[0] == '\n'));
+            dummy = dummy1;
+          } else { // simple int, strings or doubles start in the same line
+            while ((*dummy == '\t') || (*dummy == ' '))  // skip interjacent tabs and spaces
+              dummy++;
+          }
+
+          for (j=((type >= grid) ? 0 : yth-1);j<yth;j++) { // j = columns
+            // check for lower triangular area and upper triangular area
+            if ( ((i > j) && (type == upper_trigrid)) || ((j > i) && (type == lower_trigrid))) {
+              *((double *)value) = 0.0;
+              fprintf(stderr,"%f\t",*((double *)value));
+              value = (void *)((long)value + sizeof(double));
+              //value += sizeof(double);
+            } else {
+              // otherwise we must skip all interjacent tabs and spaces and find next value
+              dummy1 = dummy;
+              dummy = strchr(dummy1, '\t'); // seek for tab or space
+              if (dummy == NULL)
+                dummy = strchr(dummy1, ' ');  // if not found seek for space
+              if (dummy == NULL) { // if still zero returned ...
+                dummy = strchr(dummy1, '\n'); // ... at line end then
+                if ((j < yth-1) && (type < 4)) {  // check if xth value or not yet
+                  if (critical) {
+                    if (verbose) fprintf(stderr,"Error: EoL at %i and still missing %i value(s) for parameter %s\n", line, yth-j, name);
+                    //return 0;
+                    exit(255);
+                    //Error(FileOpenParams, NULL);
+                  } else {
+                    if (!sequential) { // here we need it!
+                      FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
+                    }
+                    return 0;
+                  }
+                }
+              } else {
+                //fprintf(stderr,"found tab at %i\n",(char *)dummy-(char *)free_dummy);
+              }
+              if (*dummy1 == '#') {
+                // found comment, skipping rest of line
+                //if (verbose) fprintf(stderr,"Error: '#' at %i and still missing %i value(s) for parameter %s\n", line, yth-j, name);
+                if (!sequential) { // here we need it!
+                  FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
+                }
+                return 0;
+              }
+              //fprintf(stderr,"value from %i to %i\n",(char *)dummy1-(char *)free_dummy,(char *)dummy-(char *)free_dummy);
+              switch(type) {
+                case (row_int):
+                  *((int *)value) = atoi(dummy1);
+                  if ((verbose) && (i==0) && (j==0)) fprintf(stderr,"%s = ", name);
+                  if (verbose) fprintf(stderr,"%i\t",*((int *)value));
+                    value = (void *)((long)value + sizeof(int));
+                    //value += sizeof(int);
+                  break;
+                case(row_double):
+                case(grid):
+                case(lower_trigrid):
+                case(upper_trigrid):
+                  *((double *)value) = atof(dummy1);
+                  if ((verbose) && (i==0) && (j==0)) fprintf(stderr,"%s = ", name);
+                  if (verbose) fprintf(stderr,"%lg\t",*((double *)value));
+                  value = (void *)((long)value + sizeof(double));
+                  //value += sizeof(double);
+                  break;
+                case(double_type):
+                  *((double *)value) = atof(dummy1);
+                  if ((verbose) && (i == xth-1)) fprintf(stderr,"%s = %lg\n", name, *((double *) value));
+                  //value += sizeof(double);
+                  break;
+                case(int_type):
+                  *((int *)value) = atoi(dummy1);
+                  if ((verbose) && (i == xth-1)) fprintf(stderr,"%s = %i\n", name, *((int *) value));
+                  //value += sizeof(int);
+                  break;
+                default:
+                case(string_type):
+                  if (value != NULL) {
+                    //if (maxlength == -1) maxlength = strlen((char *)value); // get maximum size of string array
+                    maxlength = MAXSTRINGSIZE;
+                    length = maxlength > (dummy-dummy1) ? (dummy-dummy1) : maxlength; // cap at maximum
+                    strncpy((char *)value, dummy1, length); // copy as much
+                    ((char *)value)[length] = '\0'; // and set end marker
+                    if ((verbose) && (i == xth-1)) fprintf(stderr,"%s is '%s' (%i chars)\n",name,((char *) value), length);
+                    //value += sizeof(char);
+                  } else {
+                  }
+                break;
+              }
+            }
+            while (*dummy == '\t')
+              dummy++;
+          }
+        }
+      }
+    }
+  }
+  if ((type >= row_int) && (verbose)) fprintf(stderr,"\n");
+  if (!sequential) {
+    FileBuffer->CurrentLine = OldCurrentLine; // rewind to start position
+  }
+  //fprintf(stderr, "End of Parsing\n\n");
+
+  return (found); // true if found, false if not
+}
Index: src/molecules.cpp
===================================================================
--- src/molecules.cpp	(revision 6cb412a55f11f6c6a632854832fd61aa21fb5d9e)
+++ src/molecules.cpp	(revision d1df9b520a18b7600eb18c25dc98b318ecd308be)
@@ -1471,5 +1471,4 @@
 	atom *walker = NULL;
 	int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
-	int CurrentMaxElement = 0;
 	CountElements();
 
@@ -1485,6 +1484,14 @@
     while (walker->next != end) { // go through every atom of this element
       walker = walker->next;
-      if (ElementNo[walker->type->Z] == 0) // new element
-        ElementNo[walker->type->Z] = ++CurrentMaxElement;
+      ElementNo[walker->type->Z] = 1;
+    }
+    int current=1;
+    for (int i=0;i<MAX_ELEMENTS;++i) {
+      if (ElementNo[i] == 1)
+        ElementNo[i] = current++;
+    }
+    walker = start;
+    while (walker->next != end) { // go through every atom of this element
+      walker = walker->next;
       AtomNo[walker->type->Z]++;
       walker->Output(ElementNo[walker->type->Z], AtomNo[walker->type->Z], out); // removed due to trajectories
@@ -1501,5 +1508,4 @@
 	atom *walker = NULL;
   int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
-  int CurrentMaxElement = 0;
 	CountElements();
 
@@ -1517,9 +1523,17 @@
 		    ElementNo[i] = 0;
 		  }
-      walker = start;
+    walker = start;
+    while (walker->next != end) { // go through every atom of this element
+      walker = walker->next;
+      ElementNo[walker->type->Z] = 1;
+    }
+    int current=1;
+    for (int i=0;i<MAX_ELEMENTS;++i) {
+      if (ElementNo[i] == 1)
+        ElementNo[i] = current++;
+    }
+    walker = start;
       while (walker->next != end) { // go through every atom of this element
         walker = walker->next;
-        if (ElementNo[walker->type->Z] == 0) // new element
-          ElementNo[walker->type->Z] = ++CurrentMaxElement;
         AtomNo[walker->type->Z]++;
         *out << "Ion_Type" << ElementNo[walker->type->Z] << "_" << AtomNo[walker->type->Z] << "\t"	<< fixed << setprecision(9) << showpoint;
Index: src/molecules.hpp
===================================================================
--- src/molecules.hpp	(revision 6cb412a55f11f6c6a632854832fd61aa21fb5d9e)
+++ src/molecules.hpp	(revision d1df9b520a18b7600eb18c25dc98b318ecd308be)
@@ -392,4 +392,20 @@
 };
 
+class ConfigFileBuffer {
+  public:
+    char **buffer;
+    int *LineMapping;
+    int CurrentLine;
+    int NoLines;
+
+    ConfigFileBuffer();
+    ConfigFileBuffer(char *filename);
+    ~ConfigFileBuffer();
+
+    void InitMapping();
+    void MapIonTypesInBuffer(int NoAtoms);
+};
+
+
 /** The config file.
  * The class contains all parameters that control a dft run also functions to load and save.
@@ -469,4 +485,5 @@
 
 	int ParseForParameter(int verbose, ifstream *file, const char *name, int sequential, int const xth, int const yth, int type, void *value, int repetition, int critical);
+	int ParseForParameter(int verbose, struct ConfigFileBuffer *FileBuffer, const char *name, int sequential, int const xth, int const yth, int type, void *value, int repetition, int critical);
 
 	public:
