    

 ## 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(Averaging, B867395_no_crash_with_empty_element_list)

{

 <a id="_a16" name="_a16"></a>[ansys::dpf::MeshedRegion](classansys_1_1dpf_1_1MeshedRegion.xhtml) m;

 m.<a id="a17" name="a17"></a>[addNode](classansys_1_1dpf_1_1MeshedRegion.xhtml#a2b4891674d74a792b63318c305963500)(1, { 0.0,0.0,0.0 });

 m.[addNode](classansys_1_1dpf_1_1MeshedRegion.xhtml#a2b4891674d74a792b63318c305963500)(2, { 1.0,0.0,0.0 });

 m.[addNode](classansys_1_1dpf_1_1MeshedRegion.xhtml#a2b4891674d74a792b63318c305963500)(3, { 0.0,1.0,0.0 });

 

 <a id="_a18" name="_a18"></a>[ansys::dpf::Field](classansys_1_1dpf_1_1Field.xhtml) nodal_field;

 {

 <a id="_a19" name="_a19"></a>[ansys::dpf::FieldDefinition](classansys_1_1dpf_1_1FieldDefinition.xhtml) fdef;

 fdef.<a id="a20" name="a20"></a>[setLocation](classansys_1_1dpf_1_1FieldDefinition.xhtml#a5ef8fa0b594db3066e6aab50d3b019b6)([ansys::dpf::locations::nodal](structansys_1_1dpf_1_1locations.xhtml#aa4e1967b0838d8597200c606c8564d29));

 fdef.<a id="a21" name="a21"></a>[dimensions](classansys_1_1dpf_1_1FieldDefinition.xhtml#a0d33e4d4faaa30b3808014b19de680cd)() = { 1 };

 fdef.<a id="a22" name="a22"></a>[setUnit](classansys_1_1dpf_1_1FieldDefinition.xhtml#a8033b1d4486ea3d819ecbb6418bab079)(<a id="_a23" name="_a23"></a>[ansys::dpf::Unit](classansys_1_1dpf_1_1Unit.xhtml)("m"));

 nodal_field.<a id="a24" name="a24"></a>[setFieldDefinition](classansys_1_1dpf_1_1Field.xhtml#a471c3f541458acc9c05dfb019a99abb3)(fdef);

 nodal_field.<a id="a25" name="a25"></a>[push\_back](classansys_1_1dpf_1_1Field.xhtml#a5b717ed309ddf287134905f71ada833b)(1, { 1.0 });

 nodal_field.[push\_back](classansys_1_1dpf_1_1Field.xhtml#a5b717ed309ddf287134905f71ada833b)(2, { 2.0 });

 nodal_field.[push\_back](classansys_1_1dpf_1_1Field.xhtml#a5b717ed309ddf287134905f71ada833b)(3, { 3.0 });

 nodal_field.<a id="a26" name="a26"></a>[setSupport](classansys_1_1dpf_1_1Field.xhtml#ab71f825765a7ce1b37373443917ec23e)(m);

 }

 

 [ansys::dpf::Operator](classansys_1_1dpf_1_1Operator.xhtml) 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");

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

 [ansys::dpf::MeshedRegion](classansys_1_1dpf_1_1MeshedRegion.xhtml) mesh = meshProvider.<a id="a27" name="a27"></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="a28" name="a28"></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="_a29" name="_a29"></a>[ansys::dpf::ScopingsContainer](classansys_1_1dpf_1_1ScopingsContainer.xhtml) scopings_per_body = scoping_op.<a id="a30" name="a30"></a>[getOutputScopingsContainer](classansys_1_1dpf_1_1Operator.xhtml#ab8effed66eaf13176c6d68eb3fd2c11f)(0);

 

 EXPECT_EQ(scopings_per_body.size(), 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="a31" name="a31"></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.size(), 4);

}

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

**Definition:** dpf_api.h:2371



[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](classansys_1_1dpf_1_1FieldDefinition.xhtml)

**Definition:** dpf_api.h:633



[ansys::dpf::FieldDefinition::dimensions](classansys_1_1dpf_1_1FieldDefinition.xhtml#a0d33e4d4faaa30b3808014b19de680cd)

std::vector&lt; dp_int &gt; dimensions() const



[ansys::dpf::FieldDefinition::setLocation](classansys_1_1dpf_1_1FieldDefinition.xhtml#a5ef8fa0b594db3066e6aab50d3b019b6)

void setLocation(ansys::dpf::Location const &amp;rhs)



[ansys::dpf::FieldDefinition::setUnit](classansys_1_1dpf_1_1FieldDefinition.xhtml#a8033b1d4486ea3d819ecbb6418bab079)

void setUnit(ansys::dpf::Unit const &amp;rhs)



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

ansys::dpf::Location location() const



[ansys::dpf::Field](classansys_1_1dpf_1_1Field.xhtml)

Data for an entity.

**Definition:** dpf_api.h:991



[ansys::dpf::Field::setFieldDefinition](classansys_1_1dpf_1_1Field.xhtml#a471c3f541458acc9c05dfb019a99abb3)

void setFieldDefinition(FieldDefinition const &amp;f)



[ansys::dpf::Field::push\_back](classansys_1_1dpf_1_1Field.xhtml#a5b717ed309ddf287134905f71ada833b)

void push_back(dp_id entity_id, std::vector&lt; dp_double &gt; const &amp;values)



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

FieldDefinition fieldDefinition() const



[ansys::dpf::Field::setSupport](classansys_1_1dpf_1_1Field.xhtml#ab71f825765a7ce1b37373443917ec23e)

void setSupport(ansys::dpf::Support const &amp;s)



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

Contains a group of fields.

**Definition:** dpf_api.h:1889



[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:4231



[ansys::dpf::MeshedRegion::addNode](classansys_1_1dpf_1_1MeshedRegion.xhtml#a2b4891674d74a792b63318c305963500)

void addNode(dp_id node_id, std::array&lt; dp_double, 3 &gt; const &amp;data)



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

Wrap an elementary operation.

**Definition:** dpf_api.h:2651



[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:1983



[ansys::dpf::Unit](classansys_1_1dpf_1_1Unit.xhtml)

**Definition:** dpf_api.h:301



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

@ eDataSourcesPin

**Definition:** dpf_api.h:2583



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

@ eMeshScopPin

**Definition:** dpf_api.h:2577



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

@ eMeshRegionPin

**Definition:** dpf_api.h:2589



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

@ eLocationPin

**Definition:** dpf_api.h:2593



[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:180



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

static const Location elemental

**Definition:** dpf_api_base.h:178



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

static const Location nodal

**Definition:** dpf_api_base.h:176