Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <assert.h>
- #include <time.h>
- #include <string.h>
- #include <GLFW/glfw3.h>
- #include <windows.h>
- #include <wincon.h>
- #include "bitmap-font-opengl.h"
- #define M_PI 3.14159265358979323846
- double demonio = -10;
- static void error_callback(int error, const char* description) //acusar eventuais impedimentos da janela ser aberta
- {
- fprintf(stderr, "Error: %s\n", description);
- }
- static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) //listar respostas do programa a comandos no teclado
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- {
- glfwSetWindowShouldClose (window, GLFW_TRUE);
- }
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- {
- glfwSetWindowShouldClose (window, GLFW_TRUE);
- }
- }
- void gerar_sitio(int N, int sitio[][N])
- {
- int r;
- int i, j;
- for(i = 0; i < N; i++)
- {
- for(j = 0; j < N; j++)
- {
- r = rand();
- if(r%2 == 0)
- {
- sitio[i][j] = -1;
- }
- else
- {
- sitio[i][j] = 1;
- }
- }
- }
- }
- double connectionvalue(int N, int sitio[][N], int J, int i, int j)
- {
- // Calcula o valor da conexão entre um elemento do sitio e seus primeiros vizinhos. Uso condições de contorno periódicas!
- if (i != 0 && i != N-1 && j != 0 && j != N-1) // Caso normal, elemento dentro do "bulk" do sitio
- {
- return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][j-1]));
- }
- if (i != N-1 && j != 0 && j != N-1) // Fronteira esquerda
- {
- return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[N-1][j] + sitio[i][j+1] + sitio[i][j-1]));
- }
- if (i != 0 && j != 0 && j != N-1) // Fronteira direita
- {
- return -J*(sitio[i][j]*(sitio[0][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][j-1]));
- }
- if (i != 0 && i != N-1 && j != N-1) // Fronteira de cima
- {
- return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][N-1]));
- }
- if (i != 0 && i != N-1 && j != 0) // Fronteira de baixo
- {
- return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[i-1][j] + sitio[i][0] + sitio[i][j-1]));
- }
- if (i == 0 && j == 0) // Corner superior esquerdo
- {
- return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[N-1][j] + sitio[i][j+1] + sitio[i][N-1]));
- }
- if (i == 0 && j == N-1) // Corner inferior esquerdo
- {
- return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[N-1][j] + sitio[i][0] + sitio[i][j-1]));
- }
- if (i == N-1 && j == 0) // Corner superior direito
- {
- return -J*(sitio[i][j]*(sitio[0][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][N-1]));
- }
- if (i == N-1 && j == N-1) // Corner inferior direito
- {
- return -J*(sitio[i][j]*(sitio[0][j] + sitio[i-1][j] + sitio[i][0] + sitio[i][j-1]));
- }
- else
- {
- return 0;
- }
- }
- void sortear(int N, int sitio[][N], int J, int B)
- {
- int x;
- int y;
- double delta_energia = 0;
- x = rand()%(N);
- y = rand()%(N);
- delta_energia = connectionvalue(N, sitio, J, x, y) - B*sitio[x][y];
- sitio[x][y] = -1*sitio[x][y];
- delta_energia = connectionvalue(N, sitio, J, x, y) - B*sitio[x][y] - delta_energia;
- if(delta_energia <= 0)
- {
- demonio = demonio + (-1*delta_energia);
- }
- else if(delta_energia > 0 && demonio > delta_energia)
- {
- demonio = demonio - delta_energia;
- }
- else if(delta_energia > 0 && demonio < delta_energia)
- {
- sitio[x][y] = -1*sitio[x][y];
- }
- }
- int main()
- {
- int i, j;
- int N = 10;
- int J = -1;
- int B = 0;
- int sitio[N][N];
- gerar_sitio(N, sitio);
- GLFWwindow* window;
- if (!glfwInit()) // Initialize the library //
- return -1;
- window = glfwCreateWindow(600, 600, "Modelo de Ising", NULL, NULL); // Create a windowed mode window and its OpenGL context //
- if (!window)
- {
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window,key_callback);
- glfwSwapInterval(2);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glfwSetErrorCallback(error_callback); //avisar que houve erro
- init();
- while (!glfwWindowShouldClose(window))
- {
- //for(k=1;k<C;k++){
- glClear(GL_COLOR_BUFFER_BIT);
- sortear(N, sitio, J, B);
- glPointSize(600/N);
- glBegin(GL_POINTS);
- glColor3d(1, 0, 0);
- for(i = 0; i < N; i++)
- {
- for(j = 0; j < N; j++)
- {
- if(sitio[i][j] == 1)
- {
- glColor3d(1, 0, 0);
- }
- if(sitio[i][j] == -1)
- {
- glColor3d(0, 0, 1);
- }
- glVertex2f((float)2*(i+1)/(N+1) - 1, (float)2*(j+1)/(N+1) - 1);
- }
- }
- glEnd();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement