Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick O'Brien committed Oct 9, 2018
0 parents commit d1022e3
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
.DS_Store
build
cmake-build-default/
18 changes: 18 additions & 0 deletions Algorithm/Algorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created by Patrick O'Brien on 9/19/18.
//

#ifndef SIMULATION_ALGORITHM_HPP
#define SIMULATION_ALGORITHM_HPP

#include "../Model/Model.hpp"
#include <armadillo>
using namespace arma;

#endif //SIMULATION_ALGORITHM_HPP


class Algorithm{
public:
virtual void simulate(int nitr, Model *model_ptr)=0;
};
34 changes: 34 additions & 0 deletions Algorithm/MonteCarlo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Patrick O'Brien on 9/19/18.
//

#include "MonteCarlo.hpp"
#include "../Model/Model.hpp"

void MonteCarlo::simulate(int nitr, Model *model_ptr) {
std::ofstream output;
output.open("output.json");
model_ptr->create_initial_spin_configuration();

output << R"({"type": "common_origin", "data": [)";

int accepted_counter = 0;
output << model_ptr->save_spin_configuration(accepted_counter).str();

for (int i = 0; i < nitr; i++) {
uword random_index = randi<umat>(1, 1, distr_param(0, (model_ptr->system_size) - 1))(0);
mat old = model_ptr->spin_config.row(random_index);
mat new_s = model_ptr->new_spin();
double change_eng = model_ptr->energy_change(random_index, new_s);

if (change_eng < 0.0) {
output << ", ";
model_ptr->update_spin_configuration(random_index, new_s);
accepted_counter++;
output << model_ptr->save_spin_configuration(accepted_counter).str();
}
}

output << "]}";
output.close();
}
16 changes: 16 additions & 0 deletions Algorithm/MonteCarlo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by Patrick O'Brien on 10/2/18.
//

#ifndef SIMULATION_MONTECARLO_HPP
#define SIMULATION_MONTECARLO_HPP

#include "Algorithm.hpp"
#include "../Model/Model.hpp"

#endif //SIMULATION_MONTECARLO_HPP

class MonteCarlo: public Algorithm{
public:
void simulate(int nitr, Model *model_ptr) override;
};
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)
project(Simulation)

set(CMAKE_CXX_STANDARD 11)

find_library(Simulation libarmadillo)

add_executable(Simulation main.cpp Model/Model.hpp Model/Heisenberg.cpp Algorithm/MonteCarlo.cpp Lattice/Lattice.hpp Model/Heisenberg.hpp Algorithm/MonteCarlo.hpp Algorithm/Algorithm.hpp)

target_link_libraries(Simulation armadillo)
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM ubuntu:18.10
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y g++ git cmake
COPY . /usr/local
WORKDIR /usr/local/cmake-build-debug
8 changes: 8 additions & 0 deletions Lattice/Lattice.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Created by Patrick O'Brien on 9/27/18.
//

#ifndef SIMULATION_LATTICE_HPP
#define SIMULATION_LATTICE_HPP

#endif //SIMULATION_LATTICE_HPP
109 changes: 109 additions & 0 deletions Model/Heisenberg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Created by Patrick O'Brien on 9/18/18.
//

#include "Heisenberg.hpp"

Heisenberg::Heisenberg(int system_size) {
this->system_size = system_size;
}

mat Heisenberg::new_spin() {
return normalise(randu<rowvec>(3));
}

mat Heisenberg::create_initial_spin_configuration() {
mat spin_configuration(this->system_size, 3);

for (int i = 0; i < this->system_size; i++) {
mat ns = new_spin();
for (int j = 0; j < 3; j++) {
spin_configuration(i, j) = ns(j);
}
}

this->spin_config = spin_configuration;
return spin_configuration;
}

mat Heisenberg::create_ferromagnetic_exchange_matrix() {
this->exchange_matrix = -ones(this->system_size, this->system_size);
return -ones(this->system_size, this->system_size);
}

double Heisenberg::energy() {
double eng = 0.0;

for (int i = 0; i < this->system_size; i++) {
for (int j = 0; j < this->system_size; j++) {
for (int k = 0; k < 3; k++) {
eng += this->exchange_matrix(i, j) * (this->spin_config)(i, k) * (this->spin_config)(j, k);
}
}
}

return eng;
}

double Heisenberg::energy_change(int ind, mat new_spin_vec) {
double del_eng = 0.0;

for (int i = 0; i < this->system_size; i++) {
for (int k = 0; k < 3; k++) {
del_eng += 2 * this->exchange_matrix(i, ind) * (new_spin_vec(k) - (this->spin_config)(ind, k)) *
(this->spin_config)(i, k);
}
}

for (int k = 0; k < 3; k++) {
del_eng -= 2 * this->exchange_matrix(ind, ind) * (new_spin_vec(k) - (this->spin_config)(ind, k)) *
(this->spin_config)(ind, k);
}

return del_eng;
}

imat Heisenberg::choose_random_index() {
return randi(1, 1, distr_param(1, this->system_size));
}

mat Heisenberg::old_spin() {
// return (this->spin_config)(randi(1,1, distr_param(+1, +10)), 0);
return zeros(this->system_size, 3);
}

void Heisenberg::update_spin_configuration(uword ind, mat n_spin) {
this->spin_config.row(ind) = n_spin;
}

std::stringstream Heisenberg::save_spin_configuration(int spin_config_number) {
std::stringstream output;

output << "{\"step\": " << spin_config_number << ", \"data\": [";

for (int i = 0; i < (this->system_size) - 1; i++) {
output << "{\"spin_number\": " << i << "," << "\"x\": " << (this->spin_config)(i, 0) << "," << "\"y\": "
<< (this->spin_config)(i, 1) << "," << "\"z\": " << (this->spin_config)(i, 2) << "}, ";
}

output << "{\"spin_number\": " << (this->system_size) - 1 << "," << "\"x\": " << (this->spin_config)((this->system_size) - 1, 0) << "," << "\"y\": "
<< (this->spin_config)((this->system_size) - 1, 1) << "," << "\"z\": " << (this->spin_config)((this->system_size) - 1, 2) << "}]}";

return output;
}


//mat Heisenberg::create_ferromagnetic_spin_configuration() {
// mat spin_configuration(10, 3);
//
// vec ns = new_spin();
//
// for (int i = 0; i < 10; i++) {
// for (int j = 0; j < 3; j++) {
// spin_configuration(i, j) = ns(j);
// }
// }
//
// this->spin_config = spin_configuration;
// return spin_configuration;
//}
27 changes: 27 additions & 0 deletions Model/Heisenberg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Patrick O'Brien on 10/2/18.
//

#ifndef SIMULATION_HEISENBERG_HPP
#define SIMULATION_HEISENBERG_HPP

#include "Model.hpp"

#endif //SIMULATION_HEISENBERG_HPP

class Heisenberg: public Model {
public:
mat exchange_matrix;
double energy() override;
double energy_change(int ind, mat new_spin_vec) override;
imat choose_random_index() override;
mat old_spin() override;
mat new_spin() override;
mat create_initial_spin_configuration() override;
mat create_ferromagnetic_exchange_matrix();
mat create_ferromagnetic_spin_configuration();
void update_spin_configuration(uword ind, mat n_spin) override;
std::stringstream save_spin_configuration(int spin_config_number) override;

explicit Heisenberg(int system_size);
};
22 changes: 22 additions & 0 deletions Model/Model.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by Patrick O'Brien on 9/18/18.
//

#include <armadillo>
#include <fstream>
using namespace arma;

#pragma once
class Model{
public:
mat spin_config;
int system_size;
virtual double energy() = 0;
virtual double energy_change(int ind, mat new_spin_vec) =0;
virtual imat choose_random_index() =0;
virtual mat old_spin()=0;
virtual mat new_spin() =0;
virtual mat create_initial_spin_configuration()=0;
virtual void update_spin_configuration(uword ind, mat n_spin)=0;
virtual std::stringstream save_spin_configuration(int spin_config_number)=0;
};
31 changes: 31 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <armadillo>
#include "Model/Heisenberg.hpp"
#include "Algorithm/MonteCarlo.hpp"

using namespace arma;

int main() {
std::cout << "Phys-sym v. 0.0" << std::endl;
std::cout << "Simulation software" << std::endl << std::endl;

Model *model_ptr;
Heisenberg heisenberg(10);
model_ptr = &heisenberg;

heisenberg.create_ferromagnetic_exchange_matrix();
model_ptr -> create_initial_spin_configuration();
std::cout << "The spin configuration is: " << std::endl << model_ptr -> spin_config << std::endl;
std::cout << "The energy of the system is: " << model_ptr->energy() << std::endl;

int n_itr = 10000;
Algorithm *alg;
MonteCarlo mc;
alg = &mc;
alg->simulate(n_itr, model_ptr);

std::cout << "The spin configuration is: " << std::endl << model_ptr -> spin_config << std::endl;
std::cout << "The energy of the system is: " << model_ptr->energy() << std::endl;

return 0;
}

0 comments on commit d1022e3

Please sign in to comment.