00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <cstdlib>
00023 #include <fstream>
00024
00025 #include <Mesh.hpp>
00026
00027 #include <Structured3DMesh.hpp>
00028
00029 #include <Instruction.hpp>
00030 #include <FunctionExpression.hpp>
00031
00032 #include <FunctionExpressionFEM.hpp>
00033 #include <FunctionExpressionValue.hpp>
00034
00035 #include <FEMFunctionBuilder.hpp>
00036
00037 #include <StringExpression.hpp>
00038
00039 #include <Scene.hpp>
00040
00041 #include <MeshExpression.hpp>
00042 #include <Variable.hpp>
00043
00044 #include <Information.hpp>
00045
00046 #include <VTKDriver.hpp>
00047
00048 #include <FEMFunction.hpp>
00049 #include <SpectralFunction.hpp>
00050
00051 #include <ErrorHandler.hpp>
00052
00053 #include <WriterMedit.hpp>
00054 #include <WriterRaw.hpp>
00055 #include <WriterVTK.hpp>
00056
00057 void InstructionExec::execute()
00058 {
00059 __command->execute();
00060 std::string command = __command->value();
00061 ffout(2) << "executing: " << command << '\n';
00062 #ifdef ALLOW_EXEC
00063 int result = system(command.c_str());
00064 if (result == -1) {
00065 throw ErrorHandler(__FILE__,__LINE__,
00066 "could not execute: "+command,
00067 ErrorHandler::normal);
00068 }
00069 #else // ALLOW_EXEC
00070 fferr(2) << "warning: this version was not compiled with 'exec' support.\n";
00071 #endif // ALLOW_EXEC
00072 }
00073
00074 InstructionExec::InstructionExec(ReferenceCounting<StringExpression> command)
00075 : Instruction(Instruction::exec),
00076 __command(command)
00077 {
00078 ;
00079 }
00080
00081 InstructionExec::InstructionExec(const InstructionExec& I)
00082 : Instruction(I),
00083 __command(I.__command)
00084 {
00085 ;
00086 }
00087
00088 InstructionExec:: ~InstructionExec()
00089 {
00090 ;
00091 }
00092
00093
00094 void InstructionCat::execute()
00095 {
00096 __filename->execute();
00097
00098 const std::string filename = __filename->value();
00099 std::ifstream fin(filename.c_str());
00100
00101 if (not fin) {
00102 throw ErrorHandler(__FILE__,__LINE__,
00103 "could not open file \""+filename+"\"",
00104 ErrorHandler::normal);
00105 }
00106 char c = fin.get();
00107 while (not(fin.eof())) {
00108 ffout(0) << c;
00109 c = fin.get();
00110 }
00111 }
00112
00113 InstructionCat::
00114 InstructionCat(ReferenceCounting<StringExpression> filename)
00115 : Instruction(Instruction::cat),
00116 __filename(filename)
00117 {
00118 ;
00119 }
00120
00121 InstructionCat::
00122 InstructionCat(const InstructionCat& I)
00123 : Instruction(I),
00124 __filename(I.__filename)
00125 {
00126 ;
00127 }
00128
00129 InstructionCat::
00130 ~InstructionCat()
00131 {
00132 ;
00133 }
00134
00135
00136 InstructionSave::
00137 InstructionSave(ReferenceCounting<FileDescriptor> descriptor,
00138 ReferenceCounting<StringExpression> fileName,
00139 ReferenceCounting<MeshExpression> mesh)
00140 : Instruction(Instruction::save),
00141 __fileDescriptor(descriptor),
00142 __fileName(fileName),
00143 __mesh(mesh)
00144 {
00145 ;
00146 }
00147
00148 InstructionSave::InstructionSave(const InstructionSave& I)
00149 : Instruction(I),
00150 __fileDescriptor(I.__fileDescriptor),
00151 __fileName(I.__fileName),
00152 __mesh(I.__mesh)
00153 {
00154 ;
00155 }
00156
00157 InstructionSave::~InstructionSave()
00158 {
00159 ;
00160 }
00161
00162 InstructionSaveMesh::
00163 InstructionSaveMesh(ReferenceCounting<FileDescriptor> descriptor,
00164 ReferenceCounting<StringExpression> fileName,
00165 ReferenceCounting<MeshExpression> mesh)
00166 : InstructionSave(descriptor, fileName, mesh)
00167 {
00168 ;
00169 }
00170
00171 InstructionSaveMesh::
00172 InstructionSaveMesh(const InstructionSaveMesh& I)
00173 : InstructionSave(I)
00174 {
00175 ;
00176 }
00177
00178 InstructionSaveMesh::
00179 ~InstructionSaveMesh()
00180 {
00181 ;
00182 }
00183
00184 void InstructionSaveMesh::execute()
00185 {
00186 __mesh->execute();
00187 __fileName->execute();
00188
00189 Information::instance().setMesh(__mesh->mesh());
00190
00191 ReferenceCounting<WriterBase> writer;
00192
00193 switch(__fileDescriptor->format()) {
00194 case FileDescriptor::medit: {
00195 writer = new WriterMedit(__mesh->mesh(),
00196 __fileName->value(),
00197 *__fileDescriptor);
00198 break;
00199 }
00200 case FileDescriptor::raw: {
00201 writer = new WriterRaw(__mesh->mesh(),
00202 __fileName->value(),
00203 *__fileDescriptor);
00204 break;
00205 }
00206 case FileDescriptor::vtk: {
00207 writer = new WriterVTK(__mesh->mesh(),
00208 __fileName->value(),
00209 *__fileDescriptor);
00210 break;
00211 }
00212 default: {
00213 throw ErrorHandler(__FILE__,__LINE__,
00214 "file type \""+__fileDescriptor->formatName()+'\"',
00215 ErrorHandler::unexpected);
00216 }
00217 }
00218
00219 writer->proceed();
00220
00222 Information::instance().unsetMesh();
00223 }
00224
00225 void
00226 InstructionAffectation<FunctionExpression, FunctionVariable>::
00227 execute()
00228 {
00229 FunctionVariable* functionVariable
00230 = VariableRepository::instance().findVariable<FunctionVariable>(__variableName);
00231
00232 ConstReferenceCounting<FunctionExpression> f = functionVariable->expression();
00233
00234 const ScalarFunctionBase& scalarFunction = *f->function();
00235
00236 switch(scalarFunction.type()) {
00237 case ScalarFunctionBase::femfunction: {
00238 const FEMFunctionBase& femFunction = dynamic_cast<const FEMFunctionBase&>(scalarFunction);
00239
00240 Information::instance().setMesh(femFunction.baseMesh());
00241
00242 __expression->execute();
00243
00244 FEMFunctionBuilder builder;
00245 builder.build(femFunction.discretizationType(),
00246 femFunction.baseMesh(),
00247 *__expression->function());
00248
00249 (*functionVariable)
00250 = new FunctionExpressionValue(builder.getBuiltScalarFunction(), false);
00251
00252 Information::instance().unsetMesh();
00253 break;
00254 }
00255 case ScalarFunctionBase::spectral: {
00256 const SpectralFunction& spectralFunction
00257 = dynamic_cast<const SpectralFunction&>(scalarFunction);
00258 const Mesh* mesh = spectralFunction.mesh();
00259 Information::instance().setMesh(mesh);
00260
00261 __expression->execute();
00262
00263 (*functionVariable)
00264 = new FunctionExpressionValue(new SpectralFunction(spectralFunction.mesh(),
00265 *__expression->function()),
00266 false);
00267 Information::instance().unsetMesh();
00268 break;
00269 }
00270 default: {
00271 __expression->execute();
00272 (*functionVariable) = __expression->value();
00273 }
00274 }
00275 }
00276
00277
00278 InstructionSaveFieldList::
00279 InstructionSaveFieldList(ReferenceCounting<FileDescriptor> descriptor,
00280 ReferenceCounting<StringExpression> fileName,
00281 ReferenceCounting<FieldExpressionList> fieldList,
00282 ReferenceCounting<MeshExpression> mesh)
00283 : InstructionSave(descriptor,fileName,mesh),
00284 __fieldList(fieldList)
00285 {
00286 ;
00287 }
00288
00289 InstructionSaveFieldList::
00290 InstructionSaveFieldList(const InstructionSaveFieldList& I)
00291 : InstructionSave(I),
00292 __fieldList(I.__fieldList)
00293 {
00294 ;
00295 }
00296
00297 InstructionSaveFieldList::
00298 ~InstructionSaveFieldList()
00299 {
00300 ;
00301 }
00302
00303 void InstructionSaveFieldList::
00304 execute()
00305 {
00306 __mesh->execute();
00307 __fieldList->execute();
00308 __fileName->execute();
00309
00310 Information::instance().setMesh(__mesh->mesh());
00311
00312 const std::string CR = __fileDescriptor->cr();
00313
00314 ReferenceCounting<WriterBase> writer;
00315
00316 switch(__fileDescriptor->format()) {
00317 case FileDescriptor::medit: {
00318 writer = new WriterMedit(__mesh->mesh(),
00319 __fileName->value(),
00320 *__fileDescriptor);
00321 break;
00322 }
00323 case FileDescriptor::raw: {
00324 writer = new WriterRaw(__mesh->mesh(),
00325 __fileName->value(),
00326 *__fileDescriptor);
00327 break;
00328 }
00329 case FileDescriptor::vtk: {
00330 writer = new WriterVTK(__mesh->mesh(),
00331 __fileName->value(),
00332 *__fileDescriptor);
00333 break;
00334 }
00335 default: {
00336 throw ErrorHandler(__FILE__,__LINE__,
00337 "file type \""+__fileDescriptor->formatName()+'\"',
00338 ErrorHandler::unexpected);
00339 }
00340 }
00341
00342 for (size_t iList = 0; iList<__fieldList->numberOfFields(); ++iList) {
00343 ReferenceCounting<FieldExpression> field = __fieldList->field(iList);
00344
00345 switch(field->numberOfComponents()) {
00346 case 0: {
00347 break;
00348 }
00349 case 1: {
00350 writer->add(field->field()->function(0));
00351 break;
00352 }
00353 default: {
00354 writer->add(field->field());
00355 }
00356 }
00357 }
00358
00359 writer->proceed();
00360
00362 Information::instance().unsetMesh();
00363 }
00364
00365 void InstructionPlot::execute()
00366 {
00367 __mesh->execute();
00368
00369 const Mesh& M = *(__mesh->mesh());
00370 VTKDriver d;
00371
00372 if (__f == 0) {
00373 d.plot(M);
00374 } else {
00375 __f->execute();
00376 ConstReferenceCounting<ScalarFunctionBase> u
00377 = __f->value()->function();
00378 d.plot(M, u);
00379 }
00380 }
00381
00382 InstructionPlot::InstructionPlot(ReferenceCounting<MeshExpression> m,
00383 ReferenceCounting<FunctionExpression> f)
00384 : Instruction(Instruction::plot),
00385 __mesh(m),
00386 __f(f)
00387 {
00388 ;
00389 }
00390
00391 InstructionPlot::InstructionPlot(const InstructionPlot& I)
00392 : Instruction(I),
00393 __mesh(I.__mesh)
00394 {
00395 ;
00396 }
00397
00398 InstructionPlot::~InstructionPlot()
00399 {
00400 ;
00401 }
00402
00403 void InstructionUsingScene::execute()
00404 {
00405 __sceneExpression->execute();
00406 Information::instance().setScene(__sceneExpression->scene());
00407 }
00408
00409 InstructionUsingScene::InstructionUsingScene(ReferenceCounting<SceneExpression> e)
00410 : Instruction(Instruction::Using),
00411 __sceneExpression(e)
00412 {
00413 ;
00414 }
00415
00416 InstructionUsingScene::InstructionUsingScene(const InstructionUsingScene& I)
00417 : Instruction(I),
00418 __sceneExpression(I.__sceneExpression)
00419 {
00420 ;
00421 }
00422
00423 InstructionUsingScene::~InstructionUsingScene()
00424 {
00425 ;
00426 }
00427
00428 void InstructionCoarseMesh::execute()
00429 {
00430 Information::instance().setCoarseMesh(__coarseMesh);
00431 }
00432
00433 void InstructionBlockBegin::execute()
00434 {
00435 VariableRepository::instance().beginBlock();
00436 }
00437
00438 void InstructionBlockEnd::execute()
00439 {
00440 VariableRepository::instance().endBlock();
00441 }
00442