Statistics
| Revision:

root / src / minimize_BFGS.cpp @ 2

History | View | Annotate | Download (2.2 kB)

1
#include "compute_energy_and_forces.h"
2
#include "minimize_BFGS.h"
3
#include <nlopt.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <cmath>
7

    
8
/* this function minimizes the mechanical energy */
9

    
10
void minimize_BFGS(int vertex_number, double *vertices, int edge_number, int *edges_vertices,
11
                   double *lengths, double *rest_lengths, double *edges_width, int *periodizer, double *width, double *height){
12

    
13
nlopt_opt opt;
14
opt=nlopt_create(NLOPT_LD_LBFGS,2*vertex_number+2);
15

    
16
nlopt_set_stopval(opt, -HUGE_VAL);
17
nlopt_set_maxeval(opt, -1.);
18
nlopt_set_maxtime(opt, -1.);
19
nlopt_set_xtol_rel(opt, -1.);
20
nlopt_set_xtol_abs1(opt, 1.e-6);
21
nlopt_set_ftol_rel(opt, -1.);
22
nlopt_set_ftol_abs(opt, -1.);
23

    
24
struct parameters_list {int vertex_number; int edge_number; int *edges_vertices;
25
                        double *lengths; double *rest_lengths; double *edges_width; int *periodizer; int anisotropy; double ref_width; double ref_height;};
26

    
27
double min_energy;
28
struct parameters_list parameters;
29
parameters.vertex_number=vertex_number;
30
parameters.edge_number=edge_number;
31
parameters.edges_vertices=edges_vertices;
32
parameters.lengths=lengths;
33
parameters.rest_lengths=rest_lengths;
34
parameters.edges_width=edges_width;
35
parameters.periodizer=periodizer;
36
parameters.anisotropy=0;
37
parameters.ref_width=0;
38
parameters.ref_height=0;
39
void* pparameters=&parameters;
40

    
41
double vertices_BFGS[2*vertex_number+2];
42
for (int i=0; i<2*vertex_number;i++){ vertices_BFGS[i]=vertices[i];}
43
vertices_BFGS[2*vertex_number]=width[0]; vertices_BFGS[2*vertex_number+1]=height[0];
44

    
45
/// first resolution: find the reference width and height without the anisotropic stress
46
int reso=nlopt_set_min_objective(opt, compute_energy_and_forces, pparameters);
47
nlopt_optimize(opt, vertices_BFGS, &min_energy);
48

    
49
parameters.anisotropy=1;
50
parameters.ref_width=vertices_BFGS[2*vertex_number];
51
parameters.ref_height=vertices_BFGS[2*vertex_number+1];
52

    
53
/// second resolution with the anisotropic stress
54
reso=nlopt_set_min_objective(opt, compute_energy_and_forces, pparameters);
55
nlopt_optimize(opt, vertices_BFGS, &min_energy);
56

    
57
for (int i=0; i<2*vertex_number;i++){vertices[i]=vertices_BFGS[i];}
58
width[0]=vertices_BFGS[2*vertex_number]; height[0]=vertices_BFGS[2*vertex_number+1];
59

    
60
nlopt_destroy(opt);
61

    
62
}