MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
constltp_ann.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/constltp_ann.h,v 1.22 2017/01/12 14:46:09 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #ifndef CONSTLAW_ANN_H
33 #define CONSTLAW_ANN_H
34 
35 #include "dataman.h"
36 #include "constltp.h"
37 #include "ann.h"
38 
39 template <class T, class Tder>
41 : public ConstitutiveLaw<T, Tder> {
42 protected:
43  bool bUnit;
44  unsigned dim;
45  ANN *net;
46  std::string fname;
47 
48  void AnnInit(void)
49  {
50  if (typeid(T) == typeid(Vec3)) {
51  dim = 3;
52 
53  } else if (typeid(T) == typeid(Vec6)) {
54  dim = 6;
55 
56  } else {
58  }
59 
60  net = new ANN;
61  if (ANN_init(net, fname.c_str()) != 0) {
63  }
64  }
65 
66  virtual void
67  AnnSanity(void)
68  {
69  /* add sanity checks */
70  NO_OP;
71  }
72 
73  virtual void Update(const T& Eps, const T& EpsPrime, int feedback) {
74 
76 
78 
81 
82  for (unsigned r = 0; r < dim; r++) {
83  net->input.vec[r] = E(r + 1);
84  }
85  if (ANN_sim(net, &net->input, &net->output, feedback)) {
87  }
89 
90  for (unsigned r = 0; r < dim; r++) {
92  for (unsigned c = 0; c < dim; c++) {
93  ConstitutiveLaw<T, Tder>::FDE(r + 1, c + 1) = -net->jacobian.mat[r][c];
94  }
95  }
96  };
97 
98 
99 public:
100  AnnElasticConstitutiveLaw(const std::string& f, bool b = false)
101  : bUnit(b), fname(f)
102  {
103  AnnInit();
104  AnnSanity();
105  }
106 
108  if (net != 0) {
109  (void)ANN_destroy(net);
110  delete net;
111  }
112  };
113 
114  virtual ConstLawType::Type GetConstLawType(void) const {
115  return ConstLawType::ELASTIC;
116  };
117 
118  virtual void
119  AfterConvergence(const T& Eps, const T& EpsPrime = 0.)
120  {
121  Update(Eps, EpsPrime, ANN_FEEDBACK_UPDATE);
122  };
123 
124  virtual ConstitutiveLaw<T, Tder>* pCopy(void) const {
125  ConstitutiveLaw<T, Tder>* pCL = NULL;
126 
128  SAFENEWWITHCONSTRUCTOR(pCL, cl, cl(fname));
129  return pCL;
130  };
131 
132  virtual std::ostream& Restart(std::ostream& out) const {
133  return out << " ann elastic, \"" << fname << "\",";
134  };
135 
136  virtual void Update(const T& Eps, const T& EpsPrime = 0.) {
137  Update(Eps, EpsPrime, ANN_FEEDBACK_NONE);
138  };
139 };
140 
141 template <>
143 : public ConstitutiveLaw<doublereal, doublereal> {
144 protected:
145  bool bUnit;
147  std::string fname;
148 
149  void AnnInit(void)
150  {
151  net = new ANN;
152  if (ANN_init(net, fname.c_str()) != 0) {
154  }
155  }
156 
157  virtual void
158  AnnSanity(void)
159  {
160  /* add sanity checks */
161  NO_OP;
162  }
163 
164  virtual void Update(const doublereal& Eps, const doublereal& EpsPrime, int feedback) {
165 
167 
169 
172 
173  net->input.vec[0] = E;
174  if (net->N_input == 2) {
175  if (bUnit) {
176  net->input.vec[1] = 1.;
177  } else {
178  net->input.vec[1] = 0.;
179  }
180  }
181 
182  if (ANN_sim(net, &net->input, &net->output, feedback)) {
184  }
186 
189  };
190 
191 public:
192  AnnElasticConstitutiveLaw(const std::string& f, bool b = false)
193  : bUnit(b), fname(f)
194  {
195  AnnInit();
196  AnnSanity();
197  }
198 
200  if (net != 0) {
201  (void)ANN_destroy(net);
202  delete net;
203  }
204  };
205 
206  virtual ConstLawType::Type GetConstLawType(void) const {
207  return ConstLawType::ELASTIC;
208  };
209 
210  virtual void
211  AfterConvergence(const doublereal& Eps, const doublereal& EpsPrime = 0.)
212  {
213  Update(Eps, EpsPrime, ANN_FEEDBACK_UPDATE);
214  };
215 
218 
220  SAFENEWWITHCONSTRUCTOR(pCL, cl, cl(fname));
221  return pCL;
222  };
223 
224  virtual std::ostream& Restart(std::ostream& out) const {
225  return out << " ann elastic, \"" << fname << "\",";
226  };
227 
228  virtual void Update(const doublereal& Eps, const doublereal& EpsPrime = 0.) {
229  Update(Eps, EpsPrime, ANN_FEEDBACK_NONE);
230  };
231 };
232 
233 template <class T, class Tder>
235 : public AnnElasticConstitutiveLaw<T, Tder> {
236 protected:
237  /* overrides AnnElasticConstitutiveLaw::AnnSanity() */
238  virtual void
239  AnnSanity(void)
240  {
241  /* add sanity checks */
242  NO_OP;
243  }
244 
245  virtual void Update(const T& Eps, const T& EpsPrime, int feedback) {
248 
250 
253 
255 
256  /* loop according to dimensions... */
257  for (unsigned r = 0; r < AnnViscoElasticConstitutiveLaw<T, Tder>::dim; r++) {
258  net->input.vec[r] = E(r + 1);
259  net->input.vec[AnnViscoElasticConstitutiveLaw<T, Tder>::dim + r] = EpsPrime(r + 1);
260  }
261  if (ANN_sim(net, &net->input, &net->output, feedback))
262  {
263  silent_cout("AnnViscoElasticConstitutiveLaw: Network simulation error" << std::endl);
265  }
266  ANN_jacobian_matrix(net, &net->jacobian);
267 
268  for (unsigned r = 0; r < AnnViscoElasticConstitutiveLaw<T, Tder>::dim; r++) {
269  ConstitutiveLaw<T, Tder>::F(r + 1) = -net->output.vec[r];
270  for (unsigned c = 0; c < AnnViscoElasticConstitutiveLaw<T, Tder>::dim; c++) {
271  ConstitutiveLaw<T, Tder>::FDE(r + 1, c + 1) = -net->jacobian.mat[r][c];
272  }
273  }
274  };
275 
276 public:
277  AnnViscoElasticConstitutiveLaw(const std::string& f, bool b = false)
278  : AnnElasticConstitutiveLaw<T, Tder>(f, b)
279  {
280  NO_OP;
281  };
282 
284  NO_OP;
285  };
286 
289  };
290 
291  virtual ConstitutiveLaw<T, Tder>* pCopy(void) const {
292  ConstitutiveLaw<T, Tder>* pCL = NULL;
293 
295  SAFENEWWITHCONSTRUCTOR(pCL, cl, cl(cl::fname));
296  return pCL;
297  };
298 };
299 
300 template <>
302 : public AnnElasticConstitutiveLaw<doublereal, doublereal> {
303 protected:
304  /* overrides AnnElasticConstitutiveLaw::AnnSanity() */
305  virtual void
306  AnnSanity(void)
307  {
308  /* add sanity checks */
309  NO_OP;
310  }
311 
312  virtual void Update(const doublereal& Eps, const doublereal& EpsPrime, int feedback ) {
315 
317 
319  net->input.vec[0] = E;
320  net->input.vec[1] = EpsPrime;
321  if (net->N_input == 3) {
322  if (bUnit) {
323  net->input.vec[2] = 1.;
324  } else {
325  net->input.vec[2] = 0.;
326  }
327  }
328  if (ANN_sim(net, &net->input, &net->output, feedback))
329  {
330  silent_cout("AnnViscoElasticConstitutiveLaw: Network simulation error" << std::endl);
332  }
333  ANN_jacobian_matrix(net, &net->jacobian );
334 
337  };
338 
339 public:
340  AnnViscoElasticConstitutiveLaw(const std::string& f, bool b = false)
342  {
343  NO_OP;
344  };
345 
347  NO_OP;
348  };
349 
352  };
353 
356 
358  SAFENEWWITHCONSTRUCTOR(pCL, cl, cl(cl::fname));
359  return pCL;
360  };
361 };
362 
363 /* specific functional object(s) */
364 template <class T, class Tder>
365 struct AnnElasticCLR : public ConstitutiveLawRead<T, Tder> {
366  virtual ConstitutiveLaw<T, Tder> *
367  Read(const DataManager* pDM, MBDynParser& HP, ConstLawType::Type& CLType) {
368  ConstitutiveLaw<T, Tder>* pCL = 0;
369 
370  CLType = ConstLawType::ELASTIC;
371 
372  bool bUnit(false);
373  if (HP.IsKeyWord("unit" "input")) {
374  bUnit = HP.GetYesNoOrBool();
375  }
376 
377  const char *s = HP.GetFileName();
378  if (s == 0) {
379  silent_cerr("AnnElasticCLR: "
380  "unable to get ann file name "
381  "at line " << HP.GetLineData() << std::endl);
383  }
384 
386  SAFENEWWITHCONSTRUCTOR(pCL, L, L(s, bUnit));
387 
388  return pCL;
389  };
390 };
391 
392 template <class T, class Tder>
393 struct AnnViscoElasticCLR : public ConstitutiveLawRead<T, Tder> {
394  virtual ConstitutiveLaw<T, Tder> *
395  Read(const DataManager* pDM, MBDynParser& HP, ConstLawType::Type& CLType) {
396  ConstitutiveLaw<T, Tder>* pCL = 0;
397 
399 
400  bool bUnit(false);
401  if (HP.IsKeyWord("unit" "input")) {
402  bUnit = HP.GetYesNoOrBool();
403  }
404 
405  const char *s = HP.GetFileName();
406  if (s == 0) {
407  silent_cerr("AnnViscoElasticCLR: "
408  "unable to get ann file name "
409  "at line " << HP.GetLineData() << std::endl);
411  }
412 
414  SAFENEWWITHCONSTRUCTOR(pCL, L, L(s, bUnit));
415 
416  return pCL;
417  };
418 };
419 
420 #endif // CONSTLAW_ANN_H
AnnViscoElasticConstitutiveLaw(const std::string &f, bool b=false)
Definition: constltp_ann.h:277
ann_res_t ANN_sim(ANN *net, vector *input, vector *output, unsigned flags)
Definition: ann.c:425
virtual void Update(const T &Eps, const T &EpsPrime, int feedback)
Definition: constltp_ann.h:245
AnnViscoElasticConstitutiveLaw(const std::string &f, bool b=false)
Definition: constltp_ann.h:340
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Definition: matvec3.h:98
virtual void AnnSanity(void)
Definition: constltp_ann.h:239
virtual ConstitutiveLaw< T, Tder > * Read(const DataManager *pDM, MBDynParser &HP, ConstLawType::Type &CLType)
Definition: constltp_ann.h:367
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
virtual std::ostream & Restart(std::ostream &out) const
Definition: constltp_ann.h:224
ann_res_t ANN_init(ANN *net, const char *FileName)
Definition: ann.c:48
#define NO_OP
Definition: myassert.h:74
virtual ConstitutiveLaw< T, Tder > * pCopy(void) const
Definition: constltp_ann.h:124
virtual void Update(const T &Eps, const T &EpsPrime=0.)
Definition: constltp_ann.h:136
#define ANN_FEEDBACK_NONE
Definition: ann.h:51
virtual void Update(const T &Eps, const T &EpsPrime, int feedback)
Definition: constltp_ann.h:73
virtual bool GetYesNoOrBool(bool bDefval=false)
Definition: parser.cc:1038
ConstLawType::Type GetConstLawType(void) const
Definition: constltp_ann.h:287
vector input
Definition: ann.h:116
virtual ConstLawType::Type GetConstLawType(void) const
Definition: constltp_ann.h:206
virtual void Update(const doublereal &Eps, const doublereal &EpsPrime=0.)
Definition: constltp_ann.h:228
Definition: matvec6.h:37
virtual ConstitutiveLaw< doublereal, doublereal > * pCopy(void) const
Definition: constltp_ann.h:216
virtual ConstitutiveLaw< doublereal, doublereal > * pCopy(void) const
Definition: constltp_ann.h:354
virtual ~AnnViscoElasticConstitutiveLaw(void)
Definition: constltp_ann.h:283
vector output
Definition: ann.h:116
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual ConstitutiveLaw< T, Tder > * pCopy(void) const
Definition: constltp_ann.h:291
virtual ConstitutiveLaw< T, Tder > * Read(const DataManager *pDM, MBDynParser &HP, ConstLawType::Type &CLType)
Definition: constltp_ann.h:395
#define ANN_FEEDBACK_UPDATE
Definition: ann.h:52
virtual void Update(const doublereal &Eps, const doublereal &EpsPrime, int feedback)
Definition: constltp_ann.h:164
virtual void AnnSanity(void)
Definition: constltp_ann.h:67
int N_input
Definition: ann.h:84
AnnElasticConstitutiveLaw(const std::string &f, bool b=false)
Definition: constltp_ann.h:100
virtual std::ostream & Restart(std::ostream &out) const
Definition: constltp_ann.h:132
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
matrix jacobian
Definition: ann.h:98
Definition: ann.h:74
static std::stack< cleanup * > c
Definition: cleanup.cc:59
double ** mat
Definition: matrix.h:62
virtual void Update(const doublereal &Eps, const doublereal &EpsPrime, int feedback)
Definition: constltp_ann.h:312
double * vec
Definition: matrix.h:69
virtual ~AnnElasticConstitutiveLaw(void)
Definition: constltp_ann.h:107
ann_res_t ANN_jacobian_matrix(ANN *net, matrix *jacobian)
Definition: ann.c:921
virtual void AfterConvergence(const T &Eps, const T &EpsPrime=0.)
Definition: constltp_ann.h:119
AnnElasticConstitutiveLaw(const std::string &f, bool b=false)
Definition: constltp_ann.h:192
double doublereal
Definition: colamd.c:52
struct ANN ANN
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
virtual ConstLawType::Type GetConstLawType(void) const
Definition: constltp_ann.h:114
virtual void AfterConvergence(const doublereal &Eps, const doublereal &EpsPrime=0.)
Definition: constltp_ann.h:211
ann_res_t ANN_destroy(ANN *net)
Definition: ann.c:218