The code.ill.fr has been recreated and upgraded with the latest version this weekend, If you encounter any problem please inform the Helpdesk.

Commit 8f1533c2 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

continued with moldyn loader

parent 56a55f3e
...@@ -7,15 +7,11 @@ ...@@ -7,15 +7,11 @@
* g++ -std=c++17 -I ../../tlibs2 -o moldyn moldyn.cpp * g++ -std=c++17 -I ../../tlibs2 -o moldyn moldyn.cpp
*/ */
#include <fstream> #include "moldyn.h"
#include <iostream>
#include <vector>
#include <string>
#include "libs/str.h"
using t_real = double; using t_real = double;
using t_vec = std::vector<t_real>;
int main(int argc, char** argv) int main(int argc, char** argv)
...@@ -23,134 +19,8 @@ int main(int argc, char** argv) ...@@ -23,134 +19,8 @@ int main(int argc, char** argv)
if(argc <= 1) if(argc <= 1)
return -1; return -1;
MolDyn<t_real, t_vec> mol;
std::ifstream ifstr{argv[1]}; mol.LoadFile(argv[1], 100);
if(!ifstr)
{
std::cerr << "Cannot open \"" << argv[1] << "\".";
return -1;
}
const std::string strDelim{" \t"};
std::string strSys;
std::getline(ifstr, strSys);
tl2::trim(strSys);
std::cout << "System: " << strSys << std::endl;
std::string strTmp;
std::getline(ifstr, strTmp);
int numSys = tl2::str_to_var<int>(strTmp);
std::cout << "# System: " << numSys << std::endl;
std::string strVecs1;
std::string strVecs2;
std::string strVecs3;
std::getline(ifstr, strVecs1);
std::getline(ifstr, strVecs2);
std::getline(ifstr, strVecs3);
std::vector<t_real> _vecBase1, _vecBase2, _vecBase3;
tl2::get_tokens<t_real>(strVecs1, strDelim, _vecBase1);
tl2::get_tokens<t_real>(strVecs2, strDelim, _vecBase2);
tl2::get_tokens<t_real>(strVecs3, strDelim, _vecBase3);
if(_vecBase1.size()!=3 || _vecBase2.size()!=3 || _vecBase3.size()!=3)
{
std::cerr << "Invalid base vectors." << std::endl;
return -1;
}
std::vector<t_real> vecBase1(3), vecBase2(3), vecBase3(3);
vecBase1[0] = _vecBase1[0]; vecBase2[0] = _vecBase1[1]; vecBase3[0] = _vecBase1[2];
vecBase1[1] = _vecBase2[0]; vecBase2[1] = _vecBase2[1]; vecBase3[1] = _vecBase2[2];
vecBase1[2] = _vecBase3[0]; vecBase2[2] = _vecBase3[1]; vecBase3[2] = _vecBase3[2];
std::cout << "Base vector 1: ";
for(int i=0; i<3; ++i)
std::cout << vecBase1[i] << ", ";
std::cout << std::endl;
std::cout << "Base vector 2: ";
for(int i=0; i<3; ++i)
std::cout << vecBase2[i] << ", ";
std::cout << std::endl;
std::cout << "Base vector 3: ";
for(int i=0; i<3; ++i)
std::cout << vecBase3[i] << ", ";
std::cout << std::endl;
std::vector<std::string> vecAtoms;
std::vector<int> vecAtomNums;
std::string strAtoms;
std::string strAtomNums;
std::getline(ifstr, strAtoms);
std::getline(ifstr, strAtomNums);
tl2::get_tokens<std::string>(strAtoms, strDelim, vecAtoms);
tl2::get_tokens<unsigned int>(strAtomNums, strDelim, vecAtomNums);
if(vecAtoms.size() != vecAtomNums.size())
{
std::cerr << "Atom size mismatch." << std::endl;
return -1;
}
for(std::size_t i=0; i<vecAtoms.size(); ++i)
{
std::cout << vecAtomNums[i] << " " << vecAtoms[i] << " atoms." << std::endl;
}
std::size_t iNumConfigs = 0;
while(true)
{
std::string strConfig;
std::getline(ifstr, strConfig);
tl2::trim(strConfig);
if(ifstr.eof())
break;
if(iNumConfigs % 100)
std::cout << "\rReading " << strConfig << "..." << " ";
for(std::size_t iAtomType=0; iAtomType<vecAtoms.size(); ++iAtomType)
{
for(std::size_t iAtom=0; iAtom<vecAtomNums[iAtomType]; ++iAtom)
{
std::string strCoords;
std::getline(ifstr, strCoords);
std::vector<t_real> vecCoords;
vecCoords.reserve(3);
tl2::get_tokens<t_real>(strCoords, strDelim, vecCoords);
if(vecCoords.size() != 3)
{
std::cerr << "Invalid coordinate." << std::endl;
return -1;
}
}
}
++iNumConfigs;
}
std::cout << "\rRead " << iNumConfigs << " configurations." << " " << std::endl;
return 0; return 0;
} }
/**
* loads atom dynamics file
* @author Tobias Weber <tweber@ill.fr>
* @date Dec-2019
* @license GPLv3, see 'LICENSE' file
*/
#ifndef __MOLDYN_H__
#define __MOLDYN_H__
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include "libs/str.h"
template<class t_real, class t_vec>
class MolFrame
{
public:
MolFrame()
{
m_config.reserve(128);
}
void AddAtomConfig(std::vector<t_vec>&& config)
{
m_config.emplace_back(std::move(config));
}
void AddAtomConfig(const std::vector<t_vec>& config)
{
m_config.push_back(config);
}
private:
// atoms -> coordinates
std::vector<std::vector<t_vec>> m_config;
};
template<class t_real, class t_vec>
class MolDyn
{
public:
MolDyn() : m_baseA(3), m_baseB(3), m_baseC(3)
{
m_frames.reserve(16384);
}
void SetBaseA(t_real x, t_real y, t_real z)
{
m_baseA[0] = x;
m_baseA[1] = y;
m_baseA[2] = z;
}
void SetBaseB(t_real x, t_real y, t_real z)
{
m_baseB[0] = x;
m_baseB[1] = y;
m_baseB[2] = z;
}
void SetBaseC(t_real x, t_real y, t_real z)
{
m_baseC[0] = x;
m_baseC[1] = y;
m_baseC[2] = z;
}
void AddAtomType(const std::string& name, unsigned int number)
{
m_vecAtoms.push_back(name);
m_vecAtomNums.push_back(number);
}
void AddFrame(MolFrame<t_real, t_vec>&& frame)
{
m_frames.emplace_back(std::move(frame));
}
void AddFrame(const MolFrame<t_real, t_vec>& frame)
{
m_frames.push_back(frame);
}
bool LoadFile(const std::string& filename, unsigned int frameskip = 0)
{
const std::string strDelim{" \t"};
std::ifstream ifstr{filename};
if(!ifstr)
{
std::cerr << "Cannot open \"" << filename << "\".";
return 0;
}
std::string strSys;
std::getline(ifstr, strSys);
tl2::trim(strSys);
std::cout << "System: " << strSys << std::endl;
std::string strScale;
std::getline(ifstr, strScale);
t_real scale = tl2::str_to_var<t_real>(strScale);
std::cout << "scale: " << scale << std::endl;
std::string strVecs1;
std::string strVecs2;
std::string strVecs3;
std::getline(ifstr, strVecs1);
std::getline(ifstr, strVecs2);
std::getline(ifstr, strVecs3);
std::vector<t_real> _vecBase1, _vecBase2, _vecBase3;
tl2::get_tokens<t_real>(strVecs1, strDelim, _vecBase1);
tl2::get_tokens<t_real>(strVecs2, strDelim, _vecBase2);
tl2::get_tokens<t_real>(strVecs3, strDelim, _vecBase3);
if(_vecBase1.size()!=3 || _vecBase2.size()!=3 || _vecBase3.size()!=3)
{
std::cerr << "Invalid base vectors." << std::endl;
return 0;
}
SetBaseA(_vecBase1[0]*scale, _vecBase2[0]*scale, _vecBase3[0]*scale);
SetBaseB(_vecBase1[1]*scale, _vecBase2[1]*scale, _vecBase3[1]*scale);
SetBaseC(_vecBase1[2]*scale, _vecBase2[2]*scale, _vecBase3[2]*scale);
std::string strAtoms;
std::string strAtomNums;
std::getline(ifstr, strAtoms);
std::getline(ifstr, strAtomNums);
tl2::get_tokens<std::string>(strAtoms, strDelim, m_vecAtoms);
tl2::get_tokens<unsigned int>(strAtomNums, strDelim, m_vecAtomNums);
if(m_vecAtoms.size() != m_vecAtomNums.size())
{
std::cerr << "Atom size mismatch." << std::endl;
return 0;
}
for(std::size_t i=0; i<m_vecAtoms.size(); ++i)
{
std::cout << m_vecAtomNums[i] << " " << m_vecAtoms[i] << " atoms." << std::endl;
}
std::size_t iNumConfigs = 0;
while(true)
{
std::string strConfig;
std::getline(ifstr, strConfig);
tl2::trim(strConfig);
if(ifstr.eof())
break;
if(frameskip || iNumConfigs % 100)
{
std::cout << "\rReading " << strConfig << "..." << " ";
std::cout.flush();
}
MolFrame<t_real, t_vec> frame;
for(std::size_t iAtomType=0; iAtomType<m_vecAtoms.size(); ++iAtomType)
{
std::vector<t_vec> atomconf;
for(std::size_t iAtom=0; iAtom<m_vecAtomNums[iAtomType]; ++iAtom)
{
std::string strCoords;
std::getline(ifstr, strCoords);
t_vec vecCoords;
vecCoords.reserve(3);
tl2::get_tokens<t_real>(strCoords, strDelim, vecCoords);
if(vecCoords.size() != 3)
{
std::cerr << "Invalid coordinate." << std::endl;
return 0;
}
atomconf.emplace_back(std::move(vecCoords));
}
frame.AddAtomConfig(std::move(atomconf));
}
AddFrame(std::move(frame));
++iNumConfigs;
// skip frames
for(unsigned int skipped=0; skipped<frameskip; ++skipped)
{
std::string strTmp;
std::getline(ifstr, strTmp);
//std::cout << "Skipping " << strTmp << "..." << " ";
for(std::size_t iAtomType=0; iAtomType<m_vecAtoms.size(); ++iAtomType)
{
for(std::size_t iAtom=0; iAtom<m_vecAtomNums[iAtomType]; ++iAtom)
std::getline(ifstr, strTmp);
}
}
}
std::cout << "\rRead " << iNumConfigs << " configurations." << " " << std::endl;
return 1;
}
private:
t_vec m_baseA;
t_vec m_baseB;
t_vec m_baseC;
std::vector<std::string> m_vecAtoms;
std::vector<unsigned int> m_vecAtomNums;
std::vector<MolFrame<t_real, t_vec>> m_frames;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment