Commit 86613bd2 authored by Tobias WEBER's avatar Tobias WEBER

saving of moldyn data

parent 4d06a20b
......@@ -32,25 +32,18 @@ class MolFrame
void AddAtomConfig(std::vector<t_vec>&& config)
{
m_config.emplace_back(std::move(config));
}
{ m_config.emplace_back(std::move(config)); }
void AddAtomConfig(const std::vector<t_vec>& config)
{
m_config.push_back(config);
}
{ m_config.push_back(config); }
std::size_t GetNumAtoms() const
{
return m_config.size();
}
{ return m_config.size(); }
const std::vector<t_vec>& GetCoords(std::size_t atomidx) const
{
return m_config[atomidx];
}
{ return m_config[atomidx]; }
private:
// atoms -> coordinates
......@@ -125,6 +118,21 @@ class MolDyn
void Clear()
{
m_vecAtoms.clear();
m_vecAtomNums.clear();
m_frames.clear();
m_sigLoadProgress.disconnect_all_slots();
m_sigSaveProgress.disconnect_all_slots();
}
/**
* loading of files
*/
bool LoadFile(const std::string& filename, unsigned int frameskip = 0)
{
const std::string strDelim{" \t"};
......@@ -132,7 +140,7 @@ class MolDyn
std::ifstream ifstr{filename};
if(!ifstr)
{
std::cerr << "Cannot open \"" << filename << "\".";
std::cerr << "Cannot open \"" << filename << "\" for loading.";
return 0;
}
......@@ -140,10 +148,9 @@ class MolDyn
std::size_t filesize = tl2::get_file_size(ifstr);
std::cout << "File size: " << filesize / 1024 / 1024 << " MB." << std::endl;
std::string strSys;
std::getline(ifstr, strSys);
tl2::trim(strSys);
std::cout << "System: " << strSys << std::endl;
std::getline(ifstr, m_strSys);
tl2::trim(m_strSys);
std::cout << "System: " << m_strSys << std::endl;
......@@ -270,7 +277,7 @@ class MolDyn
std::size_t filepos = tl2::get_file_pos(ifstr);
percentage = t_real{filepos*100} / t_real{filesize};
if(!*m_sigLoadProgress(percentage))
if(m_sigLoadProgress.num_slots() && !*m_sigLoadProgress(percentage))
{
std::cerr << "\nLoading cancelled." << std::endl;
return 0;
......@@ -282,22 +289,104 @@ class MolDyn
}
template<class subscriber> void SubscribeToLoadProgress(const subscriber& subs)
/**
* saving of files
*/
bool SaveFile(const std::string& filename)
{
m_sigLoadProgress.connect(subs);
std::cout << m_sigSaveProgress.num_slots() << std::endl;
std::ofstream ofstr{filename};
if(!ofstr)
{
std::cerr << "Cannot open \"" << filename << "\" for saving.";
return 0;
}
ofstr.precision(8);
if(m_baseA.size() != 3)
return 0;
ofstr << m_strSys << "\n" << "1" << std::endl;
for(int i=0; i<m_baseA.size(); ++i)
ofstr << m_baseA[i] << " " << m_baseB[i] << " " << m_baseC[i] << std::endl;
for(const std::string& strAtom : m_vecAtoms)
ofstr << strAtom << " ";
ofstr << std::endl;
for(unsigned int numAtom : m_vecAtomNums)
ofstr << numAtom << " ";
ofstr << std::endl;
// iterate frames
t_real percentage = 0;
for(std::size_t frame=0; frame<m_frames.size(); ++frame)
{
ofstr << "Config " << (frame+1) << "\n";
const MolFrame<t_real, t_vec>& config = m_frames[frame];
// iterate atom types
for(std::size_t atomidx=0; atomidx<config.GetNumAtoms(); ++atomidx)
{
const auto& coords = config.GetCoords(atomidx);
// iterate coordinates
for(const auto& vec : coords)
{
if(vec.size() != 3)
return 0;
ofstr << vec[0]+0.5 << " " << vec[1]+0.5 << " " << vec[2]+0.5 << "\n";
}
}
percentage = t_real{(frame+1)*100}/t_real{m_frames.size()};
if(m_sigSaveProgress.num_slots() && !*m_sigSaveProgress(percentage))
{
std::cerr << "\nSaving cancelled." << std::endl;
return 0;
}
if(frame % 100)
{
std::cout << "\rSaving configuration " << (frame+1) << " of " << m_frames.size() << ". "
<< unsigned{percentage} << " %. ";
std::cout.flush();
}
}
std::cout << "\rSaved " << m_frames.size() << " configurations. " << " " << std::endl;
ofstr.flush();
return 1;
}
void Clear()
template<class subscriber> void SubscribeToLoadProgress(const subscriber& subs)
{ m_sigLoadProgress.connect(subs); }
template<class subscriber> void SubscribeToSaveProgress(const subscriber& subs)
{ m_sigSaveProgress.connect(subs); }
template<class subscriber> void UnsubscribeFromLoadProgress(const subscriber* subs=nullptr)
{
m_vecAtoms.clear();
m_vecAtomNums.clear();
m_frames.clear();
m_sigLoadProgress.disconnect_all_slots();
// TODO
//if(!subs)
m_sigLoadProgress.disconnect_all_slots();
}
template<class subscriber> void UnsubscribeFromSaveProgress(const subscriber* subs=nullptr)
{
// TODO
//if(!subs)
m_sigSaveProgress.disconnect_all_slots();
}
private:
std::string m_strSys;
t_vec m_baseA;
t_vec m_baseB;
t_vec m_baseC;
......@@ -308,6 +397,7 @@ class MolDyn
std::vector<MolFrame<t_real, t_vec>> m_frames;
boost::signals2::signal<bool (t_real)> m_sigLoadProgress;
boost::signals2::signal<bool (t_real)> m_sigSaveProgress;
};
......
......@@ -76,6 +76,7 @@ class MolDynFileDlg : public QFileDialog
private:
QSpinBox *m_spinFrameSkip = nullptr;
};
// ----------------------------------------------------------------------------
......@@ -102,26 +103,33 @@ MolDynDlg::MolDynDlg(QWidget* pParent) : QMainWindow{pParent},
m_menu = new QMenuBar(this);
m_menu->setNativeMenuBar(m_sett ? m_sett->value("native_gui", false).toBool() : false);
// File
auto menuFile = new QMenu("File", m_menu);
auto acNew = new QAction("New", menuFile);
auto acLoad = new QAction("Load...", menuFile);
//auto acSave = new QAction("Save...", menuFile);
auto acSaveAs = new QAction("Save As...", menuFile);
auto acExit = new QAction("Exit", menuFile);
menuFile->addAction(acNew);
menuFile->addSeparator();
menuFile->addAction(acLoad);
//menuFile->addAction(acSave);
menuFile->addAction(acSaveAs);
menuFile->addSeparator();
menuFile->addAction(acExit);
connect(acNew, &QAction::triggered, this, &MolDynDlg::New);
connect(acLoad, &QAction::triggered, this, &MolDynDlg::Load);
//connect(acSave, &QAction::triggered, this, &MolDynDlg::Save);
connect(acSaveAs, &QAction::triggered, this, &MolDynDlg::SaveAs);
connect(acExit, &QAction::triggered, this, &QDialog::close);
// Edit
auto menuEdit = new QMenu("Edit", m_menu);
m_menu->addMenu(menuFile);
m_menu->addMenu(menuEdit);
this->setMenuBar(m_menu);
}
......@@ -274,23 +282,26 @@ void MolDynDlg::Load()
return;
QString filename = files[0];
if(filename=="" || !QFile::exists(filename))
if(filename == "" || !QFile::exists(filename))
return;
m_sett->setValue("dir", QFileInfo(filename).path());
New();
std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
"Loading \"" + QFileInfo(filename).fileName() + "\"...", "Cancel", 0, 1000, this);
bool bCancelled = 0;
m_mol.SubscribeToLoadProgress([dlgProgress, &bCancelled](t_real percentage) -> bool
auto progressHandler = [dlgProgress, &bCancelled](t_real percentage) -> bool
{
dlgProgress->setValue(int(percentage*10));
bCancelled = dlgProgress->wasCanceled();
return !bCancelled;
});
};
m_mol.SubscribeToLoadProgress(progressHandler);
dlgProgress->setWindowModality(Qt::WindowModal);
if(!m_mol.LoadFile(filename.toStdString(), filedlg->GetFrameSkip()))
{
// only show error if not explicitely cancelled
......@@ -299,6 +310,8 @@ void MolDynDlg::Load()
return;
}
m_mol.UnsubscribeFromLoadProgress(&progressHandler);
m_slider->setMaximum(m_mol.GetFrameCount() - 1);
......@@ -366,8 +379,42 @@ void MolDynDlg::Load()
}
void MolDynDlg::Save()
void MolDynDlg::SaveAs()
{
try
{
QString dirLast = m_sett->value("dir", "").toString();
QString filename = QFileDialog::getSaveFileName(this, "Save File", dirLast, "Molecular Dynamics File (*)");
if(filename == "")
return;
m_sett->setValue("dir", QFileInfo(filename).path());
std::shared_ptr<QProgressDialog> dlgProgress = std::make_shared<QProgressDialog>(
"Saving \"" + QFileInfo(filename).fileName() + "\"...", "Cancel", 0, 1000, this);
bool bCancelled = 0;
auto progressHandler = [dlgProgress, &bCancelled](t_real percentage) -> bool
{
dlgProgress->setValue(int(percentage*10));
bCancelled = dlgProgress->wasCanceled();
return !bCancelled;
};
m_mol.SubscribeToSaveProgress(progressHandler);
dlgProgress->setWindowModality(Qt::WindowModal);
if(!m_mol.SaveFile(filename.toStdString()))
{
QMessageBox::critical(this, "Molecular Dynamics", "Error saving file.");
}
m_mol.UnsubscribeFromSaveProgress(&progressHandler);
}
catch(const std::exception& ex)
{
QMessageBox::critical(this, "Molecular Dynamics", ex.what());
}
}
// ----------------------------------------------------------------------------
......
......@@ -43,7 +43,7 @@ protected:
void New();
void Load();
void Save();
void SaveAs();
void PlotMouseDown(bool left, bool mid, bool right);
void PlotMouseUp(bool left, bool mid, bool right);
......
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