Skip to main content

DPF C++ Client Library 2025 R2

AveragingTest

Last update: 16.07.2025

Examples showing how to average results to reach a nodal or elemental form, per bodies...

//
// COPYRIGHT ANSYS. ALL RIGHTS RESERVED.
//
#include "dpf_api.h"
#include <limits>
#ifdef EXAMPLE
#include "Example.h"
#else
#include <gtest/gtest.h>
#endif
TEST(Averaging, ResultsRequestedLocation)
{
// The location is the type of topology associated with the data container.
// For finite element data, the location is one of three spatial locations: Nodal, Elemental, or ElementalNodal.
// For more information about the location, see https://dpf.docs.pyansys.com/version/stable/concepts/concepts.html#location.
//
// When reading a result, a specific location can be requested.
// The following example illustrates how to request a Nodal location for a result with an ElementalNodal native location.
// Data source
std::string fileName("../../../testfiles/mapdl_files/TwoSolids.rst");
dataSources.addResultFile(fileName);
ansys::dpf::Operator stress_op("S");
if (stress_op.empty()) {
throw std::runtime_error("S operator failed to load");
}
stress_op.connect(ansys::dpf::eDataSourcesPin, dataSources);
EXPECT_EQ(std::string(result_fc.at(0).fieldDefinition().location().c_str()), std::string(ansys::dpf::locations::elemental_nodal));
stress_op.connect(ansys::dpf::eLocationPin, std::string(ansys::dpf::locations::nodal.c_str()));
result_fc = stress_op.getOutputFieldsContainer(0);
EXPECT_EQ(std::string(result_fc.at(0).fieldDefinition().location().c_str()), std::string(ansys::dpf::locations::nodal.c_str()));
stress_op.connect(ansys::dpf::eLocationPin, std::string(ansys::dpf::locations::elemental.c_str()));
result_fc = stress_op.getOutputFieldsContainer(0);
EXPECT_EQ(std::string(result_fc.at(0).fieldDefinition().location().c_str()), std::string(ansys::dpf::locations::elemental.c_str()));
}
TEST(Averaging, B867395_no_crash_with_empty_element_list)
{
m.addNode(1, { 0.0,0.0,0.0 });
m.addNode(2, { 1.0,0.0,0.0 });
m.addNode(3, { 0.0,1.0,0.0 });
ansys::dpf::Field nodal_field;
{
fdef.dimensions() = { 1 };
nodal_field.setFieldDefinition(fdef);
nodal_field.push_back(1, { 1.0 });
nodal_field.push_back(2, { 2.0 });
nodal_field.push_back(3, { 3.0 });
nodal_field.setSupport(m);
}
ansys::dpf::Operator averager("nodal_to_elemental");
averager.connect(0, nodal_field);
auto out = averager.getOutputField(0);
EXPECT_EQ(out.dataSize(), 0);
}
TEST(AveragingPerBody, Nodal)
{
// The following example shows you how to average a result per body for a multibody simulation.
// Data source
std::string fileName("../../../testfiles/mapdl_files/TwoSolids.rst");
dataSources.addResultFile(fileName);
// Get scoping split by body
// -------------------------
// Instantiate MeshProvider operator to get the MeshedRegion
ansys::dpf::Operator meshProvider = ansys::dpf::Operator("MeshProvider");
if (meshProvider.empty()) {
throw std::runtime_error("MeshProvider failed to load");
}
meshProvider.connect(ansys::dpf::eDataSourcesPin, dataSources);
// Instantiate the scoping::by_property operator that provides a ScopingsContainer
// containing a scoping per body
ansys::dpf::Operator scoping_op = ansys::dpf::Operator("scoping::by_property");
if (scoping_op.empty()) {
throw std::runtime_error("scoping::by_property operator failed to load");
}
scoping_op.connect(ansys::dpf::eMeshRegionPin, mesh);
scoping_op.connect(13, std::string("mat")); // split per body with mat attribute
ansys::dpf::ScopingsContainer scopings_per_body = scoping_op.getOutputScopingsContainer(0);
EXPECT_EQ(scopings_per_body.size(), 4);
// Get split result
// ----------------
// Instantiate the result operator (stress)
ansys::dpf::Operator stress_op("S");
if (stress_op.empty()) {
throw std::runtime_error("S operator failed to load");
}
// Connect the DataSources
stress_op.connect(ansys::dpf::eDataSourcesPin, dataSources);
// Request a location for the output
// Stress result from TwoSolids.rst file has native ElementalNodal location
// An average from ElementalNodal to Nodal location will be done
// Provide the ScopingsContainer (containing a scoping per body) as input to the operator
// The resulting FieldsContainer will contain a Field per body
stress_op.connect(ansys::dpf::eMeshScopPin, scopings_per_body);
// Compute output FieldsContainer
ansys::dpf::FieldsContainer result_fc = stress_op.getOutputFieldsContainer(0);
EXPECT_EQ(result_fc.size(), 4);
}
Definition: dpf_api.h:2433
void addResultFile(std::string const &file_path, std::string const &key)
virtual bool empty() const
Definition: dpf_api.h:667
std::vector< dp_int > dimensions() const
void setLocation(ansys::dpf::Location const &rhs)
void setUnit(ansys::dpf::Unit const &rhs)
ansys::dpf::Location location() const
Data for an entity.
Definition: dpf_api.h:1028
void setFieldDefinition(FieldDefinition const &f)
void push_back(dp_id entity_id, std::vector< dp_double > const &values)
FieldDefinition fieldDefinition() const
void setSupport(ansys::dpf::Support const &s)
Contains a group of fields.
Definition: dpf_api.h:1945
Field at(dp_index index)
Holds the mesh for a given region (body, faces, skin, ...)
Definition: dpf_api.h:4305
void addNode(dp_id node_id, std::array< dp_double, 3 > const &data)
Wrap an elementary operation.
Definition: dpf_api.h:2724
FieldsContainer getOutputFieldsContainer(dp_index pin_index)
ScopingsContainer getOutputScopingsContainer(dp_index pin_index)
MeshedRegion getOutputMeshedRegion(dp_index pin_index)
Definition: dpf_api.h:2040
Definition: dpf_api.h:329
@ eDataSourcesPin
Definition: dpf_api.h:2647
@ eMeshScopPin
Definition: dpf_api.h:2641
@ eMeshRegionPin
Definition: dpf_api.h:2653
@ eLocationPin
Definition: dpf_api.h:2657
const char * c_str() const
static const Location elemental_nodal
Definition: dpf_api_base.h:181
static const Location elemental
Definition: dpf_api_base.h:179
static const Location nodal
Definition: dpf_api_base.h:177

Connect with Ansys