MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
struts.cc
Go to the documentation of this file.
1 
6 #include "globals.hh"
7 #include "geometry.hh"
8 #include "edges.hh"
9 #include "nodes.hh"
10 #include <gsl/gsl_errno.h>
11 #include <gsl/gsl_math.h>
12 #include <gsl/gsl_min.h>
13 #include <iostream>
14 using namespace std;
15 using namespace globals;
17 double fn1 (\
18  double x ,\
19  void * p )
20 {
21  // (void)(p); /* avoid unused parameter warning */
22  struct fn1_params * params = (struct fn1_params *)p;
23  int sv = (params->sv);
24  int incmax = (params->incmax);
25  int vmax = (params->vmax);
26  double **vert = (params->vert);
27  int **vinc = (params->vinc);
28  int ***smat = (params->smat);
29  bool report = (params->report);
30  int i,j,k;
31  dedge=x;
32  for (i = 0; i < nx; i++)
33  for (j = 0; j < ny; j++)
34  for (k = 0; k < nz; k++)
35  smat[i][j][k] = 0;
36  if (createEdges) {
37  makeEdgeStruts(smat,sv,vert,vinc,report);
38  }
39  if (createNodes) {
40  makeNodeStruts(smat,sv,incmax,vmax,vert,vinc,report);
41  }
42  double por = porosity(smat);
43  return pow(por-strutPorosity,2);
44 }
46 int optim (\
47  void * params ,\
48  double &m ,\
49  double &a ,\
50  double &b ,\
51  bool report )
52 {
53  int status;
54  int iter = 0, max_iter = 100;
55  const gsl_min_fminimizer_type *T;
56  gsl_min_fminimizer *s;
57  gsl_function F;
58  gsl_set_error_handler_off ();
59 
60  F.function = &fn1;
61  F.params = params;
62 
63  T = gsl_min_fminimizer_brent;
64  s = gsl_min_fminimizer_alloc (T);
65  status = gsl_min_fminimizer_set (s, &F, m, a, b);
66  if (status == GSL_EINVAL) {
67  if (report) {
68  cout << "endpoints do not enclose a minimum" << endl;
69  }
70  return(1);
71  } else if (status != GSL_SUCCESS) {
72  cout << "something is wrong in optim function" << endl;
73  }
74 
75  if (report) {
76  printf ("using %s method\n", gsl_min_fminimizer_name (s));
77  printf ("%5s [%9s, %9s] %9s %9s\n",
78  "iter", "lower", "upper", "min", "err(est)");
79  printf ("%5d [%.7f, %.7f] %.7f %.7f\n",iter, a, b, m, b - a);
80  }
81 
82  do {
83  iter++;
84  status = gsl_min_fminimizer_iterate (s);
85 
86  m = gsl_min_fminimizer_x_minimum (s);
87  a = gsl_min_fminimizer_x_lower (s);
88  b = gsl_min_fminimizer_x_upper (s);
89 
90  status
91  = gsl_min_test_interval (a, b, 0.01, 0.0);
92 
93  if (status == GSL_SUCCESS)
94  if (report) {
95  printf ("Converged:\n");
96  printf ("%5d [%.7f, %.7f] %.7f %.7f\n", iter, a, b, m, b - a);
97  }
98  }
99  while (status == GSL_CONTINUE && iter < max_iter);
100 
101  gsl_min_fminimizer_free (s);
102 
103  return status;
104 }
double porosity(int ***amat)
Calculates porosity.
Definition: geometry.cc:70
void makeNodeStruts(int ***amat, int sv, int incmax, int vmax, double **vert, int **vinc, bool report)
Definition: nodes.cc:19
double dedge
parameter influencing size of struts in cell edges
Definition: globals.cc:13
double fn1(double x, void *p)
Objective function for optimization. We want to have certain strut porosity.
Definition: struts.cc:17
bool createEdges
create struts at cell edges
Definition: globals.cc:8
int optim(void *params, double &m, double &a, double &b, bool report)
GSL minimizer for univariate scalar function.
Definition: struts.cc:46
Defines global variables, macros, templates and namespace.
void makeEdgeStruts(int ***amat, int sv, double **vert, int **vinc, bool report)
Definition: edges.cc:17
m
Bubble Growth Application Recipe.
Definition: bubbleGrowth.py:59
real(dp) por
porosity
Definition: constants.f90:42
int nx
domain size in X
Definition: globals.cc:19
bool createNodes
create struts at cell vertices
Definition: globals.cc:7
struct for passing paramaters to GSL function
Definition: globals.hh:27
integer nz
spatial discretization
Definition: constants.f90:32
namespace with global variables
Definition: globals.f90:8
double strutPorosity
Definition: globals.cc:14
int ny
domain size in Y
Definition: globals.cc:20