Feellgood
fem.h
Go to the documentation of this file.
1 #ifndef fem_h
2 #define fem_h
3 
9 #include <cassert>
10 #include <cmath>
11 #include <fstream>
12 #include <iostream>
13 #include <list>
14 #include <vector>
15 
16 #include <sys/times.h>
17 
18 #include "mesh.h"
19 #include "fmm_demag.h"
20 #include "spinAccumulationSolver.h"
21 #include "linear_algebra.h"
22 #include "chronometer.h"
23 #include "ANN/ANN.h"
24 #include "settings.h"
25 
27 const int NB_ENERGY_TERMS = 4;
28 
31  {
32  EXCHANGE = 0,
33  ANISOTROPY = 1,
34  DEMAG = 2,
35  ZEEMAN = 3
36  };
37 
42 class Fem
43  {
44 public:
46  inline Fem(Settings &mySets, timing &t_prm) : msh(mySets)
47  {
48  vmax = 0.0;
49  std::fill(E.begin(),E.end(),0);
50  Etot0 = INFINITY; // avoid "WARNING: energy increased" on first time step
51  Etot = 0.0;
52 
53  recenter_mem = false;
54  if (mySets.recenter)
55  {
56  if (mySets.verbose)
57  {
58  std::cout << "Approximate nearest neighbors:\n";
59  }
60  pts = annAllocPts(msh.getNbNodes(), Nodes::DIM);
61  if (!pts)
62  {
63  std::cout << "ANN memory error while allocating points" << std::endl;
64  SYSTEM_ERROR;
65  }
66  else if (mySets.verbose)
67  {
68  std::cout << " points allocated\n";
69  }
70 
71  for (int i = 0; i < msh.getNbNodes(); i++)
72  {
73  this->pts[i][0] = msh.getNode_p(i).x();
74  this->pts[i][1] = msh.getNode_p(i).y();
75  this->pts[i][2] = msh.getNode_p(i).z();
76  }
77 
78  if (mySets.verbose)
79  {
80  std::cout << " building kd_tree\n";
81  }
82 
83  kdtree = new ANNkd_tree(pts, msh.getNbNodes(), Nodes::DIM);
84  if (!kdtree)
85  {
86  std::cout << "ANN memory error while allocating kd_tree" << std::endl;
87  SYSTEM_ERROR;
88  }
89  recenter_mem = true;
90 
91  if (mySets.verbose)
92  {
93  std::cout << " kd_tree allocated\n";
94  }
95  }
96  else if (mySets.verbose)
97  {
98  std::cout << "No recentering.\n";
99  }
100 
101  if (mySets.restoreFileName == "")
102  {
103  msh.init_distrib(mySets);
104  }
105  else
106  {
107  t_prm.set_t(msh.readSol(mySets.verbose, mySets.restoreFileName));
108  }
109 
110  /* This potentially overrides the initial time set above by msh.readSol(). */
111  if (!isnan(mySets.initial_time))
112  {
113  t_prm.set_t(mySets.initial_time);
114  }
115 
116  if (mySets.recenter)
117  {
118  direction(Nodes::IDX_Z);
119  } /* DW propagation direction for recentering */
120  }
121 
124  {
125  if (recenter_mem)
126  {
127  annDeallocPts(pts);
128  delete kdtree;
129  }
130  }
131 
133  double vmax;
134 
136  std::array<double,NB_ENERGY_TERMS> E;
137 
139  double DW_dir;
140 
142  double Etot0;
143 
145  double Etot;
146 
149 
151  void energy(const double t
153  ,Settings &settings );
154 
158  inline void evolution(void)
159  {
160  msh.evolution();
161  Etot0 = Etot;
162  }
163 
165  void saver(Settings &settings , const timing &t_prm ,
166  std::ofstream &fout , const int nt ,
167  std::vector<Eigen::Vector3d> &s ) const;
168 
180  bool recenter(double thres ,
181  char recentering_direction );
182 
184  int time_integration(Settings &settings ,
185  LinAlgebra &linAlg ,
186  spinAcc &spinAcc_solver ,
187  scal_fmm::fmm &myFMM ,
188  timing &t_prm ,
189  int &nt );
190 
191 private:
193  ANNkd_tree *kdtree;
195  ANNpointArray pts;
198  void direction(enum Nodes::index idx_dir );
199 
202  void compute_all(Settings &settings , spinAcc &spinAcc_solver ,
203  scal_fmm::fmm &myFMM , const double t )
204  {
205  bool success(false);
206  chronometer fmm_counter(2);
207  myFMM.calc_demag(msh);
208  if (settings.spin_acc && spinAcc_solver.compute())
209  { success = true; }
210  if (settings.verbose)
211  {
212  std::cout << "magnetostatics done in " << fmm_counter.millis() << std::endl;
213  if (settings.spin_acc)
214  {
215  if (success)
216  { std::cout << "spin diffusion solved.\n"; }
217  else
218  { std::cout << "spin diffusion solver failed.\n"; }
219  }
220  }
221  energy(t, settings);
222  evolution();
223  }
224  }; // end class
225 
226 #endif
Definition: fem.h:43
double Etot
Definition: fem.h:145
void direction(enum Nodes::index idx_dir)
Definition: recentering.cpp:7
void evolution(void)
Definition: fem.h:158
std::array< double, NB_ENERGY_TERMS > E
Definition: fem.h:136
double Etot0
Definition: fem.h:142
Mesh::mesh msh
Definition: fem.h:148
bool recenter(double thres, char recentering_direction)
Definition: recentering.cpp:47
ANNpointArray pts
Definition: fem.h:195
ANNkd_tree * kdtree
Definition: fem.h:193
void energy(const double t, Settings &settings)
Definition: energy.cpp:5
~Fem()
Definition: fem.h:123
double DW_dir
Definition: fem.h:139
int time_integration(Settings &settings, LinAlgebra &linAlg, spinAcc &spinAcc_solver, scal_fmm::fmm &myFMM, timing &t_prm, int &nt)
Definition: time_integration.cpp:136
bool recenter_mem
Definition: fem.h:192
Fem(Settings &mySets, timing &t_prm)
Definition: fem.h:46
void compute_all(Settings &settings, spinAcc &spinAcc_solver, scal_fmm::fmm &myFMM, const double t)
Definition: fem.h:202
void saver(Settings &settings, const timing &t_prm, std::ofstream &fout, const int nt, std::vector< Eigen::Vector3d > &s) const
Definition: save.cpp:14
double vmax
Definition: fem.h:133
Definition: linear_algebra.h:35
Definition: mesh.h:32
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:153
double readSol(bool VERBOSE, const std::string &fileName)
Definition: read.cpp:195
int getNbNodes(void) const
Definition: mesh.h:144
void init_distrib(const Settings &mySets)
Definition: mesh.h:224
void evolution(void)
Definition: mesh.h:189
Container for all the settings provided by the user, with conversions to/from YAML.
Definition: settings.h:70
int verbose
Definition: settings.h:136
bool spin_acc
Definition: settings.h:160
double initial_time
Definition: settings.h:196
std::string restoreFileName
Definition: settings.h:193
bool recenter
Definition: settings.h:145
Definition: chronometer.h:18
std::string millis()
Definition: chronometer.h:49
Definition: fmm_demag.h:60
void calc_demag(Mesh::mesh &msh)
Definition: fmm_demag.h:96
Definition: spinAccumulationSolver.h:23
bool compute(void)
Definition: spinAccumulationSolver.cpp:145
Definition: time_integration.h:7
void set_t(const double _t)
Definition: time_integration.h:54
const int NB_ENERGY_TERMS
Definition: fem.h:27
ENERGY_TYPE
Definition: fem.h:31
this header is the interface to scalfmm. Its purpose is to prepare an octree for the application of t...
secondary header, it grabs altogether the linear algebra by the solver to apply fem method It encap...
class mesh, readMesh is expecting a mesh file in gmsh format either text or binary,...
index
Definition: node.h:33
const int DIM
Definition: node.h:19
many settings to give some parameters to the solver, boundary conditions for the problem,...