File: test_mat_elem.cc

package info (click to toggle)
getfem 5.4.4%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,640 kB
  • sloc: cpp: 126,151; ansic: 24,798; python: 9,244; sh: 3,648; perl: 1,829; makefile: 1,367
file content (365 lines) | stat: -rw-r--r-- 13,086 bytes parent folder | download
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
/*===========================================================================

 Copyright (C) 2002-2020 Yves Renard.

 This file is a part of GetFEM

 GetFEM  is  free software;  you  can  redistribute  it  and/or modify it
 under  the  terms  of the  GNU  Lesser General Public License as published
 by  the  Free Software Foundation;  either version 3 of the License,  or
 (at your option) any later version along with the GCC Runtime Library
 Exception either version 3.1 or (at your option) any later version.
 This program  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 Lesser General Public
 License and GCC Runtime Library Exception for more details.
 You  should  have received a copy of the GNU Lesser General Public License
 along  with  this program;  if not, write to the Free Software Foundation,
 Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.

===========================================================================*/
/* *********************************************************************** */
/*                                                                         */
/*   Program to test the efficiency of elementary matrices computation.    */
/*                                                                         */
/* *********************************************************************** */

#include "getfem/getfem_assembling.h"
#include "getfem/getfem_export.h"
#include "getfem/getfem_regular_meshes.h"
#include "getfem/getfem_mat_elem.h"
using std::endl; using std::cout; using std::cerr;
using std::ends; using std::cin;


using bgeot::base_vector;
using bgeot::base_small_vector;
using bgeot::base_node;
using bgeot::scalar_type;
using bgeot::size_type;
using bgeot::dim_type;

/**************************************************************************/
/*  structure representing the problem.                                   */
/**************************************************************************/

struct lap_pb {
  getfem::mesh mesh;
  getfem::mesh_im mim;
  getfem::mesh_fem mef;
  getfem::mesh_fem mef_data;

  scalar_type LX, LY, LZ, incline, residual;
  size_type N;
  int NX, K, fem_type, KI;
 
  int integration, mesh_type;

  std::string datafilename;
  bgeot::md_param PARAM;

  
  void init(void);
  lap_pb(void) : mim(mesh), mef(mesh), mef_data(mesh) {}
};

void lap_pb::init(void)
{
  dal::bit_vector nn;

  /***********************************************************************/
  /* READING PARAMETER FILE                                              */
  /***********************************************************************/
  
  N = PARAM.int_value("N", "Domaine dimension");
  LX = PARAM.real_value("LX", "Size in X");
  LY = PARAM.real_value("LY", "Size in Y");
  LZ = PARAM.real_value("LZ", "Size in Y");
  incline = PARAM.real_value("INCLINE", "incline of the mesh");
  NX = int(PARAM.int_value("NX", "Nomber of sace steps "));
  integration = int(PARAM.int_value("INTEGRATION", "integration method"));
  mesh_type = int(PARAM.int_value("MESH_TYPE", "Mesh type "));
  residual = PARAM.real_value("RESIDUAL", "Residu for c.g.");
  K = int(PARAM.int_value("K", "Finite element degree"));
  KI = int(PARAM.int_value("KI", "Integration degree"));
  fem_type = int(PARAM.int_value("FEM_TYPE", "Finite element method"));
  datafilename = std::string( PARAM.string_value("ROOTFILENAME",
			     "File name for saving"));

  /***********************************************************************/
  /*  BUILD MESH.                                                        */
  /***********************************************************************/

  cout << "Mesh generation\n";

  base_node org(N); 
  std::vector<base_small_vector> vtab(N);
  std::vector<size_type> ref(N); std::fill(ref.begin(), ref.end(), NX);
  for (dim_type i = 0; i < N; i++)
  { 
    vtab[i] = base_small_vector(N); 
    (vtab[i])[i] = ((i == 0) ? LX : ((i == 1) ? LY : LZ)) / scalar_type(NX);
  }
  if (N > 1) vtab[N-1][0] = incline * LX / scalar_type(NX);

  switch (mesh_type) {
  case 0 : getfem::parallelepiped_regular_simplex_mesh
      (mesh, bgeot::dim_type(N), org, vtab.begin(), ref.begin()); break;
  case 1 : getfem::parallelepiped_regular_mesh
      (mesh, bgeot::dim_type(N), org, vtab.begin(), ref.begin()); break;
  case 2 : getfem::parallelepiped_regular_prism_mesh
      (mesh, bgeot::dim_type(N), org, vtab.begin(), ref.begin()); break;
  default : GMM_ASSERT1(false, "Unknown type of mesh");
  }

  mesh.optimize_structure();

  if (mesh_type == 2 && N <= 1) mesh_type = 0;

  cout << "Selecting finite element method.\n";

  switch(fem_type) {
  case 0 : break;
  case 1 :
    if (N != 1 || mesh_type != 0)
      GMM_ASSERT1(false, "This element is only defined on segments");
     K = 3;
    break;
  case 2 : 
    if (mesh_type != 0)
      GMM_ASSERT1(false, "This element is only defined on simplexes");
    break;
  case 3 : 
    if (mesh_type != 0)
      GMM_ASSERT1(false, "This element is only defined on simplexes");
    break;
  default : GMM_ASSERT1(false, "Unknown finite element method");
  }

  getfem::pintegration_method ppi;
  char meth[500];
  nn = mesh.convex_index(bgeot::dim_type(N));
  switch (integration) {
  case 0 :
    switch (mesh_type) { 
    case 0 : snprintf(meth, 499, "IM_EXACT_SIMPLEX(%d)", int(N)); break;
    case 1 : snprintf(meth, 499, "IM_EXACT_PARALLELEPIPED(%d)", int(N)); break;
    default : GMM_ASSERT1(false,
			  "Exact integration not allowed in this context");
    }
    break;
  case 1 :
    switch (mesh_type) { 
    case 0 : 
      snprintf(meth, 499, "IM_NC(%d,%d)", int(N), int(2*K));
      break;
    case 1 : 
      snprintf(meth, 499, "IM_NC_PARALLELEPIPED(%d,%d)", int(N), int(2*K));
      break;
    case 2 :
      snprintf(meth, 499, "IM_NC_PRISM(%d,%d)", int(N), int(2*K));
      break;
    }
    break;
  case 2 :
    if (mesh_type == 1)
      snprintf(meth, 499, "IM_GAUSS_PARALLELEPIPED(%d,%d)", int(N), int(KI));
    else
      GMM_ASSERT1(false, "Product of 1D Gauss only for parallelepipeds");
    break;
  case 3 :
    if (mesh_type == 0) {
      if (N == 1)
	snprintf(meth, 499, "IM_STRUCTURED_COMPOSITE(IM_GAUSS1D(%d), %d)",2,int(KI));
      else if (N == 2)
	snprintf(meth, 499, "IM_STRUCTURED_COMPOSITE(IM_TRIANGLE(%d), %d)",
		2,int(KI));
      else
	snprintf(meth, 499, "IM_STRUCTURED_COMPOSITE(IM_NC(%d, %d), %d)",
		int(N), int(2*K), int(KI));
    }
    else
      GMM_ASSERT1(false, "Composite integration only for simplexes");
    break;
  case 11 : snprintf(meth, 499, "IM_TRIANGLE(1)"); break;
  case 12 : snprintf(meth, 499, "IM_TRIANGLE(2)"); break;
  case 13 : snprintf(meth, 499, "IM_TRIANGLE(3)"); break;
  case 14 : snprintf(meth, 499, "IM_TRIANGLE(4)"); break;
  case 15 : snprintf(meth, 499, "IM_TRIANGLE(5)"); break;
  case 16 : snprintf(meth, 499, "IM_TRIANGLE(6)"); break;
  case 17 : snprintf(meth, 499, "IM_TRIANGLE(7)"); break;
  case 21 : snprintf(meth, 499, "IM_TETRAHEDRON(1)"); break;
  case 22 : snprintf(meth, 499, "IM_TETRAHEDRON(2)"); break;
  case 23 : snprintf(meth, 499, "IM_TETRAHEDRON(3)"); break;
  case 25 : snprintf(meth, 499, "IM_TETRAHEDRON(5)"); break;
  case 32 : snprintf(meth, 499, "IM_QUAD(2)"); break;
  case 33 : snprintf(meth, 499, "IM_QUAD(3)"); break;
  case 35 : snprintf(meth, 499, "IM_QUAD(5)"); break;
  default : GMM_ASSERT1(false, "Undefined integration method");
  }
  ppi = getfem::int_method_descriptor(meth);
  getfem::pfem pfprinc = 0;
  switch (mesh_type) {
  case 0 :
    snprintf(meth, 499, "FEM_PK(%d,%d)", int(N), int(K));
    pfprinc = getfem::fem_descriptor(meth);
    mim.set_integration_method(nn, ppi);
    mef.set_finite_element(nn, getfem::fem_descriptor(meth));
    mef_data.set_finite_element(nn, getfem::fem_descriptor(meth));
    snprintf(meth, 499, "FEM_PK(%d,%d)", int(N), 0);
    
    break;
  case 1 :
    snprintf(meth, 499, "FEM_QK(%d,%d)", int(N), K);
    pfprinc = getfem::fem_descriptor(meth);
    mim.set_integration_method(nn, ppi);
    mef.set_finite_element(nn, getfem::fem_descriptor(meth)); 
    mef_data.set_finite_element(nn, getfem::fem_descriptor(meth));
    snprintf(meth, 499, "FEM_QK(%d,%d)", int(N), 0);
    
    break;
  case 2 :
    snprintf(meth, 499, "FEM_PK_PRISM(%d,%d)", int(N), K);
    pfprinc = getfem::fem_descriptor(meth);
    mim.set_integration_method(nn, ppi);
    mef.set_finite_element(nn, getfem::fem_descriptor(meth));
    mef_data.set_finite_element(nn, getfem::fem_descriptor(meth));
    snprintf(meth, 499, "FEM_PK_PRISM(%d,%d)", int(N), 0);
    
    break;
  }

  switch(fem_type) {

  case 0 : break;

  case 1 :
    snprintf(meth, 499, "FEM_HERMITE(1)");
    pfprinc = getfem::fem_descriptor(meth);
    mef.set_finite_element(nn, getfem::fem_descriptor(meth));
    break;
    
  case 2 :
    snprintf(meth, 499, "FEM_PK_HIERARCHICAL(%d, %d)", int(N), int(K));
    pfprinc = getfem::fem_descriptor(meth);
    mef.set_finite_element(nn, getfem::fem_descriptor(meth));
    break;

  case 3 :
    snprintf(meth, 499, "FEM_PK_HIERARCHICAL_COMPOSITE(%d,%d,%d)", int(N), 1, int(K));
    pfprinc = getfem::fem_descriptor(meth);
    mef.set_finite_element(nn, getfem::fem_descriptor(meth));
    break;
  
  }
  
  cout << "Name of principal finite element method : "
       << getfem::name_of_fem(pfprinc) << endl;
  cout << "Name of principal integration method : "
       << getfem::name_of_int_method(ppi) << endl;
}

void test1_mat_elem(const getfem::mesh_im &mim,
		    const getfem::mesh_fem &mf,
		   const getfem::mesh_fem &mfdata) { 
  
  size_type cv;
  dal::bit_vector nn = mf.convex_index();
  bgeot::base_tensor t;
  getfem::pfem pf1, pf2, pf1prec = 0, pf2prec = 0;
  getfem::pintegration_method pim, pimprec = 0;
  bgeot::pgeometric_trans pgt, pgtprec = 0;
  getfem::pmat_elem_computation pmec = 0;
  
  if (&(mf.linked_mesh()) != &(mfdata.linked_mesh()))
    GMM_ASSERT1(false,"This assembling procedure only works on a single mesh");
  
  for (cv << nn; cv != size_type(-1); cv << nn) {
    pf1 =     mf.fem_of_element(cv);
    pf2 = mfdata.fem_of_element(cv);
    pgt = mf.linked_mesh().trans_of_convex(cv);
    pim = mim.int_method_of_element(cv);
    if (pf1prec != pf1 || pf2prec != pf2 || pgtprec != pgt || pimprec != pim) {
      getfem::pmat_elem_type pme 
	= getfem::mat_elem_product
	(getfem::mat_elem_product(getfem::mat_elem_grad(pf1),
				  getfem::mat_elem_grad(pf1)),
	 getfem::mat_elem_base(pf2));
      pmec = getfem::mat_elem(pme, pim, pgt);
      pf1prec = pf1; pf2prec = pf2; pgtprec = pgt; pimprec = pim;
    }
    pmec->gen_compute(t, mf.linked_mesh().points_of_convex(cv), cv);
    // cout << "t = " << t << endl;
  }
}

void test2_mat_elem(const getfem::mesh_im &mim, const getfem::mesh_fem &mf,
		   const getfem::mesh_fem &mfdata) { 
  
  size_type cv;
  dal::bit_vector nn = mf.convex_index();
  bgeot::base_tensor t;
  getfem::pfem pf1, pf2, pf1prec = 0, pf2prec = 0;
  getfem::pintegration_method pim, pimprec = 0;
  bgeot::pgeometric_trans pgt, pgtprec = 0;
  getfem::pmat_elem_computation pmec = 0;
  
  if (&(mf.linked_mesh()) != &(mfdata.linked_mesh()))
    GMM_ASSERT1(false,"This assembling procedure only works on a single mesh");
  
  for (cv << nn; cv != size_type(-1); cv << nn) {
    pf1 =     mf.fem_of_element(cv);
    pf2 = mfdata.fem_of_element(cv);
    pgt = mf.linked_mesh().trans_of_convex(cv);
    pim = mim.int_method_of_element(cv);
    if (pf1prec != pf1 || pf2prec != pf2 || pgtprec != pgt || pimprec != pim) {
      getfem::pmat_elem_type pme 
	= getfem::mat_elem_product(getfem::mat_elem_base(pf1),
				   getfem::mat_elem_base(pf1));
      pmec = getfem::mat_elem(pme, pim, pgt);
      pf1prec = pf1; pf2prec = pf2; pgtprec = pgt; pimprec = pim;
    }
    pmec->gen_compute_on_face(t, mf.linked_mesh().points_of_convex(cv), 0, cv);
  }
}


/**************************************************************************/
/*  main program.                                                         */
/**************************************************************************/

int main(int argc, char *argv[]) {
  
  try {
    
    lap_pb p;
    scalar_type exectime = gmm::uclock_sec(), total_time = 0.0;
    
    // cout << "initialisation ...\n";
    p.PARAM.read_command_line(argc, argv);
    p.init();
    // cout << "Initialisation terminee\n";
    
    total_time += gmm::uclock_sec() - exectime;
    
    
    
    exectime = gmm::uclock_sec();
    test1_mat_elem(p.mim, p.mef, p.mef_data);
    cout << "Mat elem computation time 1 : "
	 << gmm::uclock_sec() - exectime << endl;
 
    exectime = gmm::uclock_sec();
    test2_mat_elem(p.mim, p.mef, p.mef_data);
    cout << "Mat elem computation time 2 : "
	 << gmm::uclock_sec() - exectime << endl;


    /* check mesh/mesh_fem I/O */
    p.mef.write_to_file(p.datafilename + ".mesh", true);
    p.mesh.read_from_file(p.datafilename + ".mesh");
    p.mef.read_from_file(p.datafilename + ".mesh");
  }
  GMM_STANDARD_CATCH_ERROR;
  return 0; 
}