Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "Albany_Catalyst_Adapter.hpp"
00008
00009 #include "Albany_Catalyst_EpetraDataArray.hpp"
00010 #include "Albany_Catalyst_Grid.hpp"
00011
00012 #include "Teuchos_Array.hpp"
00013 #include "Teuchos_TestForException.hpp"
00014
00015 #include <vtkClientServerInterpreter.h>
00016 #include <vtkClientServerInterpreterInitializer.h>
00017 #include <vtkCPDataDescription.h>
00018 #include <vtkCPInputDataDescription.h>
00019 #include <vtkCPProcessor.h>
00020 #include <vtkCPPythonScriptPipeline.h>
00021 #include <vtkNew.h>
00022 #include <vtkPointData.h>
00023 #include <vtkPVInstantiator.h>
00024
00025 #include <iostream>
00026
00027 namespace Albany {
00028 namespace Catalyst {
00029
00030 Adapter * Adapter::instance = NULL;
00031
00032 class Adapter::Private
00033 {
00034 public:
00035
00036 static vtkObjectBase* MakeGrid() { return Grid::New(); }
00037 Private() { processor->Initialize(); }
00038 ~Private() { processor->Finalize(); }
00039 vtkNew<vtkCPProcessor> processor;
00040 };
00041
00042 Adapter::Adapter()
00043 : d(new Private)
00044 {
00045 }
00046
00047 Adapter::~Adapter()
00048 {
00049 delete d;
00050 }
00051
00052 Adapter *
00053 Adapter::initialize(const Teuchos::RCP<Teuchos::ParameterList>& catalystParams)
00054 {
00055
00056 catalystParams->validateParameters(*getValidAdapterParameters(),0);
00057
00058 if (Adapter::instance)
00059 delete Adapter::instance;
00060 Adapter::instance = new Adapter();
00061
00062
00063 if (vtkClientServerInterpreterInitializer *intInit =
00064 vtkClientServerInterpreterInitializer::GetInitializer()) {
00065 if (vtkClientServerInterpreter *interp = intInit->GetGlobalInterpreter()) {
00066 interp->AddNewInstanceFunction("Grid", Private::MakeGrid);
00067 }
00068 }
00069
00070
00071 Teuchos::Array<std::string> files =
00072 catalystParams->get<Teuchos::Array<std::string> >("Pipeline Files");
00073 typedef Teuchos::Array<std::string>::const_iterator FileIterT;
00074 for (FileIterT it = files.begin(), itEnd = files.end(); it != itEnd; ++it)
00075 Adapter::instance->addPythonScriptPipeline(*it);
00076
00077 return Adapter::instance;
00078 }
00079
00080 Adapter * Adapter::get()
00081 {
00082 TEUCHOS_TEST_FOR_EXCEPTION(!Adapter::instance, std::runtime_error,
00083 "Albany::Catalyst::Adapter::get() called before "
00084 "initialize()!" << std::endl);
00085 return Adapter::instance;
00086 }
00087
00088 void Adapter::cleanup()
00089 {
00090 delete Adapter::instance;
00091 Adapter::instance = NULL;
00092 }
00093
00094 bool Adapter::addPythonScriptPipeline(const std::string &filename)
00095 {
00096 vtkNew<vtkCPPythonScriptPipeline> pipeline;
00097 return pipeline->Initialize(filename.c_str()) != 0 &&
00098 this->addPipeline(pipeline.GetPointer());
00099 }
00100
00101 bool Adapter::addPipeline(vtkCPPipeline *pipeline)
00102 {
00103 return d->processor->AddPipeline(pipeline) != 0;
00104 }
00105
00106 void Adapter::update(int timeStep, double time, Decorator &decorator,
00107 const Epetra_Vector &soln)
00108 {
00109 vtkNew<vtkCPDataDescription> desc;
00110 desc->AddInput("input");
00111 desc->SetTimeData(time, timeStep);
00112 if (d->processor->RequestDataDescription(desc.GetPointer())) {
00113 typedef vtkSmartPointer<vtkUnstructuredGridBase> GridRCP;
00114 GridRCP grid = GridRCP::Take(decorator.newVtkUnstructuredGrid());
00115
00116 vtkNew<EpetraDataArray> pointScalars;
00117 pointScalars->SetEpetraVector(soln);
00118 pointScalars->SetName("Scalars_");
00119 grid->GetPointData()->SetScalars(pointScalars.GetPointer());
00120
00121 desc->GetInputDescriptionByName("input")->SetGrid(grid);
00122
00123 d->processor->CoProcess(desc.GetPointer());
00124 }
00125 }
00126
00127 Teuchos::RCP<const Teuchos::ParameterList>
00128 Adapter::getValidAdapterParameters()
00129 {
00130 Teuchos::RCP<Teuchos::ParameterList> validPL =
00131 rcp(new Teuchos::ParameterList("ValidCatalystAdapterParams"));
00132
00133 validPL->set<bool>("Interface Activated", false,
00134 "Activates Catalyst if set to true");
00135 validPL->set<Teuchos::Array<std::string> >(
00136 "Pipeline Files", Teuchos::Array<std::string>(),
00137 "Filenames that contains Catalyst pipeline commands.");
00138
00139 return validPL;
00140 }
00141
00142 }
00143 }