Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 249 additions & 53 deletions src/IO/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
//============================================================================

#include "io.h"
#include <filesystem>

IO::IO(Apothesis* apothesis):Pointers(apothesis),
m_sLatticeType("NONE"),
m_sProcess("process"),
m_sLattice("lattice"),
m_sRuns("runs"),
m_sTemperature("temperature"),
m_sPressure("pressure"),
m_sTime("time"),
Expand All @@ -31,7 +33,8 @@ IO::IO(Apothesis* apothesis):Pointers(apothesis),
m_sGrowth("growth"),
m_sCommentLine("#"),
m_sPrecursors("precursors"),
m_sReport("report")
m_sReport("report"),
m_sHeights("heights.txt")
{
//Initialize the map for the lattice
m_mLatticeType[ "NONE" ] = Lattice::NONE;
Expand All @@ -53,7 +56,7 @@ string IO::getInputPath() const {;}

void IO::readInputFile()
{
list< string > lKeywords{ m_sLattice, m_sPressure, m_sTemperature, m_sTime, m_sSteps, m_sRandom, m_sSpecies, m_sWrite, m_sGrowth, m_sReport};
list< string > lKeywords{ m_sLattice, m_sPressure, m_sTemperature, m_sTime, m_sSteps, m_sRandom, m_sSpecies, m_sWrite, m_sGrowth, m_sReport,m_sRuns};

string sLine;
while ( getline( m_InputFile, sLine ) ) {
Expand Down Expand Up @@ -95,61 +98,123 @@ void IO::readInputFile()
}
}

if ( vsTokensBasic[ 0].compare( m_sLattice ) == 0 ){

vector<string> vsTokens;
vsTokens = split( vsTokensBasic[ 1 ], string( " " ) );

bool bComment = false;
for ( unsigned int i = 0; i< vsTokens.size(); i++){
if ( !bComment && startsWith( vsTokens[ i ], m_sCommentLine ) )
bComment = true;

// Remove the comments from the tokens so not to consider them
if ( bComment )
vsTokens[ i ].clear();
}

// Remove any empty parts of the vector
vector<string>::iterator it = remove_if( vsTokens.begin(), vsTokens.end(), mem_fun_ref(&string::empty) );
vsTokensBasic.erase( it, vsTokens.end() );

m_parameters->setLatticeType( vsTokens[ 0 ] );

if ( isNumber( vsTokens[ 1 ] ) ){
m_parameters->setLatticeXDim( toInt( trim( vsTokens[ 1 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The x dimension of lattice is not a number.");
EXIT
}

if ( isNumber( vsTokens[ 2 ] ) ){
m_parameters->setLatticeYDim( toInt( trim( vsTokens[ 2 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The y dimension of lattice is not a number.");
EXIT
}

if ( isNumber( vsTokens[ 3 ] ) ){
m_parameters->setLatticeHeight( toInt( trim( vsTokens[ 3 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The height must be a number.");
EXIT
if(vsTokensBasic[0].compare(m_sRuns)==0){
if(isNumber(trim(vsTokensBasic[1]))){
m_parameters->setRuns(toInt(trim(vsTokensBasic[1])));
m_parameters->setMultipleRunFlag();
}
}

if ( !vsTokens[ 4 ].empty() )
m_parameters->setLatticeLabels( vsTokens[4] ) ;
else {
m_errorHandler->error_simple_msg("You must specify a species that the lattice is composed off.");
EXIT
if ( vsTokensBasic[ 0].compare( m_sLattice ) == 0 ){
if ( trim(vsTokensBasic[ 1]).compare( m_sHeights ) == 0) {
// Check if the input line contains a filename for height with the given name of heights.txt
string heightFileName = trim(vsTokensBasic[1]);
ifstream heightFile(heightFileName);

if (heightFile.good()) {
// Read heights from the file
string line;
getline(heightFile, line);
vector<string> vsTokens = split(line, " ");

if (vsTokens.size() != 5 ) {
m_errorHandler->error_simple_msg("Invalid heights header format ");
EXIT
}
m_parameters->setLatticeType( trim( vsTokens[ 1 ] ) );

if ( isNumber( vsTokens[ 2 ] ) ){
m_parameters->setLatticeXDim( toInt( trim( vsTokens[ 2 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The x dimension of lattice is not a number.");
EXIT
}

if ( isNumber( vsTokens[ 3 ] ) ){
m_parameters->setLatticeYDim( toInt( trim( vsTokens[ 3 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("`The y dimension of lattice is not a number.");
EXIT
}
int latticeXDim = m_parameters->getLatticeXDim();
int latticeYDim = m_parameters->getLatticeYDim();
vector<vector<int>> heights(latticeYDim, vector<int>(latticeXDim));

for (int i = 0; i < latticeYDim; ++i) {
for (int j = 0; j < latticeXDim; ++j) {
if (!(heightFile >> heights[i][j])) {
m_errorHandler->error_simple_msg("Error reading heights from file.");
EXIT
}
}
}

m_parameters->setHeightData(heights);
m_parameters->setHeightFileExists(true);
m_parameters->setLatticeLabels(vsTokens[4]);
heightFile.close();
} else {
m_errorHandler->error_simple_msg("Failed to open height file: " + heightFileName);
EXIT
}
}
else{
vector<string> vsTokens;
vsTokens = split( vsTokensBasic[ 1 ], string( " " ) );

bool bComment = false;
for ( unsigned int i = 0; i< vsTokens.size(); i++){
if ( !bComment && startsWith( vsTokens[ i ], m_sCommentLine ) )
bComment = true;

// Remove the comments from the tokens so not to consider them
if ( bComment )
vsTokens[ i ].clear();
}

// Remove any empty parts of the vector
vector<string>::iterator it = remove_if( vsTokens.begin(), vsTokens.end(), mem_fun_ref(&string::empty) );
vsTokensBasic.erase( it, vsTokens.end() );

m_parameters->setLatticeType( vsTokens[ 0 ] );

if ( isNumber( vsTokens[ 1 ] ) ){
m_parameters->setLatticeXDim( toInt( trim( vsTokens[ 1 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The x dimension of lattice is not a number.");
EXIT
}

if ( isNumber( vsTokens[ 2 ] ) ){
m_parameters->setLatticeYDim( toInt( trim( vsTokens[ 2 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The y dimension of lattice is not a number.");
EXIT
}

if ( isNumber( vsTokens[ 3 ] ) ){
m_parameters->setLatticeHeight( toInt( trim( vsTokens[ 3 ] ) ) );
}
else {
m_errorHandler->error_simple_msg("The height must be a number.");
EXIT
}

if ( !vsTokens[ 4 ].empty() )
m_parameters->setLatticeLabels( vsTokens[4] ) ;
else {
m_errorHandler->error_simple_msg("You must specify a species that the lattice is composed off.");
EXIT
}
m_parameters->setHeightFileExists(false);
continue;
}
}

continue;
}

if (vsTokensBasic[ 0].compare( m_sGrowth ) == 0){
vector<string> vsTokens;
vsTokens = split( vsTokensBasic[ 1 ], string( " " ) );
Expand Down Expand Up @@ -786,6 +851,137 @@ pair<string, double> IO::analyzeCompound( string reactant ) {
return react;
}

vector<vector<int>>IO::readHeightFile(string location)
{
string heightFileName = location;
ifstream heightFile(heightFileName);

if (heightFile.good()) {
// Read heights from the file
string line;
getline(heightFile, line);
//skip the first line

int latticeXDim = m_parameters->getLatticeXDim();
int latticeYDim = m_parameters->getLatticeYDim();
vector<vector<int>> heights(latticeYDim, vector<int>(latticeXDim));

for (int i = 0; i < latticeYDim; ++i) {
for (int j = 0; j < latticeXDim; ++j) {
if (!(heightFile >> heights[i][j])) {
m_errorHandler->error_simple_msg("Error reading heights from file.");
EXIT
}
}
}

m_parameters->setHeightData(heights);
m_parameters->setHeightFileExists(true);
//m_parameters->setLatticeLabels(vsTokens[4]);
heightFile.close();
return heights;

}
}


vector<vector<string>>IO::readSpeciesFile(string location)
{
string speciesFileName = location;
ifstream speciesFile(speciesFileName);

if (speciesFile.good()) {
// Read heights from the file
string line;
getline(speciesFile, line);
//skip the first line

int latticeXDim = m_parameters->getLatticeXDim();
int latticeYDim = m_parameters->getLatticeYDim();
vector<vector<string>> species(latticeYDim, vector<string>(latticeXDim));

for (int i = 0; i < latticeYDim; ++i) {
for (int j = 0; j < latticeXDim; ++j) {
if (!( speciesFile>> species[i][j])) {
m_errorHandler->error_simple_msg("Error reading species from file.");
EXIT
}
}
}

speciesFile.close();
return species;

}
}


void IO::writeLatticeHeightsInFolder(double time, const std::string& folder_path)
{
// Ensure the directory exists
std::filesystem::path dir_path(folder_path);
if (!std::filesystem::exists(dir_path)) {
std::filesystem::create_directories(dir_path);
std::cout << "Created directory: " << dir_path << std::endl;
}

// Create the file name
std::ostringstream streamObj;
streamObj.precision(15);
streamObj << time;
std::string file_name = "Height_" + streamObj.str() + ".dat";

// Combine the directory path and file name
std::filesystem::path file_path = dir_path / file_name;

std::ofstream file(file_path);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << file_path << std::endl;
return;
}

file << "Time (s): " << time << std::endl;
for (int i = 0; i < m_lattice->getY(); i++) {
for (int j = 0; j < m_lattice->getX(); j++)
file << m_lattice->getSite(i*m_lattice->getX() + j)->getHeight() << " ";
file << std::endl;
}

file.close();
}


void IO::writeLatticeSpeciesInFolder( double time,const std::string& folder_path )
{
std::filesystem::path dir_path(folder_path);
if (!std::filesystem::exists(dir_path)) {
std::filesystem::create_directories(dir_path);
std::cout << "Created directory: " << dir_path << std::endl;
}
// Create an output string stream
ostringstream streamObj;
//Add double to stream
streamObj.precision(15);
streamObj << time;

std::string file_name="SurfaceSpecies_" + streamObj.str() + ".dat";

// Combine the directory path and file name
std::filesystem::path file_path = dir_path / file_name;
std::ofstream file(file_name);

if (!file.is_open()) {
std::cerr << "Error: Could not open file " << file_path << std::endl;
return;
}
file << "Time (s): " << time << endl;
file.precision(10);

for (int i = 0; i < m_lattice->getY(); i++){
for (int j = 0; j < m_lattice->getX(); j++)
file << m_lattice->getSite( i*m_lattice->getX() + j )->getLabel() << " " ;

file << std::endl;
}
file.close();
}
22 changes: 21 additions & 1 deletion src/IO/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Pointers;
class IO: public Pointers
{
public:

enum CASE{ Sensitive, Insensitive };

IO();
Expand Down Expand Up @@ -104,6 +105,9 @@ class IO: public Pointers
/// Reads the input file " .kmc".
void readInputFile();

/// not implemented yet
void readSurfaceSpeciesFile();

/// Converts a string to double.
double toDouble( string );

Expand Down Expand Up @@ -188,9 +192,19 @@ class IO: public Pointers
static inline string trim(std::string &s) {
rtrim(s);
ltrim(s);
return s;
return s;
}

// read a height file at a given location
vector<vector<int>>readHeightFile(string);

// read a species file at a given location
vector<vector<string>>readSpeciesFile(string);

void writeLatticeHeightsInFolder(double , const std::string&);

void writeLatticeSpeciesInFolder(double , const std::string&);

protected:
/// The type of lattice
string m_sLatticeType;
Expand Down Expand Up @@ -252,6 +266,12 @@ class IO: public Pointers

/// The keyword for reporting additionl properties (currently only supports coverages)
string m_sReport;

/// The keyword for storing the name of heights file.
string m_sHeights;

///The keyword for number of runs
string m_sRuns;

// trim from start (in place)
static inline void ltrim(std::string &s) {
Expand Down
Loading