Skip to content

Commit

Permalink
Bugfixes: llg file output and SIB spin torque.
Browse files Browse the repository at this point in the history
- fixed wrong sign in SIB optimizer which produced incorrect spin currents.
- improved SIB optimizer to run faster when spin torque and/or temperature are zero
- the LLG file output now correctly writes the initial configuration only once into the archive, first image file and initial image file
  • Loading branch information
GPMueller committed Mar 27, 2017
1 parent 543133d commit 1f57737
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
Empty file modified clean_output.sh
100644 → 100755
Empty file.
78 changes: 47 additions & 31 deletions core/src/engine/Method_LLG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,54 +134,70 @@ namespace Engine
// File save
if (this->parameters->save_output_any)
{
auto writeoutput = [this, starttime, iteration](std::string suffix, bool override_single)
// Convert indices to formatted strings
auto s_img = IO::int_to_formatted_string(this->idx_image, 2);
auto s_iter = IO::int_to_formatted_string(iteration, (int)log10(this->parameters->n_iterations));

std::string preSpinsFile = this->parameters->output_folder + "/" + starttime + "_" + "Spins_" + s_img;
std::string preEnergyFile = this->parameters->output_folder + "/" + starttime + "_" + "Energy_" + s_img;

// Function to write or append image and energy files
auto writeoutput = [this, preSpinsFile, preEnergyFile, iteration](std::string suffix, bool append)
{
// Convert indices to formatted strings
auto s_img = IO::int_to_formatted_string(this->idx_image, 2);
auto s_iter = IO::int_to_formatted_string(iteration, 6);

if (this->systems[0]->llg_parameters->save_output_archive)
// File names
std::string spinsFile = preSpinsFile + suffix + ".txt";
std::string energyFile = preEnergyFile + suffix + ".txt";

// Spin Configuration
if (append)
{
// Append Spin configuration to Spin_Archieve_File
auto spinsFile = this->parameters->output_folder + "/" + starttime + "_" + "Spins_" + s_img + suffix + ".txt";
Utility::IO::Append_Spin_Configuration(this->systems[0], iteration, spinsFile);
}

if (this->systems[0]->llg_parameters->save_output_archive && this->parameters->save_output_energy)
else
{
// Check if Energy File exists and write Header if it doesn't
auto energyFile = this->parameters->output_folder + "/" + starttime + "_Energy_" + s_img + suffix + ".txt";
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);
Utility::IO::Append_Spin_Configuration(this->systems[0], iteration, spinsFile);
}

if (this->systems[0]->llg_parameters->save_output_single || override_single)
// Energy
if (this->parameters->save_output_energy)
{
// Save Spin configuration to new "spins" File
auto spinsIterFile = this->parameters->output_folder + "/" + starttime + "_" + "Spins_" + s_img + "_" + s_iter + ".txt";
Utility::IO::Append_Spin_Configuration(this->systems[0], iteration, spinsIterFile);
if (append)
{
// Check if Energy File exists and write Header if it doesn't
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);
}
else
{
Utility::IO::Write_Energy_Header(*this->systems[0], energyFile);
Utility::IO::Append_Energy(*this->systems[0], iteration, energyFile);
}
}
};

std::string suffix = "";

// Initial image before simulation
if (initial && this->parameters->save_output_initial)
{
auto s_fix = "_" + IO::int_to_formatted_string(iteration, (int)log10(this->parameters->n_iterations)) + "_initial";
suffix = s_fix;
writeoutput(suffix, true);
writeoutput("_" + s_iter + "_initial", false);
}
// Final image after simulation
else if (final && this->parameters->save_output_final)
{
auto s_fix = "_" + IO::int_to_formatted_string(iteration, (int)log10(this->parameters->n_iterations)) + "_final";
suffix = s_fix;
writeoutput(suffix, true);
writeoutput("_" + s_iter + "_final", false);
}

suffix = "_archive";
writeoutput(suffix, false);
// Single image file output
if (this->systems[0]->llg_parameters->save_output_single)
{
writeoutput("_" + s_iter, false);
}

// Archive file (appending)
if (this->systems[0]->llg_parameters->save_output_archive)
{
writeoutput("_archive", true);
}

// Save Log
Log.Append_to_File();
Expand Down
28 changes: 20 additions & 8 deletions core/src/engine/Optimizer_SIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Engine
Optimizer_SIB::Optimizer_SIB(std::shared_ptr<Engine::Method> method) :
Optimizer(method)
{
this->xi = vectorfield(this->nos);
this->xi = vectorfield(this->nos, {0,0,0});
this->virtualforce = std::vector<vectorfield>(this->noi, vectorfield(this->nos)); // [noi][nos]

this->spins_temp = std::vector<std::shared_ptr<vectorfield>>(this->noi);
Expand All @@ -24,9 +24,12 @@ namespace Engine
for (int i = 0; i < this->noi; ++i)
{
s = method->systems[i];
this->epsilon = std::sqrt(2.0*s->llg_parameters->damping / (1.0 + std::pow(s->llg_parameters->damping, 2))*s->llg_parameters->temperature*Constants::k_B);
// Precalculate RNs --> move this up into Iterate and add array dimension n for no of iterations?
Vectormath::get_random_vectorfield(*s, epsilon, xi);
if (s->llg_parameters->temperature > 0)
{
this->epsilon = std::sqrt(2.0*s->llg_parameters->damping / (1.0 + std::pow(s->llg_parameters->damping, 2))*s->llg_parameters->temperature*Constants::k_B);
// Precalculate RNs --> move this up into Iterate and add array dimension n for no of iterations?
Vectormath::get_random_vectorfield(*s, epsilon, xi);
}
}

// First part of the step
Expand Down Expand Up @@ -65,11 +68,20 @@ namespace Engine
Vectormath::fill (force, {0,0,0});
Vectormath::add_c_a (-0.5 * dtg, gradient, force);
Vectormath::add_c_cross(-0.5 * dtg * damping, spins, gradient, force);
Vectormath::add_c_a ( 0.5 * dtg * a_j * damping, s_c_vec, force);
Vectormath::add_c_cross(-0.5 * dtg * a_j, s_c_vec, spins, force);

Vectormath::add_c_a (-0.5 * sqrtdtg, xi, force);
Vectormath::add_c_cross(-0.5 * sqrtdtg * damping, spins, xi, force);
// STT
if (a_j > 0)
{
Vectormath::add_c_a ( 0.5 * dtg * a_j * damping, s_c_vec, force);
Vectormath::add_c_cross( 0.5 * dtg * a_j, s_c_vec, spins, force);
}

// Temperature
if (llg_params.temperature > 0)
{
Vectormath::add_c_a (-0.5 * sqrtdtg, xi, force);
Vectormath::add_c_cross(-0.5 * sqrtdtg * damping, spins, xi, force);
}
}


Expand Down

0 comments on commit 1f57737

Please sign in to comment.