1 /*
2    IGraph library.
3    Copyright (C) 2021  The igraph development team <igraph@igraph.org>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 
19 #include <igraph.h>
20 #include "test_utilities.inc"
21 
check_and_destroy(igraph_matrix_t * result,igraph_real_t half_size)22 void check_and_destroy(igraph_matrix_t *result, igraph_real_t half_size) {
23     igraph_real_t min, max;
24     igraph_matrix_minmax(result, &min, &max);
25     IGRAPH_ASSERT(min >= -half_size);
26     IGRAPH_ASSERT(max <= half_size);
27     igraph_matrix_destroy(result);
28 }
29 
main()30 int main() {
31     igraph_t g;
32     igraph_matrix_t result;
33 
34     igraph_rng_seed(igraph_rng_default(), 42);
35 
36     printf("No vertices:\n");
37     igraph_small(&g, 0, 0, -1);
38     igraph_matrix_init(&result, 0, 0);
39     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.001, /*node_mass*/ 30, /*spring_length*/ 0, /*spring constant*/ 1, /*max_sa_movement*/ 5, /*use seed*/ 0) == IGRAPH_SUCCESS);
40     print_matrix(&result);
41     igraph_matrix_destroy(&result);
42     igraph_destroy(&g);
43 
44     printf("One vertex.\n");
45     igraph_small(&g, 1, 0, -1);
46     igraph_matrix_init(&result, 0, 0);
47     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.001, /*node_mass*/ 30, /*spring_length*/ 0, /*spring constant*/ 1, /*max_sa_movement*/ 5, /*use seed*/ 0) == IGRAPH_SUCCESS);
48     check_and_destroy(&result, 1.0);
49     igraph_destroy(&g);
50 
51     printf("Full graph of 4 vertices, no loops.\n");
52     igraph_full(&g, 4, 0, 0);
53     igraph_matrix_init(&result, 0, 0);
54     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.001, /*node_mass*/ 30, /*spring_length*/ 0, /*spring constant*/ 1, /*max_sa_movement*/ 5, /*use seed*/ 0) == IGRAPH_SUCCESS);
55     check_and_destroy(&result, 20.0);
56     igraph_destroy(&g);
57 
58     printf("4 vertices, disconnected, with loops and multi-edges.\n");
59     igraph_small(&g, 4, 0, 0,0, 0,0, 0,0, 1,2, 1,2, 1,2, -1);
60     igraph_matrix_init(&result, 0, 0);
61     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.001, /*node_mass*/ 30, /*spring_length*/ 0, /*spring constant*/ 1, /*max_sa_movement*/ 5, /*use seed*/ 0) == IGRAPH_SUCCESS);
62     check_and_destroy(&result, 100.0);
63     igraph_destroy(&g);
64 
65     printf("Full graph of 4 vertices, no loops with no repulsion.\n");
66     igraph_full(&g, 4, 0, 0);
67     igraph_matrix_init(&result, 0, 0);
68     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.0, /*node_mass*/ 30, /*spring_length*/ 0, /*spring constant*/ 1, /*max_sa_movement*/ 5, /*use seed*/ 0) == IGRAPH_SUCCESS);
69     check_and_destroy(&result, 1.0);
70     igraph_destroy(&g);
71 
72     printf("4 vertices in a line, with no repulsion, spring length 1 and a seed:\n");
73     igraph_small(&g, 4, 0, 0,1, 1,2, 2,3, -1);
74     igraph_real_t seed[] = {0.15, -0.15, 0.05, -0.05, -0.05, 0.05, -0.15, 0.15};
75     matrix_init_real_row_major(&result, 4, 2, seed);
76     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.0, /*node_mass*/ 30, /*spring_length*/ 1, /*spring constant*/ 10, /*max_sa_movement*/ 5, /*use seed*/ 1) == IGRAPH_SUCCESS);
77     print_matrix(&result);
78     igraph_matrix_destroy(&result);
79     igraph_destroy(&g);
80 
81     printf("4 vertices in a line, with no repulsion, spring length 1 and a seed, no sa_movement:\n");
82     igraph_small(&g, 4, 0, 0,1, 1,2, 2,3, -1);
83     matrix_init_real_row_major(&result, 4, 2, seed);
84     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.0, /*node_mass*/ 30, /*spring_length*/ 1, /*spring constant*/ 10, /*max_sa_movement*/ 0, /*use seed*/ 1) == IGRAPH_SUCCESS);
85     print_matrix(&result);
86     igraph_matrix_destroy(&result);
87     igraph_destroy(&g);
88 
89     printf("Wrong size seed.\n");
90     igraph_small(&g, 4, 0, 0,1, 1,2, 2,3, -1);
91     matrix_init_real_row_major(&result, 3, 2, seed);
92     IGRAPH_ASSERT(igraph_layout_graphopt(&g, &result, /*niter*/ 500, /*node_charge*/ 0.0, /*node_mass*/ 30, /*spring_length*/ 1, /*spring constant*/ 10, /*max_sa_movement*/ 0, /*use seed*/ 1) == IGRAPH_SUCCESS);
93     check_and_destroy(&result, 1.0);
94     igraph_destroy(&g);
95 
96     VERIFY_FINALLY_STACK();
97     return 0;
98 }
99