Skip to content

Commit

Permalink
Bugfix: irregular memory error (double free).
Browse files Browse the repository at this point in the history
- fixed bug in Chain API and System API where images were not locked before updating Energy arrays
- fixed bug in ControlWidget which would continuously update SettingsWidget
- improved names of energy IO methods
- added save interpolated energies to file menu
  • Loading branch information
GPMueller committed Apr 6, 2017
1 parent ddac1ad commit 04da947
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 118 deletions.
16 changes: 11 additions & 5 deletions core/include/Spirit/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ DLLEXPORT void IO_Chain_Write(State * state, const char * file, int idx_image=-1
DLLEXPORT void IO_Collection_Read(State * state, const char * file, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void IO_Collection_Write(State * state, const char * file, int idx_image=-1, int idx_chain=-1);

// Data
DLLEXPORT void IO_Energy_Spins_Save(State * state, const char * file, int idx_image=-1, int idx_chain=-1);
DLLEXPORT void IO_Energies_Save(State * state, const char * file, int idx_chain = -1);
DLLEXPORT void IO_Energies_Spins_Save(State * state, const char * file, int idx_chain = -1);
DLLEXPORT void IO_Energies_Interpolated_Save(State * state, const char * file, int idx_chain = -1);

// Save the spin-resolved energy contributions of a spin system
DLLEXPORT void IO_Write_System_Energy_per_Spin(State * state, const char * file, int idx_chain = -1);
// Save the Energy contributions of a spin system
DLLEXPORT void IO_Write_System_Energy(State * state, const char * file, int idx_image=-1, int idx_chain=-1);

// Save the Energy contributions of a chain of spin systems
DLLEXPORT void IO_Write_Chain_Energies(State * state, const char * file, int idx_chain = -1);
// Save the interpolated energies of a chain of spin systems
DLLEXPORT void IO_Write_Chain_Energies_Interpolated(State * state, const char * file, int idx_chain = -1);


#include "DLL_Undefine_Export.h"
#endif
24 changes: 14 additions & 10 deletions core/include/utility/IO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,22 @@ namespace Utility
void Save_SpinChain_Configuration(std::shared_ptr<Data::Spin_System_Chain> & c, const std::string fileName);

// =========================== Saving Energies ===========================
void Write_Energy_Header(Data::Spin_System & s, const std::string fileName, std::vector<std::string> firstcolumns={"iteration", "E_tot"}, bool contributions=true, bool normalize_nos=true);
// Appends the current Energy of the current image with energy contributions, without header
void Append_Energy(Data::Spin_System &s, const int iteration, const std::string fileName, bool normalize_nos=true);
// Save energy contributions per spin
void Save_Energy_Spins(Data::Spin_System & s, const std::string fileName, bool normalize_nos=true);
void Write_Energy_Header(const Data::Spin_System & s, const std::string fileName, std::vector<std::string> firstcolumns={"iteration", "E_tot"}, bool contributions=true, bool normalize_nos=true);
// Appends the Energy of a spin system with energy contributions (without header)
void Append_System_Energy(const Data::Spin_System &s, const int iteration, const std::string fileName, bool normalize_nos=true);
// Save energy contributions of a spin system
void Write_System_Energy(const Data::Spin_System & system, const std::string fileName, bool normalize_by_nos=true);
// Save energy contributions of a spin system per spin
void Write_System_Energy_per_Spin(const Data::Spin_System & s, const std::string fileName, bool normalize_nos=true);
// Saves the forces on an image chain
void Write_System_Force(const Data::Spin_System & s, const std::string fileName);

// Saves Energies of all images with header and contributions
void Save_Energies(Data::Spin_System_Chain & c, const int iteration, const std::string fileName, bool normalize_nos=true);
void Write_Chain_Energies(const Data::Spin_System_Chain & c, const int iteration, const std::string fileName, bool normalize_nos=true);
// Saves the Energies interpolated by the GNEB method
void Save_Energies_Interpolated(Data::Spin_System_Chain & c, const std::string fileName, bool normalize_nos=true);
// Saves the energy contributions of every spin of an image
void Save_Energies_Spins(Data::Spin_System_Chain & c, const std::string fileName, bool normalize_nos=true);
void Save_Forces(Data::Spin_System_Chain & c, const std::string fileName);
void Write_Chain_Energies_Interpolated(const Data::Spin_System_Chain & c, const std::string fileName, bool normalize_nos=true);
// Saves the forces on an image chain
void Write_Chain_Forces(const Data::Spin_System_Chain & c, const std::string fileName);


// ========================= Saving Helpers =========================
Expand Down
3 changes: 3 additions & 0 deletions core/src/Spirit/Chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,11 @@ void Chain_Update_Data(State * state, int idx_chain_i)
{
//Engine::Energy::Update(*chain->images[i]);
//chain->images[i]->E = chain->images[i]->hamiltonian_isotropichain->Energy(chain->images[i]->spins);

image->Lock();
chain->images[i]->UpdateEnergy();
if (i > 0) chain->Rx[i] = chain->Rx[i-1] + Engine::Manifoldmath::dist_geodesic(*chain->images[i-1]->spins, *chain->images[i]->spins);
image->Unlock();
}
}

Expand Down
23 changes: 13 additions & 10 deletions core/src/Spirit/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,46 +130,49 @@ void IO_Collection_Write(State * state, const char * file, int idx_image, int id
/*--------------------------------------- Data --------------------------------------------------------- */
/*------------------------------------------------------------------------------------------------------ */

void IO_Energy_Spins_Save(State * state, const char * file, int idx_image, int idx_chain)
//IO_Energies_Spins_Save
void IO_Write_System_Energy_per_Spin(State * state, const char * file, int idx_chain)
{
int idx_image = -1;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
from_indices(state, idx_image, idx_chain, image, chain);

// Write the data
Utility::IO::Save_Energy_Spins(*image, std::string(file));
Utility::IO::Write_System_Energy_per_Spin(*image, std::string(file));
}

void IO_Energies_Save(State * state, const char * file, int idx_chain)
//IO_Energy_Spins_Save
void IO_Write_System_Energy(State * state, const char * file, int idx_image, int idx_chain)
{
int idx_image = -1;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
from_indices(state, idx_image, idx_chain, image, chain);

// Write the data
Utility::IO::Save_Energies(*chain, idx_chain, std::string(file));
Utility::IO::Write_System_Energy(*image, std::string(file));
}


void IO_Energies_Spins_Save(State * state, const char * file, int idx_chain)
//IO_Energies_Save
void IO_Write_Chain_Energies(State * state, const char * file, int idx_chain)
{
int idx_image = -1;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
from_indices(state, idx_image, idx_chain, image, chain);

// Write the data
Utility::IO::Save_Energies_Spins(*state->active_chain, std::string(file));
Utility::IO::Write_Chain_Energies(*chain, idx_chain, std::string(file));
}

void IO_Energies_Interpolated_Save(State * state, const char * file, int idx_chain)
//IO_Energies_Interpolated_Save
void IO_Write_Chain_Energies_Interpolated(State * state, const char * file, int idx_chain)
{
int idx_image = -1;
std::shared_ptr<Data::Spin_System> image;
std::shared_ptr<Data::Spin_System_Chain> chain;
from_indices(state, idx_image, idx_chain, image, chain);

// Write the data
Utility::IO::Save_Energies_Interpolated(*state->active_chain, std::string(file));
Utility::IO::Write_Chain_Energies_Interpolated(*chain, std::string(file));
}
2 changes: 2 additions & 0 deletions core/src/Spirit/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ void System_Update_Data(State * state, int idx_image, int idx_chain)
std::shared_ptr<Data::Spin_System_Chain> chain;
from_indices(state, idx_image, idx_chain, image, chain);

image->Lock();
image->UpdateEnergy();
image->Unlock();
}
4 changes: 2 additions & 2 deletions core/src/engine/Method_GNEB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ namespace Engine
{
// Save current Energies with reaction coordinates
auto energiesFile = this->chain->gneb_parameters->output_folder + "/" + starttime + "_E_Images_" + s_iter + suffix + ".txt";
Utility::IO::Save_Energies(*this->chain, iteration, energiesFile);
Utility::IO::Write_Chain_Energies(*this->chain, iteration, energiesFile);

// Save interpolated Energies
auto energiesInterpFile = this->chain->gneb_parameters->output_folder + "/" + starttime + "_E_interp_Images_" + s_iter + suffix + ".txt";
Utility::IO::Save_Energies_Interpolated(*this->chain, energiesInterpFile);
Utility::IO::Write_Chain_Energies_Interpolated(*this->chain, energiesInterpFile);
}

// Save Log
Expand Down
6 changes: 3 additions & 3 deletions core/src/engine/Method_LLG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ namespace Engine
std::ifstream f(energyFile);
if (!f.good()) Utility::IO::Write_Energy_Header(*this->systems[0], energyFile);
// Append Energy to File
Utility::IO::Append_Energy(*this->systems[0], iteration, energyFile, normalize);
Utility::IO::Append_System_Energy(*this->systems[0], iteration, energyFile, normalize);
}
else
{
Utility::IO::Write_Energy_Header(*this->systems[0], energyFile);
Utility::IO::Append_Energy(*this->systems[0], iteration, energyFile, normalize);
Utility::IO::Append_System_Energy(*this->systems[0], iteration, energyFile, normalize);
if (this->systems[0]->llg_parameters->output_energy_spin_resolved)
{
Utility::IO::Save_Energy_Spins(*this->systems[0], energyFilePerSpin, normalize);
Utility::IO::Write_System_Energy_per_Spin(*this->systems[0], energyFilePerSpin, normalize);
}
}
};
Expand Down
105 changes: 40 additions & 65 deletions core/src/utility/IO_Filedump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace Utility
}
// ------------------------------------------------------------

void Write_Energy_Header(Data::Spin_System & s, const std::string fileName, std::vector<std::string> firstcolumns, bool contributions, bool normalize_by_nos)
void Write_Energy_Header(const Data::Spin_System & s, const std::string fileName, std::vector<std::string> firstcolumns, bool contributions, bool normalize_by_nos)
{
bool readability_toggle = true;

Expand Down Expand Up @@ -97,14 +97,14 @@ namespace Utility
String_to_File(header, fileName);
}

void Append_Energy(Data::Spin_System & s, const int iteration, const std::string fileName, bool normalize_by_nos)
void Append_System_Energy(const Data::Spin_System & s, const int iteration, const std::string fileName, bool normalize_by_nos)
{
bool readability_toggle = true;
scalar nd = 1.0; // nos divide
if (normalize_by_nos) nd = 1.0 / s.nos;
else nd = 1;

s.UpdateEnergy();
// s.UpdateEnergy();

std::string line = center(iteration, 0, 20) + "||" + center(s.E * nd, 10, 20) + "||";
bool first = true;
Expand All @@ -123,14 +123,40 @@ namespace Utility
Append_String_to_File(line, fileName);
}

void Save_Energy_Spins(Data::Spin_System & s, const std::string fileName, bool normalize_by_nos)
void Write_System_Energy(const Data::Spin_System & system, const std::string fileName, bool normalize_by_nos)
{
bool readability_toggle = true;
scalar nd = 1.0; // nos divide
if (normalize_by_nos) nd = 1.0 / system.nos;
else nd = 1;

Write_Energy_Header(system, fileName, {"E_tot"});

std::string line = center(system.E * nd, 10, 20) + "||";
bool first = true;
for (auto pair : system.E_array)
{
if (first) first = false;
else
{
line += "|";;
}
line += center(pair.second * nd, 10, 20);
}
line += "\n";

if (!readability_toggle) std::replace( line.begin(), line.end(), '|', ' ');
Append_String_to_File(line, fileName);
}

void Write_System_Energy_per_Spin(const Data::Spin_System & s, const std::string fileName, bool normalize_by_nos)
{
bool readability_toggle = true;
scalar nd = 1.0; // nos divide
if (normalize_by_nos) nd = 1.0 / s.nos;
else nd = 1;

s.UpdateEnergy();
// s.UpdateEnergy();

Write_Energy_Header(s, fileName, {"ispin", "E_tot"});

Expand Down Expand Up @@ -160,7 +186,11 @@ namespace Utility
Append_String_to_File(data, fileName);
}

void Save_Energies(Data::Spin_System_Chain & c, const int iteration, const std::string fileName, bool normalize_by_nos)
void Write_System_Force(const Data::Spin_System & s, const std::string fileName)
{
}

void Write_Chain_Energies(const Data::Spin_System_Chain & c, const int iteration, const std::string fileName, bool normalize_by_nos)
{
int isystem;
bool readability_toggle = true;
Expand All @@ -173,7 +203,7 @@ namespace Utility
for (isystem = 0; isystem < (int)c.noi; ++isystem)
{
auto& system = *c.images[isystem];
std::string line = center(isystem, 0, 20) + "||" + center(c.Rx[isystem], 0, 20) + "||" + center(system.E * nd, 10, 20) + "||";
std::string line = center(isystem, 0, 20) + "||" + center(c.Rx[isystem], 10, 20) + "||" + center(system.E * nd, 10, 20) + "||";
bool first = true;
for (auto pair : system.E_array)
{
Expand All @@ -191,8 +221,7 @@ namespace Utility
}
}


void Save_Energies_Interpolated(Data::Spin_System_Chain & c, const std::string fileName, bool normalize_by_nos)
void Write_Chain_Energies_Interpolated(const Data::Spin_System_Chain & c, const std::string fileName, bool normalize_by_nos)
{
int isystem, iinterp, idx;
bool readability_toggle = true;
Expand All @@ -209,7 +238,7 @@ namespace Utility
for (iinterp = 0; iinterp < c.gneb_parameters->n_E_interpolations+1; ++iinterp)
{
idx = isystem * (c.gneb_parameters->n_E_interpolations+1) + iinterp;
std::string line = center(isystem, 0, 20) + "||" + center(iinterp, 0, 20) + "||" + center(c.Rx_interpolated[idx], 0, 20) + "||" + center(c.E_interpolated[idx] * nd, 10, 20) + "||";
std::string line = center(isystem, 0, 20) + "||" + center(iinterp, 0, 20) + "||" + center(c.Rx_interpolated[idx], 10, 20) + "||" + center(c.E_interpolated[idx] * nd, 10, 20) + "||";

// TODO: interpolated Energy contributions
// bool first = true;
Expand All @@ -234,61 +263,7 @@ namespace Utility
}


void Save_Energies_Spins(Data::Spin_System_Chain & c, const std::string fileName, bool normalize_by_nos)
{
/////////////////
// TODO: rewrite like other save_energy functions
/////////////////

// //========================= Init local vars ================================
// int isystem, ispin, iE;
// bool readability_toggle = true;
// scalar nd = 1.0; // nos divide
// const int buffer_length = 200;
// std::string output_to_file = "";
// output_to_file.reserve(int(1E+08));
// char buffer_string_conversion[buffer_length + 2];
// snprintf(buffer_string_conversion, buffer_length, " isystem || ispin || E_tot || E_Zeeman | E_Aniso | E_Exchange | E_DMI | E_BQC | E_FourSC | E_DipoleDipole\n");
// if (!readability_toggle) { std::replace(buffer_string_conversion, buffer_string_conversion + strlen(buffer_string_conversion), '|', ' '); }
// output_to_file.append(buffer_string_conversion);
// snprintf(buffer_string_conversion, buffer_length, "---------++---------++----------------------++---------------------+---------------------+---------------------+---------------------+---------------------+----------------------+---------------------");
// if (readability_toggle) { output_to_file.append(buffer_string_conversion); }
// //------------------------ End Init ----------------------------------------

// int nos = (int)c.images[0]->nos;
// int noi = (int)c.noi;
// auto Energies_spins = std::vector<std::vector<scalar>>(nos, std::vector<scalar>(7, 0.0));
// auto E_tot_spins = std::vector<scalar>(nos, 0.0);
// for (isystem = 0; isystem < noi; ++isystem) {
// // Get Energies
// Energies_spins = c.images[isystem]->hamiltonian->Energy_Array_per_Spin(*c.images[isystem]->spins);
// for (ispin = 0; ispin < nos; ++ispin)
// {
// for (iE = 0; iE < 7; ++iE)
// {
// E_tot_spins[ispin] += Energies_spins[ispin][iE];
// }
// }
// // Normalise?
// if (normalize_by_nos) { nd = 1.0 / nos; }
// else { nd = 1; }
// // Write
// for (ispin = 0; ispin < nos; ++ispin)
// {
// snprintf(buffer_string_conversion, buffer_length, "\n %6i || %6i || %18.10f || %18.10f | %18.10f | %18.10f | %18.10f | %18.10f | %18.10f | %18.10f",
// isystem, ispin, E_tot_spins[ispin] * nd, Energies_spins[ispin][ENERGY_POS_ZEEMAN] * nd, Energies_spins[ispin][ENERGY_POS_ANISOTROPY] * nd,
// Energies_spins[ispin][ENERGY_POS_EXCHANGE] * nd, Energies_spins[ispin][ENERGY_POS_DMI] * nd,
// Energies_spins[ispin][ENERGY_POS_BQC] * nd, Energies_spins[ispin][ENERGY_POS_FSC] * nd,
// Energies_spins[ispin][ENERGY_POS_DD] * nd);
// if (!readability_toggle) { std::replace(buffer_string_conversion, buffer_string_conversion + strlen(buffer_string_conversion), '|', ' '); }
// output_to_file.append(buffer_string_conversion);
// }
// }
// Dump_to_File(output_to_file, fileName);
}


void Save_Forces(Data::Spin_System_Chain & c, const std::string fileName)
void Write_Chain_Forces(const Data::Spin_System_Chain & c, const std::string fileName)
{
/////////////////
// TODO: rewrite like save_energy functions
Expand Down
1 change: 1 addition & 0 deletions ui-qt/include/ControlWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private slots:
void save_EPressed();

private:
void updateOthers();
void readSettings();
void writeSettings();
// State
Expand Down
5 changes: 3 additions & 2 deletions ui-qt/include/MainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ private slots:
void load_Spin_Configuration();
void save_SpinChain_Configuration();
void load_SpinChain_Configuration();
void save_Energies();
void save_Energy_Spins();
void save_System_Energy_Spins();
void save_Chain_Energies();
void save_Chain_Energies_Interpolated();
void return_focus();

private:
Expand Down
Loading

0 comments on commit 04da947

Please sign in to comment.