    

 ## On this page

  

 

 # 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 &lt;limits&gt;

\#ifdef EXAMPLE

\#include "Example.h"

\#else

\#include &lt;gtest/gtest.h&gt;

\#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");

 <a id="_a0" name="_a0"></a>[ansys::dpf::DataSources](classansys_1_1dpf_1_1DataSources.xhtml) dataSources;

 dataSources.<a id="a1" name="a1"></a>[addResultFile](classansys_1_1dpf_1_1DataSources.xhtml#ad2a5bb6f24ca621ffaaee9e7a47cdc20)(fileName);

 

 <a id="_a2" name="_a2"></a>[ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml) stress_op("S");

 if (stress_op.<a id="a3" name="a3"></a>[empty](classansys_1_1dpf_1_1DpfTypes.xhtml#a39cd546c42518e2332be8f287db95459)()) {

 throw std::runtime_error("S operator failed to load");

 }

 stress_op.<a id="a4" name="a4"></a>connect(<a id="a5" name="a5"></a>[ansys::dpf::eDataSourcesPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a5b6613f802062fde4faea4b553db7fbc), dataSources);

 <a id="_a6" name="_a6"></a>[ansys::dpf::FieldsContainer](classansys_1_1dpf_1_1FieldsContainer.xhtml) result_fc = stress_op.<a id="a7" name="a7"></a>[getOutputFieldsContainer](classansys_1_1dpf_1_1Operator.xhtml#a9994ffe0f23c4a5de614099abb95286c)(0);

 EXPECT_EQ(std::string(result_fc.<a id="a8" name="a8"></a>[at](classansys_1_1dpf_1_1FieldsContainer.xhtml#addfee935e69fe9fb9e0e533449f9a5bc)(0).<a id="a9" name="a9"></a>[fieldDefinition](classansys_1_1dpf_1_1Field.xhtml#a860e65caa16f612746ad116429282fd3)().<a id="a10" name="a10"></a>[location](classansys_1_1dpf_1_1FieldDefinition.xhtml#a9ea61f23ef1eb93c794b7c222d7740b8)().<a id="a11" name="a11"></a>[c\_str](structansys_1_1dpf_1_1Location.xhtml#ad9fab192124627740ffa623b1a0221ea)()), std::string(<a id="a12" name="a12"></a>[ansys::dpf::locations::elemental\_nodal](structansys_1_1dpf_1_1locations.xhtml#a0bda534055805adc4c08bece34989ff3)));

 

 stress_op.connect(<a id="a13" name="a13"></a>[ansys::dpf::eLocationPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1aa8522e00b69f3a2f2626a088f20823fe), std::string(<a id="a14" name="a14"></a>[ansys::dpf::locations::nodal](structansys_1_1dpf_1_1locations.xhtml#aa4e1967b0838d8597200c606c8564d29).c_str()));

 result_fc = stress_op.[getOutputFieldsContainer](classansys_1_1dpf_1_1Operator.xhtml#a9994ffe0f23c4a5de614099abb95286c)(0);

 EXPECT_EQ(std::string(result_fc.[at](classansys_1_1dpf_1_1FieldsContainer.xhtml#addfee935e69fe9fb9e0e533449f9a5bc)(0).[fieldDefinition](classansys_1_1dpf_1_1Field.xhtml#a860e65caa16f612746ad116429282fd3)().[location](classansys_1_1dpf_1_1FieldDefinition.xhtml#a9ea61f23ef1eb93c794b7c222d7740b8)().[c\_str](structansys_1_1dpf_1_1Location.xhtml#ad9fab192124627740ffa623b1a0221ea)()), std::string([ansys::dpf::locations::nodal](structansys_1_1dpf_1_1locations.xhtml#aa4e1967b0838d8597200c606c8564d29).[c\_str](structansys_1_1dpf_1_1Location.xhtml#ad9fab192124627740ffa623b1a0221ea)()));

 

 stress_op.connect([ansys::dpf::eLocationPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1aa8522e00b69f3a2f2626a088f20823fe), std::string(<a id="a15" name="a15"></a>[ansys::dpf::locations::elemental](structansys_1_1dpf_1_1locations.xhtml#a3f66169a86ab935fb398b3fdde98bb04).c_str()));

 result_fc = stress_op.[getOutputFieldsContainer](classansys_1_1dpf_1_1Operator.xhtml#a9994ffe0f23c4a5de614099abb95286c)(0);

 EXPECT_EQ(std::string(result_fc.[at](classansys_1_1dpf_1_1FieldsContainer.xhtml#addfee935e69fe9fb9e0e533449f9a5bc)(0).[fieldDefinition](classansys_1_1dpf_1_1Field.xhtml#a860e65caa16f612746ad116429282fd3)().[location](classansys_1_1dpf_1_1FieldDefinition.xhtml#a9ea61f23ef1eb93c794b7c222d7740b8)().[c\_str](structansys_1_1dpf_1_1Location.xhtml#ad9fab192124627740ffa623b1a0221ea)()), std::string([ansys::dpf::locations::elemental](structansys_1_1dpf_1_1locations.xhtml#a3f66169a86ab935fb398b3fdde98bb04).[c\_str](structansys_1_1dpf_1_1Location.xhtml#ad9fab192124627740ffa623b1a0221ea)()));

}

 

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");

 [ansys::dpf::DataSources](classansys_1_1dpf_1_1DataSources.xhtml) dataSources;

 dataSources.[addResultFile](classansys_1_1dpf_1_1DataSources.xhtml#ad2a5bb6f24ca621ffaaee9e7a47cdc20)(fileName);

 

 // Get scoping split by body

 // -------------------------

 // Instantiate MeshProvider operator to get the MeshedRegion

 [ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml) meshProvider = [ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml)("MeshProvider");

 if (meshProvider.[empty](classansys_1_1dpf_1_1DpfTypes.xhtml#a39cd546c42518e2332be8f287db95459)()) {

 throw std::runtime_error("MeshProvider failed to load");

 }

 meshProvider.connect([ansys::dpf::eDataSourcesPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a5b6613f802062fde4faea4b553db7fbc), dataSources);

 <a id="_a16" name="_a16"></a>[ansys::dpf::MeshedRegion](classansys_1_1dpf_1_1MeshedRegion.xhtml) mesh = meshProvider.<a id="a17" name="a17"></a>[getOutputMeshedRegion](classansys_1_1dpf_1_1Operator.xhtml#ae67b9230160d80fbca07eb423f180f98)(0);

 

 // Instantiate the scoping::by\_property operator that provides a ScopingsContainer 

 // containing a scoping per body

 [ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml) scoping_op = [ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml)("scoping::by\_property");

 if (scoping_op.[empty](classansys_1_1dpf_1_1DpfTypes.xhtml#a39cd546c42518e2332be8f287db95459)()) {

 throw std::runtime_error("scoping::by\_property operator failed to load");

 }

 scoping_op.connect(<a id="a18" name="a18"></a>[ansys::dpf::eMeshRegionPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a88b6b8d0c02d2ac48fc40e8c02e3da7c), mesh);

 scoping_op.connect([ansys::dpf::eLocationPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1aa8522e00b69f3a2f2626a088f20823fe), [ansys::dpf::locations::elemental](structansys_1_1dpf_1_1locations.xhtml#a3f66169a86ab935fb398b3fdde98bb04));

 scoping_op.connect(13, std::string("mat")); // split per body with mat attribute

 <a id="_a19" name="_a19"></a>[ansys::dpf::ScopingsContainer](classansys_1_1dpf_1_1ScopingsContainer.xhtml) scopings_per_body = scoping_op.<a id="a20" name="a20"></a>[getOutputScopingsContainer](classansys_1_1dpf_1_1Operator.xhtml#ab8effed66eaf13176c6d68eb3fd2c11f)(0);

 

 EXPECT_EQ(scopings_per_body.<a id="a21" name="a21"></a>[size](classansys_1_1dpf_1_1ScopingsContainer.xhtml#a97551f74cb9ca448e5d1050a37403346)(), 4);

 

 // Get split result

 // ----------------

 // Instantiate the result operator (stress)

 [ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml) 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](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a5b6613f802062fde4faea4b553db7fbc), 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

 stress_op.connect([ansys::dpf::eLocationPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1aa8522e00b69f3a2f2626a088f20823fe), [ansys::dpf::locations::nodal](structansys_1_1dpf_1_1locations.xhtml#aa4e1967b0838d8597200c606c8564d29));

 // 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(<a id="a22" name="a22"></a>[ansys::dpf::eMeshScopPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a79a70f074b2f79ccaff75ae8e4ec9ec9), scopings_per_body);

 // Compute output FieldsContainer

 [ansys::dpf::FieldsContainer](classansys_1_1dpf_1_1FieldsContainer.xhtml) result_fc = stress_op.getOutputFieldsContainer(0);

 

 EXPECT_EQ(result_fc.<a id="a23" name="a23"></a>[size](classansys_1_1dpf_1_1FieldsContainer.xhtml#a0f93949d1256055fd0a85a70cd62aab4)(), 4);

}

[ansys::dpf::DataSources](classansys_1_1dpf_1_1DataSources.xhtml)

**Definition:** dpf_api.h:1954



[ansys::dpf::DataSources::addResultFile](classansys_1_1dpf_1_1DataSources.xhtml#ad2a5bb6f24ca621ffaaee9e7a47cdc20)

void addResultFile(std::string const &amp;file_path, std::string const &amp;key)



[ansys::dpf::DpfTypes::empty](classansys_1_1dpf_1_1DpfTypes.xhtml#a39cd546c42518e2332be8f287db95459)

virtual bool empty() const



[ansys::dpf::FieldDefinition::location](classansys_1_1dpf_1_1FieldDefinition.xhtml#a9ea61f23ef1eb93c794b7c222d7740b8)

ansys::dpf::Location location() const



[ansys::dpf::Field::fieldDefinition](classansys_1_1dpf_1_1Field.xhtml#a860e65caa16f612746ad116429282fd3)

FieldDefinition fieldDefinition() const



[ansys::dpf::FieldsContainer](classansys_1_1dpf_1_1FieldsContainer.xhtml)

Contains a group of fields.

**Definition:** dpf_api.h:1566



[ansys::dpf::FieldsContainer::size](classansys_1_1dpf_1_1FieldsContainer.xhtml#a0f93949d1256055fd0a85a70cd62aab4)

dp_int size() const



[ansys::dpf::FieldsContainer::at](classansys_1_1dpf_1_1FieldsContainer.xhtml#addfee935e69fe9fb9e0e533449f9a5bc)

Field at(dp_index index)



[ansys::dpf::MeshedRegion](classansys_1_1dpf_1_1MeshedRegion.xhtml)

Holds the mesh for a given region (body, faces, skin, ...)

**Definition:** dpf_api.h:3596



[ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml)

Wrap an elementary operation.

**Definition:** dpf_api.h:2216



[ansys::dpf::Operator::getOutputFieldsContainer](classansys_1_1dpf_1_1Operator.xhtml#a9994ffe0f23c4a5de614099abb95286c)

FieldsContainer getOutputFieldsContainer(dp_index pin_index)



[ansys::dpf::Operator::getOutputScopingsContainer](classansys_1_1dpf_1_1Operator.xhtml#ab8effed66eaf13176c6d68eb3fd2c11f)

ScopingsContainer getOutputScopingsContainer(dp_index pin_index)



[ansys::dpf::Operator::getOutputMeshedRegion](classansys_1_1dpf_1_1Operator.xhtml#ae67b9230160d80fbca07eb423f180f98)

MeshedRegion getOutputMeshedRegion(dp_index pin_index)



[ansys::dpf::ScopingsContainer](classansys_1_1dpf_1_1ScopingsContainer.xhtml)

**Definition:** dpf_api.h:1717



[ansys::dpf::ScopingsContainer::size](classansys_1_1dpf_1_1ScopingsContainer.xhtml#a97551f74cb9ca448e5d1050a37403346)

dp_int size() const



[ansys::dpf::eDataSourcesPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a5b6613f802062fde4faea4b553db7fbc)

@ eDataSourcesPin

**Definition:** dpf_api.h:2150



[ansys::dpf::eMeshScopPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a79a70f074b2f79ccaff75ae8e4ec9ec9)

@ eMeshScopPin

**Definition:** dpf_api.h:2144



[ansys::dpf::eMeshRegionPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1a88b6b8d0c02d2ac48fc40e8c02e3da7c)

@ eMeshRegionPin

**Definition:** dpf_api.h:2156



[ansys::dpf::eLocationPin](namespaceansys_1_1dpf.xhtml#aa4a44a04a0aafb8fe0645f019bbd94d1aa8522e00b69f3a2f2626a088f20823fe)

@ eLocationPin

**Definition:** dpf_api.h:2160



[ansys::dpf::Location::c\_str](structansys_1_1dpf_1_1Location.xhtml#ad9fab192124627740ffa623b1a0221ea)

const char * c_str() const



[ansys::dpf::locations::elemental\_nodal](structansys_1_1dpf_1_1locations.xhtml#a0bda534055805adc4c08bece34989ff3)

static const Location elemental_nodal

**Definition:** dpf_api_base.h:170



[ansys::dpf::locations::elemental](structansys_1_1dpf_1_1locations.xhtml#a3f66169a86ab935fb398b3fdde98bb04)

static const Location elemental

**Definition:** dpf_api_base.h:168



[ansys::dpf::locations::nodal](structansys_1_1dpf_1_1locations.xhtml#aa4e1967b0838d8597200c606c8564d29)

static const Location nodal

**Definition:** dpf_api_base.h:166