27 #include <vtkCPDataDescription.h>
28 #include <vtkCPInputDataDescription.h>
29 #include <vtkCPProcessor.h>
30 #include <vtkCPPythonScriptPipeline.h>
31 #include <vtkDoubleArray.h>
32 #include <vtkFieldData.h>
33 #include <vtkIdTypeArray.h>
34 #include <vtkImageData.h>
35 #include <vtkImageDataStreamer.h>
36 #include <vtkIntArray.h>
38 #include <vtkPointData.h>
39 #include <vtkPoints.h>
40 #include <vtkStringArray.h>
41 #include <vtkUnstructuredGrid.h>
42 #include <vtkXMLImageDataWriter.h>
43 #include <vtkXMLPImageDataWriter.h>
44 #include <vtkXMLPUnstructuredGridWriter.h>
45 #include <vtkXMLUnstructuredGridWriter.h>
53 std::unordered_map<std::string, VtkDiffusionGrid*>
vtk_dgrids_;
63 impl_ = std::unique_ptr<ParaviewAdaptor::ParaviewImpl>(
74 impl_->g_processor_->RemoveAllPipelines();
75 impl_->g_processor_->Finalize();
76 impl_->g_processor_->Delete();
77 impl_->g_processor_ =
nullptr;
79 if (param->export_visualization &&
80 param->visualization_export_generate_pvsm) {
85 if (
impl_->data_description_ !=
nullptr) {
86 impl_->data_description_->Delete();
88 for (
auto& el :
impl_->vtk_agents_) {
91 for (
auto& el :
impl_->vtk_dgrids_) {
105 auto* param = sim->GetParam();
106 uint64_t total_steps = sim->GetScheduler()->GetSimulatedSteps();
107 if (total_steps % param->visualization_interval != 0) {
111 real_t time = param->simulation_time_step * total_steps;
112 impl_->data_description_->SetTimeData(time, total_steps);
116 if (param->insitu_visualization) {
119 if (param->export_visualization) {
127 auto* param = sim->GetParam();
129 if (param->insitu_visualization &&
impl_->g_processor_ ==
nullptr) {
132 "Insitu visualization is currently not supported on MacOS. "
133 "Please use export visualization.");
135 impl_->g_processor_ = vtkCPProcessor::New();
136 impl_->g_processor_->Initialize();
139 if (param->insitu_visualization) {
140 const std::string& script =
144 std::string final_python_script_name =
145 Concat(sim->GetOutputDir(),
"/insitu_pipeline.py");
146 ofs.open(final_python_script_name);
149 vtkNew<vtkCPPythonScriptPipeline> pipeline;
150 pipeline->Initialize(final_python_script_name.c_str());
151 impl_->g_processor_->AddPipeline(pipeline.GetPointer());
154 if (
impl_->data_description_ ==
nullptr) {
155 impl_->data_description_ = vtkCPDataDescription::New();
157 impl_->data_description_->Delete();
158 impl_->data_description_ = vtkCPDataDescription::New();
160 impl_->data_description_->SetTimeData(0, 0);
162 for (
auto& pair : param->visualize_agents) {
163 impl_->vtk_agents_[pair.first.c_str()] =
166 for (
auto& entry : param->visualize_diffusion) {
167 impl_->vtk_dgrids_[entry.name] =
174 #ifdef BDM_PV_EXPERIMENTAL
175 vtkCommand::kOpenGLCacheEnable =
true;
176 vtkCommand::kOpenGLCacheIndex = 0;
177 #endif // BDM_PV_EXPERIMENTAL
178 impl_->g_processor_->RequestDataDescription(
impl_->data_description_);
179 impl_->data_description_->ForceOutputOn();
180 impl_->g_processor_->CoProcess(
impl_->data_description_);
187 auto step =
impl_->data_description_->GetTimeStep();
189 for (
auto& el :
impl_->vtk_agents_) {
190 el.second->WriteToFile(step);
193 for (
auto& el :
impl_->vtk_dgrids_) {
194 el.second->WriteToFile(step);
202 if (
impl_->data_description_->GetUserData() ==
nullptr) {
203 vtkNew<vtkStringArray> json;
204 json->SetName(
"metadata");
205 json->InsertNextValue(
207 vtkNew<vtkFieldData> field;
208 field->AddArray(json);
209 impl_->data_description_->SetUserData(field);
216 for (
auto& pair :
impl_->vtk_agents_) {
218 pair.second->Update(&agents);
228 if (it !=
impl_->vtk_dgrids_.end()) {
229 it->second->Update(grid);
240 ofstr.open(
Concat(sim->GetOutputDir(),
"/", kSimulationInfoJson));
253 std::stringstream python_cmd;
254 std::string pv_dir = std::getenv(
"ParaView_DIR");
255 std::string bdmsys = std::getenv(
"BDMSYS");
257 python_cmd << pv_dir <<
"/bin/pvbatch " << bdmsys
258 <<
"/include/core/visualization/paraview/generate_pv_state.py "
259 << sim->GetOutputDir() <<
"/" << kSimulationInfoJson;
260 int ret_code = system(python_cmd.str().c_str());
262 Log::Fatal(
"ParaviewAdaptor::GenerateParaviewState",
263 "Error during generation of ParaView state\n",
"Command\n",
270 const std::string& python_script) {
271 std::stringstream script;
274 ifs.open(python_script, std::ifstream::in);
275 if (!ifs.is_open()) {
276 Log::Fatal(
"ParaviewAdaptor::BuildPythonScriptString",
277 Concat(
"Python script (", python_script,
278 ") was not found or could not be opened."));
280 script << ifs.rdbuf();
283 std::string default_python_script =
284 std::string(std::getenv(
"BDMSYS")) +
286 "/include/core/visualization/paraview/default_insitu_pipeline.py");
288 std::ifstream ifs_default;
289 ifs_default.open(default_python_script, std::ifstream::in);
290 if (!ifs_default.is_open()) {
291 Log::Fatal(
"ParaviewAdaptor::BuildPythonScriptString",
292 Concat(
"Python script (", default_python_script,
293 ") was not found or could not be opened."));
295 script << std::endl << ifs_default.rdbuf();
319 const std::string& python_script) {}