root / bin / minimize_BFGS.cpp @ 1
Historique | Voir | Annoter | Télécharger (2,2 ko)
1 | 1 | akiss | #include "compute_energy_and_forces.h" |
---|---|---|---|
2 | 1 | akiss | #include "minimize_BFGS.h" |
3 | 1 | akiss | #include <nlopt.h> |
4 | 1 | akiss | #include <stdio.h> |
5 | 1 | akiss | #include <stdlib.h> |
6 | 1 | akiss | #include <cmath> |
7 | 1 | akiss | |
8 | 1 | akiss | /* this function minimizes the mechanical energy */
|
9 | 1 | akiss | |
10 | 1 | akiss | void minimize_BFGS(int vertex_number, double *vertices, int edge_number, int *edges_vertices, |
11 | 1 | akiss | double *lengths, double *rest_lengths, double *edges_width, int *periodizer, double *width, double *height){ |
12 | 1 | akiss | |
13 | 1 | akiss | nlopt_opt opt; |
14 | 1 | akiss | opt=nlopt_create(NLOPT_LD_LBFGS,2*vertex_number+2); |
15 | 1 | akiss | |
16 | 1 | akiss | nlopt_set_stopval(opt, -HUGE_VAL); |
17 | 1 | akiss | nlopt_set_maxeval(opt, -1.);
|
18 | 1 | akiss | nlopt_set_maxtime(opt, -1.);
|
19 | 1 | akiss | nlopt_set_xtol_rel(opt, -1.);
|
20 | 1 | akiss | nlopt_set_xtol_abs1(opt, 1.e-6); |
21 | 1 | akiss | nlopt_set_ftol_rel(opt, -1.);
|
22 | 1 | akiss | nlopt_set_ftol_abs(opt, -1.);
|
23 | 1 | akiss | |
24 | 1 | akiss | struct parameters_list {int vertex_number; int edge_number; int *edges_vertices; |
25 | 1 | akiss | double *lengths; double *rest_lengths; double *edges_width; int *periodizer; int anisotropy; double ref_width; double ref_height;}; |
26 | 1 | akiss | |
27 | 1 | akiss | double min_energy;
|
28 | 1 | akiss | struct parameters_list parameters;
|
29 | 1 | akiss | parameters.vertex_number=vertex_number; |
30 | 1 | akiss | parameters.edge_number=edge_number; |
31 | 1 | akiss | parameters.edges_vertices=edges_vertices; |
32 | 1 | akiss | parameters.lengths=lengths; |
33 | 1 | akiss | parameters.rest_lengths=rest_lengths; |
34 | 1 | akiss | parameters.edges_width=edges_width; |
35 | 1 | akiss | parameters.periodizer=periodizer; |
36 | 1 | akiss | parameters.anisotropy=0;
|
37 | 1 | akiss | parameters.ref_width=0;
|
38 | 1 | akiss | parameters.ref_height=0;
|
39 | 1 | akiss | void* pparameters=¶meters;
|
40 | 1 | akiss | |
41 | 1 | akiss | double vertices_BFGS[2*vertex_number+2]; |
42 | 1 | akiss | for (int i=0; i<2*vertex_number;i++){ vertices_BFGS[i]=vertices[i];} |
43 | 1 | akiss | vertices_BFGS[2*vertex_number]=width[0]; vertices_BFGS[2*vertex_number+1]=height[0]; |
44 | 1 | akiss | |
45 | 1 | akiss | /// first resolution: find the reference width and height without the anisotropic stress
|
46 | 1 | akiss | int reso=nlopt_set_min_objective(opt, compute_energy_and_forces, pparameters);
|
47 | 1 | akiss | nlopt_optimize(opt, vertices_BFGS, &min_energy); |
48 | 1 | akiss | |
49 | 1 | akiss | parameters.anisotropy=1;
|
50 | 1 | akiss | parameters.ref_width=vertices_BFGS[2*vertex_number];
|
51 | 1 | akiss | parameters.ref_height=vertices_BFGS[2*vertex_number+1]; |
52 | 1 | akiss | |
53 | 1 | akiss | /// second resolution with the anisotropic stress
|
54 | 1 | akiss | reso=nlopt_set_min_objective(opt, compute_energy_and_forces, pparameters); |
55 | 1 | akiss | nlopt_optimize(opt, vertices_BFGS, &min_energy); |
56 | 1 | akiss | |
57 | 1 | akiss | for (int i=0; i<2*vertex_number;i++){vertices[i]=vertices_BFGS[i];} |
58 | 1 | akiss | width[0]=vertices_BFGS[2*vertex_number]; height[0]=vertices_BFGS[2*vertex_number+1]; |
59 | 1 | akiss | |
60 | 1 | akiss | nlopt_destroy(opt); |
61 | 1 | akiss | |
62 | 1 | akiss | } |