From 99855a1605b2f48211e87bb552bf474a8c7e65b6 Mon Sep 17 00:00:00 2001 From: Bernd Hahnebach Date: Sun, 30 Sep 2018 21:42:32 +0200 Subject: [PATCH] FEM: vtk, export FC result, get rid of needless result types - an result is an result, we just gone export what we have - a vtk result can consists of all kind of result types --- src/Mod/Fem/App/FemPostPipeline.cpp | 7 +- src/Mod/Fem/App/FemVTKTools.cpp | 142 +++++++++++----------------- src/Mod/Fem/App/FemVTKTools.h | 4 +- 3 files changed, 58 insertions(+), 95 deletions(-) diff --git a/src/Mod/Fem/App/FemPostPipeline.cpp b/src/Mod/Fem/App/FemPostPipeline.cpp index ab2b3013c7..22462f5705 100644 --- a/src/Mod/Fem/App/FemPostPipeline.cpp +++ b/src/Mod/Fem/App/FemPostPipeline.cpp @@ -259,12 +259,7 @@ void FemPostPipeline::load(FemResultObject* res) { //Now copy the point data over //############################ - if(res->getPropertyByName("Velocity")){ // TODO: consider better way to detect result type, res->Type == "CfdResult" - FemVTKTools::exportFluidicResult(res, grid); - } - else{ - FemVTKTools::exportMechanicalResult(res, grid); - } + FemVTKTools::exportFreeCADResult(res, grid); Data.setValue(grid); } diff --git a/src/Mod/Fem/App/FemVTKTools.cpp b/src/Mod/Fem/App/FemVTKTools.cpp index 17a9fbc319..e7037fc83f 100644 --- a/src/Mod/Fem/App/FemVTKTools.cpp +++ b/src/Mod/Fem/App/FemVTKTools.cpp @@ -636,26 +636,19 @@ void FemVTKTools::writeResult(const char* filename, const App::DocumentObject* r } Base::TimeInfo Start; - Base::Console().Message("Start: write FemResult or CfdResult to VTK unstructuredGrid dataset =======\n"); + Base::Console().Message("Start: write FemResult to VTK unstructuredGrid dataset =======\n"); Base::FileInfo f(filename); + // mesh vtkSmartPointer grid = vtkSmartPointer::New(); App::DocumentObject* mesh = static_cast(res->getPropertyByName("Mesh"))->getValue(); const FemMesh& fmesh = static_cast(mesh->getPropertyByName("FemMesh"))->getValue(); FemVTKTools::exportVTKMesh(&fmesh, grid); - Base::Console().Message(" %f: vtk mesh builder finisched\n",Base::TimeInfo::diffTimeF(Start, Base::TimeInfo())); + Base::Console().Message(" %f: vtk mesh builder finished\n",Base::TimeInfo::diffTimeF(Start, Base::TimeInfo())); - if(res->getPropertyByName("Velocity")){ // consider better way to detect result type, res->Type == "CfdResult" - FemVTKTools::exportFluidicResult(res, grid); - } - else if(res->getPropertyByName("DisplacementVectors")){ - FemVTKTools::exportMechanicalResult(res, grid); - } - else{ - Base::Console().Error("Result type can not be detected from unique property name like Velocity or DisplacementVectors\n"); - return; - } + // result + FemVTKTools::exportFreeCADResult(res, grid); //vtkSmartPointer dataset = vtkDataSet::SafeDownCast(grid); if(f.hasExtension("vtu")){ @@ -797,68 +790,64 @@ void _importResult(const vtkSmartPointer dataset, App::DocumentObjec } void _exportResult(const App::DocumentObject* result, vtkSmartPointer grid, - const std::map& vectors, const std::map scalers, - const std::string& essential_property){ + const std::map& vectors, const std::map scalers){ const Fem::FemResultObject* res = static_cast(result); - const vtkIdType nPoints = grid->GetNumberOfPoints(); + + // vectors for (auto const& kv: vectors) { - const int dim = 3; //Fixme, detect dim + const int dim=3; //Fixme, detect dim App::PropertyVectorList* field = nullptr; if (res->getPropertyByName(kv.first.c_str())) field = static_cast(res->getPropertyByName(kv.first.c_str())); else Base::Console().Error("PropertyVectorList %s not found \n", kv.first.c_str()); - if(field && field->getValues().size()>1) { // FreeCAD property list + if (field && field->getSize() > 0) { + if (nPoints != field->getSize()) + Base::Console().Error("Size of PropertyVectorList = %d, not equal to vtk mesh node count %d \n", field->getSize(), nPoints); const std::vector& vel = field->getValues(); vtkSmartPointer data = vtkSmartPointer::New(); - if(nPoints != field->getSize()) - Base::Console().Error("PropertyVectorList->getSize() = %d, not equal to vtk mesh node count %d \n", field->getSize(), nPoints); data->SetNumberOfComponents(dim); data->SetNumberOfTuples(vel.size()); data->SetName(kv.second.c_str()); // kv.first may be a better name, without space vtkIdType i=0; - if(kv.first == essential_property) { - for(std::vector::const_iterator it=vel.begin(); it!=vel.end(); ++it) { - Base::Vector3d v = vel.at(i); - double tuple[] = {v.x, v.y, v.z}; - //double tuple[] = {it->x, it->y, it->z}; - data->SetTuple(i, tuple); - ++i; - } - } - else{ - for(std::vector::const_iterator it=vel.begin(); it!=vel.end(); ++it) { - double tuple[] = {it->x, it->y, it->z}; - data->SetTuple(i, tuple); - ++i; - } + for (std::vector::const_iterator it=vel.begin(); it!=vel.end(); ++it) { + double tuple[] = {it->x, it->y, it->z}; + data->SetTuple(i, tuple); + ++i; } grid->GetPointData()->AddArray(data); - Base::Console().Log(" Info: PropertyVectorList %s exported as vtk array name '%s'\n", kv.first.c_str(), kv.second.c_str()); + Base::Console().Message(" Info: PropertyVectorList %s exported as vtk array name '%s'\n", kv.first.c_str(), kv.second.c_str()); } else - Base::Console().Error("field = static_cast failed or empty for field: %s", kv.first.c_str()); + Base::Console().Message(" Info: PropertyVectorList %s NOT exported to vtk, because size is: %i\n", kv.first.c_str(), field->getSize()); } + // scalars for (auto const& kv: scalers) { App::PropertyFloatList* field = nullptr; if (res->getPropertyByName(kv.first.c_str())) field = static_cast(res->getPropertyByName(kv.first.c_str())); - if(field && field->getValues().size()>1) { + else + Base::Console().Error("PropertyFloatList %s not found \n", kv.first.c_str()); + if (field && field->getSize() > 0) { + if (nPoints != field->getSize()) + Base::Console().Error("Size of PropertyFloatList = %d, not equal to vtk mesh node count %d \n", field->getSize(), nPoints); const std::vector& vec = field->getValues(); vtkSmartPointer data = vtkSmartPointer::New(); data->SetNumberOfValues(vec.size()); data->SetName(kv.second.c_str()); - for(size_t i=0; iSetValue(i, vec[i]); grid->GetPointData()->AddArray(data); - Base::Console().Log(" Info: PropertyFloatList %s exported as vtk array name '%s'\n", kv.first.c_str(), kv.second.c_str()); + Base::Console().Message(" Info: PropertyFloatList %s exported as vtk array name '%s'\n", kv.first.c_str(), kv.second.c_str()); } + else + Base::Console().Message(" Info: PropertyFloatList %s NOT exported to vtk, because size is: %i\n", kv.first.c_str(), field->getSize()); } } @@ -896,28 +885,6 @@ void FemVTKTools::importFluidicResult(vtkSmartPointer dataset, App:: } -void FemVTKTools::exportFluidicResult(const App::DocumentObject* res, vtkSmartPointer grid) { - // velocity and pressure are essential, Temperature is optional, so are turbulence related variables - static std::map cfd_vectors; // vector field defined in openfoam -> property defined in CfdResult.py - cfd_vectors["Velocity"] = "U"; - - static std::map cfd_scalers; // variable name defined in openfoam -> property defined in CfdResult.py - cfd_scalers["Pressure"] = "p"; - cfd_scalers["Temperature"] = "T"; - cfd_scalers["TurbulenceEnergy"] = "k"; - cfd_scalers["TurbulenceViscosity"] = "nut"; - cfd_scalers["TurbulenceDissipationRate"] = "epsilon"; - cfd_scalers["TurbulenceSpecificDissipation"] = "omega"; - cfd_scalers["TurbulenceThermalDiffusivity"] = "alphat"; - - std::string essential_property = std::string("Velocity"); - - if(!res->getPropertyByName("Velocity")){ - Base::Console().Error("essential field like `velocity` is not found in CfdResult\n"); - return; - } - _exportResult(res, grid, cfd_vectors, cfd_scalers, essential_property); -} void FemVTKTools::importMechanicalResult(vtkSmartPointer dataset, App::DocumentObject* res) { @@ -959,33 +926,36 @@ void FemVTKTools::importMechanicalResult(vtkSmartPointer dataset, Ap } -void FemVTKTools::exportMechanicalResult(const App::DocumentObject* res, vtkSmartPointer grid) { - Base::Console().Message("Start: Create VTK result data from FreeCAD mechanical result data ======================\n"); - if(!res->getPropertyByName("DisplacementVectors")){ - Base::Console().Error("essential field like `DisplacementVectors` is not found in this Result object\n"); - return; - } - std::map vectors; // property defined in MechanicalResult.py -> variable name in vtk - vectors["DisplacementVectors"] = "Displacement"; - vectors["StrainVectors"] = "Strain vectors"; - vectors["StressVectors"] = "Stress vectors"; - std::map scalers; // App::FloatListProperty name -> vtk name - scalers["UserDefined"] = "User Defined Results"; - scalers["Temperature"] = "Temperature"; - scalers["PrincipalMax"] = "Maximum Principal stress"; - scalers["PrincipalMed"] = "Median Principal stress"; - scalers["PrincipalMin"] = "Minimum Principal stress"; - scalers["MaxShear"] = "Max shear stress (Tresca)"; - scalers["StressValues"] = "Von Mises stress"; - scalers["MassFlowRate"] = "Mass Flow Rate"; - scalers["NetworkPressure"] = "Network Pressure"; - scalers["Peeq"] = "Peeq"; - //scalers["DisplacementLengths"] = ""; // not yet exported in exportMechanicalResult() +void FemVTKTools::exportFreeCADResult(const App::DocumentObject* res, vtkSmartPointer grid) { + Base::Console().Message("Start: Create VTK result data from FreeCAD result data.\n"); - std::string essential_property = std::string("DisplacementVectors"); - _exportResult(res, grid, vectors, scalers, essential_property); - Base::Console().Message("End: Create VTK result data from FreeCAD mechanical result data ======================\n"); + // see src/Mod/Fem/femobjects/_FemResultMechanical + + // App::PropertyVectorList will be a list of vectors in vtk + std::map vectors; + vectors["DisplacementVectors"] = "DisplacementVectors"; + vectors["StrainVectors"] = "StrainVectors"; + vectors["StressVectors"] = "StrainVectors"; + + // App::PropertyFloatList will be a list of scalars in vtk + std::map scalers; + scalers["Peeq"] = "Peeq"; + scalers["DisplacementLengths"] = "DisplacementLengths"; + scalers["StressValues"] = "StressValues"; + scalers["PrincipalMax"] = "PrincipalMax"; + scalers["PrincipalMed"] = "PrincipalMed"; + scalers["PrincipalMin"] = "PrincipalMin"; + scalers["MaxShear"] = "MaxShear"; + scalers["MassFlowRate"] = "MassFlowRate"; + scalers["NetworkPressure"] = "Network Pressure"; + scalers["UserDefined"] = "UserDefined"; + scalers["Temperature"] = "Temperature"; + + _exportResult(res, grid, vectors, scalers); + + Base::Console().Message("End: Create VTK result data from FreeCAD result data.\n"); + } } // namespace diff --git a/src/Mod/Fem/App/FemVTKTools.h b/src/Mod/Fem/App/FemVTKTools.h index b9ccc6e082..22e2bc38f2 100644 --- a/src/Mod/Fem/App/FemVTKTools.h +++ b/src/Mod/Fem/App/FemVTKTools.h @@ -69,9 +69,7 @@ namespace Fem /*! * FemResult export to vtkUnstructuredGrid object */ - static void exportFluidicResult(const App::DocumentObject* res, vtkSmartPointer grid); - static void exportMechanicalResult(const App::DocumentObject* res, vtkSmartPointer grid); - + static void exportFreeCADResult(const App::DocumentObject* res, vtkSmartPointer grid); /*! * FemResult (activeObject or created if res= NULL) read from vtkUnstructuredGrid dataset file */