From 8446173b010b019cd4c7d38db835a9c0c6b08dc1 Mon Sep 17 00:00:00 2001 From: Patrick O'Brien Date: Mon, 5 Nov 2018 07:55:54 -0500 Subject: [PATCH] Ising code - broken state. --- Algorithm/MonteCarlo.cpp | 20 +++++++++++++++++++- Lattice/Lattice.hpp | 11 +++++++---- Lattice/Square.cpp | 11 +++++++---- Lattice/Square.hpp | 2 +- Model/Ising.cpp | 39 +++++++++++++++++++++++++++------------ Model/Model.hpp | 8 ++++++++ main.cpp | 36 +++++++++++++----------------------- 7 files changed, 82 insertions(+), 45 deletions(-) diff --git a/Algorithm/MonteCarlo.cpp b/Algorithm/MonteCarlo.cpp index 94c0369..345a5cd 100644 --- a/Algorithm/MonteCarlo.cpp +++ b/Algorithm/MonteCarlo.cpp @@ -37,4 +37,22 @@ void MonteCarlo::simulate(int nitr, Model *model_ptr) { output << output_json.dump(); output.close(); -} \ No newline at end of file +} + +// 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; \ No newline at end of file diff --git a/Lattice/Lattice.hpp b/Lattice/Lattice.hpp index d782999..4b14aa1 100644 --- a/Lattice/Lattice.hpp +++ b/Lattice/Lattice.hpp @@ -8,7 +8,8 @@ #include "Site.hpp" #pragma once -class Lattice{ + +class Lattice { public: int n_x = 4; int n_y = 4; @@ -16,10 +17,12 @@ class Lattice{ std::vector lat; - virtual void generate_lattice()=0; - void print_lattice(){for(auto &m:lat){std::cout << m.x << ", " << m.y << ", " << m.z << std::endl;}}; + virtual void generate_lattice() = 0; + + void print_lattice() { for (auto &m:lat) { std::cout << m.x << ", " << m.y << ", " << m.z << std::endl; }}; + + virtual void find_neighbors() = 0; - virtual void find_neighbor_indices(int x_, int y_) = 0; // virtual void find_neighbors() = 0; virtual int convert_to_index(int, int) = 0; }; diff --git a/Lattice/Square.cpp b/Lattice/Square.cpp index 6c5b2a0..c5af0de 100644 --- a/Lattice/Square.cpp +++ b/Lattice/Square.cpp @@ -7,7 +7,7 @@ void Square::generate_lattice() { for (int i = 0; i < 4; i++) { - for(int j = 0; j < 4; j++) { + for (int j = 0; j < 4; j++) { lat.push_back(Site(j, i, 0)); } } @@ -21,9 +21,12 @@ int Square::convert_to_index(int x_, int y_) { return n_x * y_ + x_; } -void Square::find_neighbor_indices(int x_, int y_) { +void Square::find_neighbors() { std::vector::pointer a; a = &lat.front(); - lat[0].neighbors.push_back(a + convert_to_index(x_ % n_x, (y_ + 1) % n_y)); - lat[0].neighbors.push_back(a + convert_to_index((x_ + 1) % n_x, y_ % n_y)); + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) { + lat[n_x * j + i].neighbors.push_back(a + convert_to_index(i % n_x, (j + 1) % n_y)); + lat[n_x * j + i].neighbors.push_back(a + convert_to_index((i + 1) % n_x, j % n_y)); + } } \ No newline at end of file diff --git a/Lattice/Square.hpp b/Lattice/Square.hpp index 731057c..db21e46 100644 --- a/Lattice/Square.hpp +++ b/Lattice/Square.hpp @@ -9,6 +9,6 @@ class Square : public Lattice { public: void generate_lattice() override; // void find_neighbors() override; - void find_neighbor_indices(int x_, int y_) override; + void find_neighbors() override; int convert_to_index(int x_, int y_) override; }; diff --git a/Model/Ising.cpp b/Model/Ising.cpp index 520d442..5f1471d 100644 --- a/Model/Ising.cpp +++ b/Model/Ising.cpp @@ -5,35 +5,50 @@ #include #include "Ising.hpp" #include "../Lattice/Square.hpp" +#include +using namespace arma; Ising::Ising(int system_size) { this->system_size = system_size; } -double Ising::energy(){ - return 0.0; +double Ising::energy() { + double eng = 0.0; + for(int i = 0; i < 16; i++) + { + std::cout << "Break 1" << std::endl; + Site *neigh = lattice->lat[i].neighbors[0]; + std::cout << "Break 2" << std::endl; + eng += -dot(spin_config.row(i), spin_config.row(lattice->convert_to_index((*neigh).x, (*neigh).y))); + std::cout << "Break 3" << std::endl; + neigh = lattice->lat[i].neighbors[1]; + std::cout << "Break 4" << std::endl; + eng += -dot(spin_config.row(i), spin_config.row(lattice->convert_to_index((*neigh).x, (*neigh).y))); + std::cout << "Break 5" << std::endl; + } + return eng; }; -double Ising::energy_change(int ind, mat new_spin_vec){ +double Ising::energy_change(int ind, mat new_spin_vec) { return 0.0; }; -imat Ising::choose_random_index(){ - return imat(3,3); +imat Ising::choose_random_index() { + return imat(3, 3); }; -mat Ising::old_spin(){ - return mat(1,3); +mat Ising::old_spin() { + return mat(1, 3); }; -mat Ising::new_spin(){ +mat Ising::new_spin() { mat s = zeros(1, 3); - s(0, 2) = 2*(std::rand()%2) - 1; + s(0, 2) = 2 * (std::rand() % 2) - 1; return s; }; -mat Ising::create_initial_spin_configuration(){ +mat Ising::create_initial_spin_configuration() { mat spin_configuration(this->system_size, 3); for (int i = 0; i < this->system_size; i++) { @@ -47,11 +62,11 @@ mat Ising::create_initial_spin_configuration(){ return spin_configuration; }; -void Ising::update_spin_configuration(uword ind, mat n_spin){ +void Ising::update_spin_configuration(uword ind, mat n_spin) { }; -std::stringstream Ising::save_spin_configuration(int spin_config_number){ +std::stringstream Ising::save_spin_configuration(int spin_config_number) { std::stringstream output; output << "{\"step\": " << spin_config_number << ", \"data\": ["; diff --git a/Model/Model.hpp b/Model/Model.hpp index 24a0d39..5cf2ebc 100644 --- a/Model/Model.hpp +++ b/Model/Model.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Lattice/Square.hpp" using namespace arma; @@ -13,6 +14,7 @@ class Model { public: mat spin_config; int system_size; + Square *lattice; virtual double energy() = 0; @@ -31,4 +33,10 @@ class Model { virtual void update_spin_configuration(uword ind, mat n_spin) = 0; virtual std::stringstream save_spin_configuration(int spin_config_number) = 0; + +// Model() { +// std::cout << "Break 1" << std::endl; +// lattice->find_neighbors(); +// std::cout << "Break 2" << std::endl; +// } }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index a74d1ff..c05c5fc 100644 --- a/main.cpp +++ b/main.cpp @@ -13,24 +13,6 @@ 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; - Lattice *lattice_ptr; Square square; lattice_ptr = □ @@ -39,18 +21,26 @@ int main() { Site *s_ptr = &s; - lattice_ptr->print_lattice(); + std::cout << "B1" << std::endl; + +// lattice_ptr->print_lattice(); - lattice_ptr->find_neighbor_indices(0, 0); - (*((lattice_ptr->lat)[0].neighbors[0])).print_site(); - (*((lattice_ptr->lat)[0].neighbors[1])).print_site(); +// std::cout << (*((lattice_ptr->lat)[7].neighbors[0])).x << std::endl; Model *model_ptr; Ising ising(16); model_ptr = &ising; + std::cout << "B2" << std::endl; + model_ptr->create_initial_spin_configuration(); - std::cout << model_ptr->spin_config << std::endl; +// std::cout << model_ptr->spin_config << std::endl; + + std::cout << "First spin:" << model_ptr->spin_config.row(0); + std::cout << "Second spin:" << model_ptr->spin_config.row(1); + std::cout << "Dot product:" << dot(model_ptr->spin_config.row(0), model_ptr->spin_config.row(1)) << std::endl; + + std::cout << "Energy: " << model_ptr->energy() << std::endl; return 0; } \ No newline at end of file