~wbetz/fesslix/ssfem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* Fesslix - Stochastic Analysis
 * Copyright (C) 2010-2017 Wolfgang Betz
 *
 * Fesslix is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fesslix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Fesslix.  If not, see <http://www.gnu.org/licenses/>. 
 */


#define VERSION_REV 7
#define VERSION_DATE "2017-05-18"


#include "ssfem_obj.h"
#include "flxlibs_interface.h"


#ifdef __WINDOWS__
  #pragma comment(lib, "flxkernel")
#endif

flxlibs_interface flx_iface;


extern "C" FLXLIBSSFEM_EXPORT void flxlib_init( FlxObjectReadBox* objReadBox, FlxData* dataBox ) {
  GlobalVar.slog(4) << "  SSFEM-library - Version " << VERSION_REV << " (" << VERSION_DATE << ")" << std::endl;
  flx_iface.set_boxes(objReadBox,&(dataBox->FunBox));
  
  flx_iface.add_obj("ssfem", new FlxObjReadSFEM());
  flx_iface.add_obj("ssfemtest", new FlxObjReadSFEMtest()); 
  flx_iface.add_obj("isorfmat", new FlxObjReadIsoRFmat());
  flx_iface.add_obj("isorvmat", new FlxObjReadIsoRVmat());
    
  flx_iface.add_fun("ssfem_moment",new FunReadSFEM_moment() );
  flx_iface.add_fun("ssfem_covariance",new FunReadSFEM_covariance() );
  
  flxSFEM::rbrv_box = &(dataBox->rbrv_box);

}

extern "C" FLXLIBSSFEM_EXPORT void flxlib_close() {
  
  // make sure to unload the SSFEM object
    flxSFEM* sfem = dynamic_cast<flxSFEM*> (FEMbox_get());
    if (sfem) {
      delete sfem;
      FEMbox_get() = NULL;
    }

  flx_iface.free_memory();
}



void FlxObjSFEM_new::task()
{
  if (FEMbox_get()==NULL) {
    throw FlxException("FlxObjSFEM_new::task", "Please load the FEM-library first: 'loadlib \"FEM\";");
  };
  delete FEMbox_get();
  FEMbox_get() = new flxSFEM(p,&data->ConstantBox);
  GlobalVar.slog(4) << "SFEM: created new SFEM calculation" << std::endl;
}

FlxObjReadSFEM::FlxObjReadSFEM() 
{
  // order
    AllDefParaBox->insert(new FlxOptionalParaFun(3,"flxrandom::order"));
    ParaBox.insert("order", "flxrandom::order");
}

int FlxObjReadSFEM::get_order()
{
  FlxOptionalParaBase* p1 = ParaBox.get("order"); 
  void *v1 = p1->get();
  FlxFunction* fun = static_cast<FlxFunction*>( v1 ); 
  int iv = fun->cast2int();
  delete fun;
  if (iv < 0) {
    std::ostringstream ssV_2;
    ssV_2 << "Order of the homogeneous chaos (" << iv << ") must not be a negativ value.";
    throw FlxException("FlxObjReadSFEM::get_order", ssV_2.str(), reader->getCurrentPos() );
  }
  return iv;
}

FlxObjBase* FlxObjReadSFEM::read_new(bool errSerious)
{
  read_optionalPara(errSerious);
  return new FlxObjSFEM_new(get_doLog(),get_order(),get_stream(),get_verbose());
}

FlxObjBase* FlxObjReadSFEMtest::read() {
  //FlxFunction* f1 = new FlxFunction(funReader);
  int M = reader->get_UInt<int>();
  int p = reader->get_UInt<int>();
  read_optionalPara(false);
  return new FlxObjSFEMtest(get_doLog(), M, p, get_stream() );
}


void FlxObjSFEMtest::task()
{
  sout() << "Polynomial Chaos" << std::endl;
  
  PolyChaos* pb = new PolyChaos(M,p);

  tulong N = pb->get_SeqNumb();
  sout() << std::endl;
  sout() << N << ": " << pb->get_ExpPB2(N-1) << std::endl;
  
  delete pb;
}

const std::string FunSFEM_moment::write()
{
  return "sfem_moment(" + write_dof() + "," + moment->write() + ")";
}

const bool FunSFEM_moment::optimize(FunBasePtr& optf, const Fun_OptimizeInfo &foi)
{
  child_optimize(moment,foi);
  return FunFEM_base::optimize(optf,foi);
}

const bool FunSFEM_moment::dependOn_Const(const tdouble*const thenumber)
{
  throw FlxException_NotImplemented("FunSFEM_moment::dependOn_Const");
}

const tdouble FunSFEM_moment::calc()
{
  flxSFEM* sfem = dynamic_cast<flxSFEM*> (FEMbox_get());
  if (sfem == NULL) {
    std::ostringstream ssV;
    ssV << "This is not a stochastic finite element analysis.";
    throw FlxException("FunSFEM_moment::calc_1", ssV.str() );
  }
  
  int nth=int(round_flx(fabs(moment->calc())));
  if (nth < 0) {
    std::ostringstream ssV;
    ssV << "Moment to calculate must not be negative.";
    throw FlxException("FunSFEM_moment::calc_1", ssV.str() );
  } else if ( nth == 0) {
    return ONE;
  }
  
  flxDOFn* dofP = get_dof();
  FlxPeval* sp = sfem->get_Pol(dofP->get_DOFindex());
  tdouble mu = sp->calc_Expectation();
  
  if (nth == 1) {
    delete sp;
    return mu;
  }
  
  mu*=-1;
  sp->add_term(FlxPeval_term(sfem->get_RndBox().get_NRV()),mu);
  
  FlxPeval* spU = sp;
  sp = new FlxPeval(*sp);
  FlxPeval* spT1;
  for ( int i = 2; i <= nth; ++i) {
    spT1 = sp;
    sp = new FlxPeval(*spT1,*spU);
    delete spT1;
  }
  delete spU;
  
  const tdouble cv = sp->calc_Expectation();
  delete sp;
  return cv;
}

FunBase* FunReadSFEM_moment::read(bool errSerious)
{
  FunBase* node;
  FlxDof::dof dofV;
  read_node(node,dofV,errSerious);
  reader->getChar(',',errSerious); 
  FunBase* moment = FunctionList->read(errSerious);
  return new FunSFEM_moment(node,dofV,moment);
}

const std::string FunSFEM_covariance::write()
{
  return "sfem_covariance(" + write_dof() + "," + write_dof(node2,dof2) + ")";
}

const bool FunSFEM_covariance::optimize(FunBasePtr& optf, const Fun_OptimizeInfo& foi)
{
  child_optimize(node2,foi);
  child_optimize(joint2,foi);
  return FunFEM_base::optimize(optf,foi);
}

const bool FunSFEM_covariance::dependOn_Const(const tdouble*const thenumber)
{
  throw FlxException_NotImplemented("FunSFEM_covariance::dependOn_Const");
}

const tdouble FunSFEM_covariance::calc()
{
  flxSFEM* sfem = dynamic_cast<flxSFEM*> (FEMbox_get());
  if (sfem == NULL) {
    std::ostringstream ssV;
    ssV << "This is not a stochastic finite element analysis.";
    throw FlxException("FunSFEM_covariance::calc_1", ssV.str() );
  }
  
  flxDOFn* dofP = get_dof();
  flxDOFn* dofP2 = get_dof(node2,dof2);
  FlxPeval* sp = sfem->get_Pol(dofP->get_DOFindex());
  tdouble mu = sp->calc_Expectation();
  FlxPeval* sp2 = sfem->get_Pol(dofP2->get_DOFindex());
  tdouble mu2 = sp2->calc_Expectation();

  mu*=mu2;
  
  FlxPeval* spT = sp;
  sp = new FlxPeval(*spT,*sp2);
  delete spT; delete sp2;
  mu2 = sp->calc_Expectation();
  delete sp;
  return mu2 - mu;
}

FunBase* FunReadSFEM_covariance::read(bool errSerious)
{
  FunBase* node; FunBase* node2;
  FlxDof::dof dofV; FlxDof::dof dof2;
  read_node(node,dofV,errSerious);
  reader->getChar(',',errSerious);
  read_node(node2,dof2,errSerious);
  return new FunSFEM_covariance(node,dofV,node2,dof2);
}


// -------------------------- IsoRVMaterial ----------------------------------------------------------------------

flxIsoRVMat::flxIsoRVMat(FlxFunction* E, FlxFunction* sD, FlxFunction* nu, FlxFunction* density)
: flxIsotropicMaterial('E',E,'n',nu,density,0),sD(sD)
{

}


flxIsoRVMat::flxIsoRVMat(const flxIsoRVMat& FlxM, const tuint mid)
: flxIsotropicMaterial(FlxM,mid), sD(new FlxFunction(*(FlxM.sD)))
{
  std::ostringstream ssV;
  ssV << "mat_" << mid;
  ts = new RBRV_set_noise_RF(false,1,ssV.str(),false);
  try {
    data->rbrv_box.register_set(ts);
  } catch (FlxException &e) {
    delete ts;
    ts = NULL;
    throw;
  }
  y_smp = &(ts->get_y()[0]);
}

flxIsoRVMat* flxIsoRVMat::copy(const tuint mid)
{
  return new flxIsoRVMat(*this,mid);
}

void flxIsoRVMat::log_info(std::ostream& lout)
{
  lout << "  isorvmat: ";
  lout << v1c << "=" << v1->write() << "; " << v2c << "=" << v2->write() << "; ";
  lout << "Random in E: ";
  lout << "Normal(mu=" << v1->write() << "; sd=" << sD->write() << "); ";
  lout << std::endl;
}

const tdouble flxIsoRVMat::get_E() const
{
  const int Current_rv_index = current_RF_mode_get();
  if (Current_rv_index < 0) {
    tdouble d = flxIsotropicMaterial::get_E();
    d+=sD->cast2positive()*(*y_smp);
    return d;
  } else if (Current_rv_index == 0) {
    return flxIsotropicMaterial::get_E();
  } else if (static_cast<tuint>(Current_rv_index)-1 == ts->get_RndBox_index()) {
    return sD->cast2positive();
  } else return ZERO;
}

const tdouble flxIsoRVMat::get_G() const
{
  const tdouble E = get_E();
  const tdouble nu = get_nu();
  return check_G(E/(2*(ONE+nu)));
}

const tdouble flxIsoRVMat::get_lambda() const
{
  const tdouble E = get_E();
  const tdouble nu = get_nu();
  return E*nu/((ONE+nu)*(ONE-2*nu));
}

FlxFunction* FlxObjReadIsoRVmat::read_matprop(char& c, bool errSerious)
{
  c = 0;
  const std::string s1 = reader->getWord(true,errSerious);
  if (s1 == "e") {
    c = 'E';
  } else if (s1 == "sd") {
    c = 's';
  } else if (s1 == "nu") {
    c = 'n';
  } else { // ERROR: not known !!!
    std::ostringstream ssV_2;
    ssV_2 << "Material parameter '" << s1 << "' is not known.";
    FlxError(errSerious,"FlxObjReadIsoRVmat::read_matprop_1", ssV_2.str(), reader->getCurrentPos() );
  }
  return new FlxFunction(funReader,errSerious);
}

void FlxObjReadIsoRVmat::get_matprop(FlxFunctionPtr& E, FlxFunctionPtr& sD, FlxFunctionPtr& nu, bool errSerious)
{
  bool b1=true;
  do {
    char c1=0;
    FlxFunction* f=read_matprop(c1,errSerious);
    if (c1=='E') {
      if (E!=NULL) {
	std::ostringstream ssV_2;
	ssV_2 << "'E' was already defined.";
	FlxError(errSerious,"FlxObjReadIsoRVmat::read_1", ssV_2.str(), reader->getCurrentPos() );
      }
      E=f;
    } else if (c1=='s') {
      if (sD!=NULL) {
	std::ostringstream ssV_2;
	ssV_2 << "'sD' was already defined.";
	FlxError(errSerious,"FlxObjReadIsoRVmat::read_2", ssV_2.str(), reader->getCurrentPos() );
      }
      sD=f;
    } else if (c1=='n') {
      if (nu!=NULL) {
	std::ostringstream ssV_2;
	ssV_2 << "'nu' was already defined.";
	FlxError(errSerious,"FlxObjReadIsoRVmat::read_4", ssV_2.str(), reader->getCurrentPos() );
      }
      nu=f;
    }
    if (E!=NULL && sD!=NULL && nu!=NULL) b1=false;
  } while (b1);
}

FlxObjBase* FlxObjReadIsoRVmat::read()
{
  FlxFunction* index = new FlxFunction(funReader,false);
  FlxFunction *E=NULL, *sD=NULL, *nu=NULL;
  try {
    get_matprop(E,sD,nu);
    read_optionalPara(false);
    flxIsoRVMat* mat = new flxIsoRVMat(E,sD,nu,get_optPara_FlxFunction("density"));
    FlxObjBase* obj1 = new FlxObjIsoMaterial(get_doLog(),index, mat);
    return obj1;  
  } catch (FlxException &e) {
    FLXMSG("FlxObjReadIsoRVmat::read",1);
    delete index;
    if(E) delete E;
    if(sD) delete sD;
    if(nu) delete nu;
    throw;
  }
}

// -------------------------- IsoRFMaterial ----------------------------------------------------------------------


flxIsoRFMat::flxIsoRFMat(FlxRandomField_base* RFptr, FlxFunction* nu, FlxFunction* density, const tuint deg_E)
: flxIsotropicMaterial('E',RFptr->get_mean_fun(),'n',nu,density,deg_E), RFptr(RFptr)
{
  FlxRandomField_KL_Analytical_ExpCorr_Gauss_1D* klanalytgauss1d = dynamic_cast<FlxRandomField_KL_Analytical_ExpCorr_Gauss_1D*>(RFptr);
  if (klanalytgauss1d==NULL) {
    FlxRandomField_KL_FEM_Gauss* klfemgauss = dynamic_cast<FlxRandomField_KL_FEM_Gauss*>(RFptr);
    if (klfemgauss==NULL) {
      std::ostringstream ssV;
      ssV << "The specified random field is not a Gaussian random field.";
      throw FlxException("flxIsoRFMat::flxIsoRFMat", ssV.str() );
    }
  }
}

flxIsoRFMat::flxIsoRFMat(const flxIsoRFMat& FlxM, const tuint mid): flxIsotropicMaterial(FlxM,mid), RFptr(FlxM.RFptr)
{
  RFptr->set_automatic();
}

flxIsoRFMat* flxIsoRFMat::copy(const tuint mid)
{
  return new flxIsoRFMat(*this,mid);
}

void flxIsoRFMat::log_info(std::ostream& lout)
{
  lout << "  isorfmat: ";
  lout << v1c << "=" << v1->write() << "; " << v2c << "=" << v2->write() << "; ";
  lout << "Random in E: ";
  RFptr->print_para(lout);
  lout << std::endl;
}

const bool flxIsoRFMat::depend_E_on_Const(const tdouble* const thenumber) {
  if (ConstantBox->is_SpatialVar(thenumber)) return true;
  else return flxIsotropicMaterial::depend_E_on_Const(thenumber);
}

const tdouble flxIsoRFMat::get_E() const
{
  const int Current_rv_index = current_RF_mode_get();
  if (!RFptr->is_Assembled() || Current_rv_index==0) return flxIsotropicMaterial::get_E();
  if (Current_rv_index == -1) {
    return RFptr->get_realization();
  } else {
    return RFptr->calc_Hi(Current_rv_index,true);
  }
}

const tdouble flxIsoRFMat::get_G() const
{
  const tdouble E = get_E();
  const tdouble nu = get_nu();
  return check_G(E/(2*(ONE+nu)));
}

const tdouble flxIsoRFMat::get_lambda() const
{
  const tdouble E = get_E();
  const tdouble nu = get_nu();
  return E*nu/((ONE+nu)*(ONE-2*nu));
}

FlxObjBase* FlxObjReadIsoRFmat::read()
{
  FlxFunction* index = new FlxFunction(funReader);
  FlxFunction *RFindex=NULL,*nu=NULL;
  try {
    RFindex = new FlxFunction(funReader);
    reader->getWord("nu",false);
    nu = new FlxFunction(funReader);
    read_optionalPara(false);
    FlxRandomField_base* RFptr = RFbox_get().get(RFindex->cast2tuintW0(false)); 
    delete RFindex; RFindex = NULL;
    RFindex = get_optPara_FlxFunction("degreee");
    const tuint deg_E = RFindex->cast2tuintW0(false);
    delete RFindex; RFindex = NULL;
    flxIsoRFMat *mat = new flxIsoRFMat(RFptr,nu,get_optPara_FlxFunction("density"),deg_E);
    return new FlxObjIsoMaterial(get_doLog(), index, mat);
  } catch ( FlxException& e) {
    FLXMSG("FlxObjReadIsoRFmat::read",1);
    delete index;
    if (RFindex) delete RFindex;
    if (nu) delete nu;
    throw;
  }
}