Advertisement
Tertius

atividade11

Dec 9th, 2019
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <assert.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <GLFW/glfw3.h>
  8. #include <windows.h>
  9. #include <wincon.h>
  10. #include "bitmap-font-opengl.h"
  11. #define M_PI 3.14159265358979323846
  12.  
  13. double demonio = -10;
  14.  
  15. static void error_callback(int error, const char* description)            //acusar eventuais impedimentos da janela ser aberta
  16. {
  17.     fprintf(stderr, "Error: %s\n", description);
  18. }
  19.  
  20. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)         //listar respostas do programa a comandos no teclado
  21. {
  22.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  23.     {
  24.         glfwSetWindowShouldClose (window, GLFW_TRUE);
  25.     }
  26.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  27.     {
  28.         glfwSetWindowShouldClose (window, GLFW_TRUE);
  29.     }
  30. }
  31.  
  32. void gerar_sitio(int N, int sitio[][N])
  33. {
  34.     int r;
  35.     int i, j;
  36.  
  37.     for(i = 0; i < N; i++)
  38.     {
  39.         for(j = 0; j < N; j++)
  40.         {
  41.             r = rand();
  42.             if(r%2 == 0)
  43.             {
  44.                 sitio[i][j] = -1;
  45.             }
  46.             else
  47.             {
  48.                 sitio[i][j] = 1;
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. double connectionvalue(int N, int sitio[][N], int J, int i, int j)
  55. {
  56.  
  57.     // Calcula o valor da conexão entre um elemento do sitio e seus primeiros vizinhos. Uso condições de contorno periódicas!
  58.  
  59.     if (i != 0 && i != N-1 && j != 0 && j != N-1) // Caso normal, elemento dentro do "bulk" do sitio
  60.     {
  61.         return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][j-1]));
  62.     }
  63.  
  64.     if (i != N-1 && j != 0 && j != N-1) // Fronteira esquerda
  65.     {
  66.         return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[N-1][j] + sitio[i][j+1] + sitio[i][j-1]));
  67.     }
  68.  
  69.     if (i != 0 && j != 0 && j != N-1) // Fronteira direita
  70.     {
  71.         return -J*(sitio[i][j]*(sitio[0][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][j-1]));
  72.     }
  73.  
  74.     if (i != 0 && i != N-1 && j != N-1) // Fronteira de cima
  75.     {
  76.         return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][N-1]));
  77.     }
  78.  
  79.     if (i != 0 && i != N-1 && j != 0) // Fronteira de baixo
  80.     {
  81.         return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[i-1][j] + sitio[i][0] + sitio[i][j-1]));
  82.     }
  83.  
  84.     if (i == 0 && j == 0) // Corner superior esquerdo
  85.     {
  86.         return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[N-1][j] + sitio[i][j+1] + sitio[i][N-1]));
  87.     }
  88.  
  89.     if (i == 0 && j == N-1) // Corner inferior esquerdo
  90.     {
  91.         return -J*(sitio[i][j]*(sitio[i+1][j] + sitio[N-1][j] + sitio[i][0] + sitio[i][j-1]));
  92.     }
  93.  
  94.     if (i == N-1 && j == 0) // Corner superior direito
  95.     {
  96.         return -J*(sitio[i][j]*(sitio[0][j] + sitio[i-1][j] + sitio[i][j+1] + sitio[i][N-1]));
  97.     }
  98.  
  99.     if (i == N-1 && j == N-1) // Corner inferior direito
  100.     {
  101.         return -J*(sitio[i][j]*(sitio[0][j] + sitio[i-1][j] + sitio[i][0] + sitio[i][j-1]));
  102.     }
  103.     else
  104.     {
  105.         return 0;
  106.     }
  107. }
  108.  
  109. void sortear(int N, int sitio[][N], int J, int B)
  110. {
  111.     int x;
  112.     int y;
  113.     double delta_energia = 0;
  114.  
  115.     x = rand()%(N);
  116.     y = rand()%(N);
  117.  
  118.     delta_energia = connectionvalue(N, sitio, J, x, y) - B*sitio[x][y];
  119.  
  120.     sitio[x][y] = -1*sitio[x][y];
  121.  
  122.     delta_energia = connectionvalue(N, sitio, J, x, y) - B*sitio[x][y] - delta_energia;
  123.  
  124.     if(delta_energia <= 0)
  125.     {
  126.         demonio = demonio + (-1*delta_energia);
  127.     }
  128.     else if(delta_energia > 0 && demonio > delta_energia)
  129.     {
  130.         demonio = demonio - delta_energia;
  131.     }
  132.     else if(delta_energia > 0 && demonio < delta_energia)
  133.     {
  134.         sitio[x][y] = -1*sitio[x][y];
  135.     }
  136. }
  137.  
  138. int main()
  139. {
  140.     int i, j;
  141.     int N = 10;
  142.     int J = -1;
  143.     int B = 0;
  144.     int sitio[N][N];
  145.  
  146.     gerar_sitio(N, sitio);
  147.  
  148.     GLFWwindow* window;
  149.  
  150.     if (!glfwInit())         // Initialize the library //
  151.         return -1;
  152.  
  153.     window = glfwCreateWindow(600, 600, "Modelo de Ising", NULL, NULL);     // Create a windowed mode window and its OpenGL context //
  154.     if (!window)
  155.     {
  156.         glfwTerminate();
  157.         return -1;
  158.     }
  159.     glfwMakeContextCurrent(window);
  160.     glfwSetKeyCallback(window,key_callback);
  161.     glfwSwapInterval(2);
  162.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  163.  
  164.     glfwSetErrorCallback(error_callback);   //avisar que houve erro
  165.  
  166.     init();
  167.     while (!glfwWindowShouldClose(window))
  168.     {
  169.         //for(k=1;k<C;k++){
  170.         glClear(GL_COLOR_BUFFER_BIT);
  171.         sortear(N, sitio, J, B);
  172.  
  173.         glPointSize(600/N);
  174.         glBegin(GL_POINTS);
  175.         glColor3d(1, 0, 0);
  176.         for(i = 0; i < N; i++)
  177.         {
  178.             for(j = 0; j < N; j++)
  179.             {
  180.  
  181.                 if(sitio[i][j] == 1)
  182.                 {
  183.                     glColor3d(1, 0, 0);
  184.                 }
  185.                 if(sitio[i][j] == -1)
  186.                 {
  187.                     glColor3d(0, 0, 1);
  188.                 }
  189.                 glVertex2f((float)2*(i+1)/(N+1) - 1, (float)2*(j+1)/(N+1) - 1);
  190.             }
  191.         }
  192.         glEnd();
  193.         glfwSwapBuffers(window);
  194.         glfwPollEvents();
  195.  
  196.     }
  197.  
  198.     glfwDestroyWindow(window);
  199.     glfwTerminate();
  200.  
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement