File: Instruction.cpp

package info (click to toggle)
freefem3d 1.0pre8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,920 kB
  • ctags: 7,145
  • sloc: cpp: 45,748; sh: 8,698; yacc: 2,847; makefile: 600; ansic: 504; perl: 110
file content (485 lines) | stat: -rw-r--r-- 11,614 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
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
480
481
482
483
484
485
//  This file is part of ff3d - http://www.freefem.org/ff3d
//  Copyright (C) 2001, 2002, 2003 Stphane Del Pino

//  This program 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 2, 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 General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

//  $Id: Instruction.cpp,v 1.17 2005/11/03 21:12:50 delpinux Exp $

#include <config.h>

#include <cstdlib>
#include <fstream>

#include <Mesh.hpp>
#include <MeshWriter.hpp>

#include <Structured3DMesh.hpp>

#include <Instruction.hpp>
#include <FunctionExpression.hpp>
#include <StringExpression.hpp>

#include <Scene.hpp>

#include <MeshExpression.hpp>
#include <Variable.hpp>

#include <Information.hpp>

#include <VTKDriver.hpp>

#include <FEMFunction.hpp>
#include <ErrorHandler.hpp>

void InstructionExec::execute()
{
  (*__command).execute();
  std::string command = (*__command).value();
  ffout(2) << "executing: " << command << '\n';
#ifdef ALLOW_EXEC
  int result = system(command.c_str());
  if (result == -1) {
    throw ErrorHandler(__FILE__,__LINE__,
		       "could not execute: "+command,
		       ErrorHandler::normal);
  }
#else // ALLOW_EXEC
  fferr(2) << "warning: this version was not compiled with 'exec' support.\n";
#endif // ALLOW_EXEC
}

InstructionExec::InstructionExec(ReferenceCounting<StringExpression> command)
  : Instruction(Instruction::exec),
    __command(command)
{
  ;
}

InstructionExec::InstructionExec(const InstructionExec& I)
  : Instruction(I),
    __command(I.__command)
{
  ;
}

InstructionExec:: ~InstructionExec()
{
  ;
}


InstructionSave::
InstructionSave(ReferenceCounting<FileDescriptor> descriptor,
		ReferenceCounting<StringExpression> fileName,
		ReferenceCounting<MeshExpression> mesh)
  : Instruction(Instruction::save),
    __fileDescriptor(descriptor),
    __fileName(fileName),
    __mesh(mesh)
{
  ;
}

InstructionSave::InstructionSave(const InstructionSave& I)
  : Instruction(I),
    __fileDescriptor(I.__fileDescriptor),
    __fileName(I.__fileName),
    __mesh(I.__mesh)
{
  ;
}

InstructionSave::~InstructionSave()
{
  ;
}

InstructionSaveMesh::
InstructionSaveMesh(ReferenceCounting<FileDescriptor> descriptor,
		    ReferenceCounting<StringExpression> fileName,
		    ReferenceCounting<MeshExpression> mesh)
  : InstructionSave(descriptor, fileName, mesh)
{
  ;
}

InstructionSaveMesh::
InstructionSaveMesh(const InstructionSaveMesh& I)
  : InstructionSave(I)
{
  ;
}

InstructionSaveMesh::
~InstructionSaveMesh()
{
  ;
}

void InstructionSaveMesh::execute()
{
  (*__mesh).execute();
  (*__fileName).execute();
  Information::instance().setMesh(__mesh);

  const std::string CR = (*__fileDescriptor).cr();

  switch((*__fileDescriptor).format()) {
  case FileDescriptor::raw: {
    std::string msh = (*__fileName).value();
    throw ErrorHandler(__FILE__,__LINE__,
		       "cannot use raw format to save a mesh!",
		       ErrorHandler::normal);
    break;
  }
  case FileDescriptor::medit: {
    //! plots the Mesh
    std::string msh = (*__fileName).value();

    msh += ".mesh";
    
    std::ofstream fmsh(msh.c_str());
    if (not(fmsh)) {
      throw ErrorHandler(__FILE__,__LINE__,
			 "cannot open file '"+msh+"'",
			 ErrorHandler::normal);
    }

    MeshWriter w(fmsh,(*__mesh).mesh(),CR);

    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "unexpected file type",
		       ErrorHandler::normal);
  }
  }

  //! The mesh is nomore used in this region.
  Information::instance().unsetMesh();
}

InstructionSaveFunction::
InstructionSaveFunction(ReferenceCounting<FileDescriptor> descriptor,
			ReferenceCounting<StringExpression> fileName,
			ReferenceCounting<FunctionExpression> function,
			ReferenceCounting<MeshExpression> mesh)
  : InstructionSave(descriptor, fileName, mesh),
    __function(function)
{
  ;
}

InstructionSaveFunction::
InstructionSaveFunction(const InstructionSaveFunction& I)
  : InstructionSave(I),
    __function(I.__function)
{
  ;
}

InstructionSaveFunction::
~InstructionSaveFunction()
{
  ;
}

void InstructionSaveFunction::execute()
{
  (*__mesh).execute();
  (*__function).execute();
  (*__fileName).execute();
  Information::instance().setMesh(__mesh);

  const std::string CR = (*__fileDescriptor).cr();

  const Mesh& mesh = static_cast<const Mesh&>(*(*__mesh).mesh());

  switch((*__fileDescriptor).format()) {
  case FileDescriptor::raw: {
    std::ofstream fsol((*__fileName).value());
    if (fsol.bad()) {
      throw ErrorHandler(__FILE__,__LINE__,
			 "cannot open file '"
			 +stringify((*__fileName).value())+"'",
			 ErrorHandler::normal);
    }

    switch ((*(*__function).value()).type()) {
    case FunctionExpression::fem: {
      FunctionExpressionFEM& f
 	= static_cast<FunctionExpressionFEM&>(*(*__function).value());
      if ((*f.mesh()).mesh() == ((*__mesh).mesh())) {
	(f.femFunction()).dump(fsol, CR);
      } // if not continues the standard method
      break;
    }
    default: {
      FunctionExpression& f = (*__function);
      for (size_t i=0; i<mesh.numberOfVertices(); ++i) {
	const TinyVector<3>& X = mesh.vertex(i);
	fsol << f.value(X[0], X[1], X[2]) << CR;
      }
    }
    }
    break;
  }
  case FileDescriptor::medit: {
    //! plots the solution
    std::string sol = (*__fileName).value();
    sol += ".bb";
    std::ofstream fsol(sol.c_str());
    if (not(fsol)) {
      throw ErrorHandler(__FILE__,__LINE__,
			 "cannot open file '"+stringify(sol)+"'",
			 ErrorHandler::normal);
    }

    fsol << "3 1 " << mesh.numberOfVertices() << " 2" << CR;

    switch ((*(*__function).value()).type()) {
    case FunctionExpression::fem: {
      FunctionExpressionFEM& f
 	= static_cast<FunctionExpressionFEM&>(*(*__function).value());
      if ((*f.mesh()).mesh() == ((*__mesh).mesh())) {
	(f.femFunction()).dump(fsol, CR);
	break;
      } // if not continues the standard method
    }
    default: {
      FunctionExpression& f = (*__function);
      for (size_t i=0; i<mesh.numberOfVertices(); ++i) {
	const TinyVector<3>& X = mesh.vertex(i);
	fsol << f.value(X[0], X[1], X[2]) << CR;
      }
    }
    }
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "unexpected file type",
		       ErrorHandler::unexpected);
  }
  }

  //! The mesh is nomore used in this region.
  Information::instance().unsetMesh();
}

void
InstructionAffectation<FunctionExpressionFEM, FunctionVariable>::
execute()
{
  (*__expression).execute();

  ReferenceCounting<FunctionExpression> f = (*__variable).expression();

  FunctionExpressionFEM& femFunction = static_cast<FunctionExpressionFEM&>(*f);
  const UserFunctionLanguage u(__expression);
  femFunction.femFunction() = u;
}


InstructionSaveField::
InstructionSaveField(ReferenceCounting<FileDescriptor> descriptor,
		     ReferenceCounting<StringExpression> fileName,
		     ReferenceCounting<FieldExpression> field,
		     ReferenceCounting<MeshExpression> mesh)
  : InstructionSave(descriptor,fileName,mesh),
    __field(field)
{
  ;
}

InstructionSaveField::
InstructionSaveField(const InstructionSaveField& I)
  : InstructionSave(I),
    __field(I.__field)
{
  ;
}

InstructionSaveField::
~InstructionSaveField()
{
  ;
}

void InstructionSaveField::execute()
{
  (*__mesh).execute();
  (*__field).execute();
  (*__fileName).execute();
  Information::instance().setMesh(__mesh);

  const std::string CR = (*__fileDescriptor).cr();

  const Mesh& mesh = static_cast<const Mesh&>(*(*__mesh).mesh());

  switch((*__fileDescriptor).format()) {
  case FileDescriptor::raw: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "not implemented",
		       ErrorHandler::normal);
  }
  case FileDescriptor::medit: {
    //! plots the solution
    std::string sol = (*__fileName).value();
    sol += ".bb";
    std::ofstream fsol(sol.c_str());
    if (not(fsol)) {
      throw ErrorHandler(__FILE__,__LINE__,
			 "error: cannot open file '"+sol+"'",
			 ErrorHandler::normal);
    }

    fsol << "3 3 " << mesh.numberOfVertices() << " 2" << CR;

    bool f1IsFEMOnThatMesh = false;
    bool f2IsFEMOnThatMesh = false;
    bool f3IsFEMOnThatMesh = false;

    const Vector<real_t>* v1 = 0;
    const Vector<real_t>* v2 = 0;
    const Vector<real_t>* v3 = 0;

    if ((*((*__field).f1()).value()).type() == FunctionExpression::fem) {
      FunctionExpressionFEM& f
	= static_cast<FunctionExpressionFEM&>(*((*__field).f1()).value());
      if ((*f.mesh()).mesh() == ((*__mesh).mesh())) {
	f1IsFEMOnThatMesh = true;
	v1 = &(f.femFunction().values());
      }
    }

    if ((*((*__field).f2()).value()).type() == FunctionExpression::fem) {
      FunctionExpressionFEM& f
	= static_cast<FunctionExpressionFEM&>(*((*__field).f2()).value());
      if ((*f.mesh()).mesh() == ((*__mesh).mesh())) {
	f2IsFEMOnThatMesh = true;
	v2 = &(f.femFunction().values());
      }
    }

    if ((*((*__field).f3()).value()).type() == FunctionExpression::fem) {
      FunctionExpressionFEM& f
	= static_cast<FunctionExpressionFEM&>(*((*__field).f3()).value());
      if ((*f.mesh()).mesh() == ((*__mesh).mesh())) {
	f3IsFEMOnThatMesh = true;
	v3 = &(f.femFunction().values());
      }
    }

    for (size_t i=0; i<mesh.numberOfVertices(); ++i) {
      const TinyVector<3>& X = mesh.vertex(i);
      if (f1IsFEMOnThatMesh) {
	fsol << (*v1)[i] << ' ';
      } else {
	fsol << (*__field).f1().value(X[0], X[1], X[2]) << ' ';
      }
      if (f2IsFEMOnThatMesh) {
	fsol << (*v2)[i] << ' ';
      } else {
	fsol << (*__field).f2().value(X[0], X[1], X[2]) << ' ';
      }
      if (f3IsFEMOnThatMesh) {
	fsol << (*v3)[i] << CR;
      } else {
	fsol << (*__field).f3().value(X[0], X[1], X[2]) << CR;
      }
    }
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "unknown file type",
		       ErrorHandler::unexpected);
  }
  }

  //! The mesh is nomore used in this region.
  Information::instance().unsetMesh();
}

void InstructionPlot::execute()
{
  (*__mesh).execute();

  Mesh& M = (*(*__mesh).mesh());
  VTKDriver d;

  if (__f == 0) {
    d.plot(M);
  } else {
    (*__f).execute();
    ReferenceCounting<UserFunction> u
      = new UserFunctionLanguage((*__f).value());
    d.plot(M, u);
  }
}

InstructionPlot::InstructionPlot(ReferenceCounting<MeshExpression> m,
				 ReferenceCounting<FunctionExpression> f)
  : Instruction(Instruction::plot),
    __mesh(m), 
    __f(f)
{
  ;
}

InstructionPlot::InstructionPlot(const InstructionPlot& I)
  : Instruction(I),
    __mesh(I.__mesh)
{
  ;
}

InstructionPlot::~InstructionPlot()
{
  ;
}

void InstructionUsingScene::execute()
{
  (*__sceneExpression).execute();
  Information::instance().setScene((*__sceneExpression).scene());
}

InstructionUsingScene::InstructionUsingScene(ReferenceCounting<SceneExpression> e)
  : Instruction(Instruction::Using),
    __sceneExpression(e)
{
  ;
}

InstructionUsingScene::InstructionUsingScene(const InstructionUsingScene& I)
  : Instruction(I),
    __sceneExpression(I.__sceneExpression)
{
  ;
}

InstructionUsingScene::~InstructionUsingScene()
{
  ;
}

void InstructionCoarseMesh::execute()
{
  Information::instance().setCoarseMesh(__coarseMesh);
}