FEM: Port elmer transient analysis to multiframe results
This commit is contained in:
committed by
Benjamin Nauck
parent
c815612dc6
commit
22c8d389d4
@@ -260,41 +260,84 @@ bool FemPostPipeline::canRead(Base::FileInfo File)
|
||||
return File.hasExtension({"vtk", "vtp", "vts", "vtr", "vti", "vtu", "pvtu", "vtm"});
|
||||
}
|
||||
|
||||
void FemPostPipeline::read(Base::FileInfo File)
|
||||
vtkSmartPointer<vtkDataObject> FemPostPipeline::dataObjectFromFile(Base::FileInfo File)
|
||||
{
|
||||
|
||||
// checking on the file
|
||||
if (!File.isReadable()) {
|
||||
throw Base::FileException("File to load not existing or not readable", File);
|
||||
}
|
||||
|
||||
if (File.hasExtension("vtu")) {
|
||||
readXMLFile<vtkXMLUnstructuredGridReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLUnstructuredGridReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("pvtu")) {
|
||||
readXMLFile<vtkXMLPUnstructuredGridReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLPUnstructuredGridReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("vtp")) {
|
||||
readXMLFile<vtkXMLPolyDataReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLPolyDataReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("vts")) {
|
||||
readXMLFile<vtkXMLStructuredGridReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLStructuredGridReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("vtr")) {
|
||||
readXMLFile<vtkXMLRectilinearGridReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLRectilinearGridReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("vti")) {
|
||||
readXMLFile<vtkXMLImageDataReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLImageDataReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("vtk")) {
|
||||
readXMLFile<vtkDataSetReader>(File.filePath());
|
||||
return readXMLFile<vtkDataSetReader>(File.filePath());
|
||||
}
|
||||
else if (File.hasExtension("vtm")) {
|
||||
readXMLFile<vtkXMLMultiBlockDataReader>(File.filePath());
|
||||
return readXMLFile<vtkXMLMultiBlockDataReader>(File.filePath());
|
||||
}
|
||||
else {
|
||||
throw Base::FileException("Unknown extension");
|
||||
|
||||
throw Base::FileException("Unknown extension");
|
||||
}
|
||||
|
||||
void FemPostPipeline::read(Base::FileInfo File)
|
||||
{
|
||||
Data.setValue(dataObjectFromFile(File));
|
||||
}
|
||||
|
||||
void FemPostPipeline::read(std::vector<Base::FileInfo>& files, std::vector<double>& values, Base::Unit unit, std::string& frame_type)
|
||||
{
|
||||
if (files.size() != values.size()) {
|
||||
Base::Console().Error("Result files and frame values have different length.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// setup the time information for the multiblock
|
||||
vtkStringArray* TimeInfo = vtkStringArray::New();
|
||||
TimeInfo->SetName("TimeInfo");
|
||||
TimeInfo->InsertNextValue(frame_type);
|
||||
TimeInfo->InsertNextValue(unit.getString());
|
||||
|
||||
auto multiblock = vtkSmartPointer<vtkMultiBlockDataSet>::New();
|
||||
for (ulong i = 0; i < files.size(); i++) {
|
||||
|
||||
|
||||
// add time information
|
||||
vtkFloatArray* TimeValue = vtkFloatArray::New();
|
||||
TimeValue->SetNumberOfComponents(1);
|
||||
TimeValue->SetName("TimeValue");
|
||||
TimeValue->InsertNextValue(values[i]);
|
||||
|
||||
// checking on the file
|
||||
auto File = files[i];
|
||||
if (!File.isReadable()) {
|
||||
throw Base::FileException("File to load not existing or not readable", File);
|
||||
}
|
||||
|
||||
auto data = dataObjectFromFile(File);
|
||||
data->GetFieldData()->AddArray(TimeValue);
|
||||
data->GetFieldData()->AddArray(TimeInfo);
|
||||
|
||||
multiblock->SetBlock(i, data);
|
||||
}
|
||||
|
||||
multiblock->GetFieldData()->AddArray(TimeInfo);
|
||||
Data.setValue(multiblock);
|
||||
}
|
||||
|
||||
void FemPostPipeline::scale(double s)
|
||||
|
||||
@@ -87,9 +87,10 @@ public:
|
||||
return "FemGui::ViewProviderFemPostPipeline";
|
||||
}
|
||||
|
||||
// load data from files
|
||||
// load data from files (single or as multiframe)
|
||||
static bool canRead(Base::FileInfo file);
|
||||
void read(Base::FileInfo file);
|
||||
void read(std::vector<Base::FileInfo>& files, std::vector<double>& values, Base::Unit unit, std::string& frame_type);
|
||||
void scale(double s);
|
||||
|
||||
// load from results
|
||||
@@ -126,14 +127,15 @@ private:
|
||||
|
||||
|
||||
template<class TReader>
|
||||
void readXMLFile(std::string file)
|
||||
vtkSmartPointer<vtkDataObject> readXMLFile(std::string file)
|
||||
{
|
||||
|
||||
vtkSmartPointer<TReader> reader = vtkSmartPointer<TReader>::New();
|
||||
reader->SetFileName(file.c_str());
|
||||
reader->Update();
|
||||
Data.setValue(reader->GetOutput());
|
||||
return reader->GetOutput();
|
||||
}
|
||||
vtkSmartPointer<vtkDataObject> dataObjectFromFile(Base::FileInfo File);
|
||||
};
|
||||
|
||||
} // namespace Fem
|
||||
|
||||
@@ -15,7 +15,16 @@
|
||||
</Documentation>
|
||||
<Methode Name="read">
|
||||
<Documentation>
|
||||
<UserDocu>Read in vtk file</UserDocu>
|
||||
<UserDocu>
|
||||
read(filepath)
|
||||
read([filepaths], [values], unit, frame_type)
|
||||
|
||||
Reads in a single vtk file or creates a multiframe result by reading in multiple result files. If multiframe is wanted, 4 argumenhts are needed:
|
||||
1. List of result files each being one frame,
|
||||
2. List of values valid for each frame (e.g. [s] if time data),
|
||||
3. the unit of the value as FreeCAD.Units.Unit,
|
||||
4. the Description of the frame type
|
||||
</UserDocu>
|
||||
</Documentation>
|
||||
</Methode>
|
||||
<Methode Name="scale">
|
||||
@@ -25,7 +34,16 @@
|
||||
</Methode>
|
||||
<Methode Name="load">
|
||||
<Documentation>
|
||||
<UserDocu>Load a single result object or create a multiframe result by loading multiple result frames. If multistep is wanted, 4 argumenhts are needed: 1. List of result object each being one frame, 2. List of values valid for each frame (e.g. [s] if time data), 3. the unit of the value, 4. the Description of the frames</UserDocu>
|
||||
<UserDocu>
|
||||
load(result_object)
|
||||
load([result_objects], [values], unit, frame_type)
|
||||
|
||||
Load a single result object or create a multiframe result by loading multiple result frames. If multiframe is wanted, 4 argumenhts are needed:
|
||||
1. List of result files each being one frame,
|
||||
2. List of values valid for each frame (e.g. [s] if time data),
|
||||
3. the unit of the value as FreeCAD.Units.Unit,
|
||||
4. the Description of the frame type
|
||||
</UserDocu>
|
||||
</Documentation>
|
||||
</Methode>
|
||||
<Methode Name="getFilter">
|
||||
|
||||
@@ -51,6 +51,76 @@ PyObject* FemPostPipelinePy::read(PyObject* args)
|
||||
PyMem_Free(Name);
|
||||
Py_Return;
|
||||
}
|
||||
|
||||
PyObject *files;
|
||||
PyObject *values = nullptr;
|
||||
PyObject *unitobj = nullptr;
|
||||
const char* value_type;
|
||||
|
||||
if (PyArg_ParseTuple(args, "O|OO!s", &files, &values, &(Base::UnitPy::Type), &unitobj, &value_type)) {
|
||||
if (values == nullptr) {
|
||||
|
||||
// single argument version was called!
|
||||
|
||||
if (!PyUnicode_Check(files)) {
|
||||
PyErr_SetString(PyExc_TypeError, "argument must be file path");
|
||||
return nullptr;
|
||||
}
|
||||
const char* path = PyUnicode_AsUTF8(files);
|
||||
getFemPostPipelinePtr()->read(Base::FileInfo(path));
|
||||
}
|
||||
else if (values != nullptr && unitobj != nullptr) {
|
||||
|
||||
//multistep version!
|
||||
|
||||
if ( !(PyTuple_Check(files) || PyList_Check(files)) ||
|
||||
!(PyTuple_Check(values) || PyList_Check(values)) ) {
|
||||
|
||||
std::string error = std::string("Files and values must be list of strings and number respectively.");
|
||||
throw Base::TypeError(error);
|
||||
}
|
||||
|
||||
// extract the result objects
|
||||
Py::Sequence file_list(files);
|
||||
Py::Sequence::size_type size = file_list.size();
|
||||
std::vector<Base::FileInfo> file_result;
|
||||
file_result.resize(size);
|
||||
|
||||
for (Py::Sequence::size_type i = 0; i < size; i++) {
|
||||
auto path = Py::Object(file_list[i]);
|
||||
if (!path.isString()) {
|
||||
throw Base::TypeError("File path must be string");
|
||||
}
|
||||
file_result[i] = Base::FileInfo(path.as_string());
|
||||
}
|
||||
|
||||
//extract the values
|
||||
Py::Sequence values_list(values);
|
||||
size = values_list.size();
|
||||
std::vector<double> value_result;
|
||||
value_result.resize(size);
|
||||
|
||||
for (Py::Sequence::size_type i = 0; i < size; i++) {
|
||||
auto value = Py::Object(values_list[i]);
|
||||
if (!value.isNumeric()) {
|
||||
std::string error = std::string("Values must be numbers");
|
||||
throw Base::TypeError(error);
|
||||
}
|
||||
value_result[i] = Py::Float(value).as_double();
|
||||
}
|
||||
|
||||
// extract the unit
|
||||
Base::Unit unit = *(static_cast<Base::UnitPy*>(unitobj)->getUnitPtr());
|
||||
|
||||
// extract the value type
|
||||
std::string step_type = std::string(value_type);
|
||||
|
||||
// Finally call the c++ function!
|
||||
getFemPostPipelinePtr()->read(file_result, value_result, unit, step_type);
|
||||
Py_Return;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user