CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Looping in parallel in template class chemistryModel (https://www.cfd-online.com/Forums/openfoam-programming-development/254878-looping-parallel-template-class-chemistrymodel.html)

mturcios777 March 5, 2024 18:39

Looping in parallel in template class chemistryModel
 
Hello all,

I have been updating some old OpenFOAM code (originally in v2, now in v10/11) based on the chemistryModel library where I do a calculation over all cells. I have added the virtual function to basicChemistryModel and the implementation in chemistryModel/chemistryModel.C. I am trying to just do a loop over all cells with a forAll loop, but I only get the first processor to work.

I can't post the full code so I need to create a stub case, but basically (assuming a private member A_ that is a volScalarField that is registered and read in):
Code:

template<class ThermoType>
void Foam::chemistryModel<ThermoType>::someFunction()
{
    tmp<volScalarField> trho(this->thermo().rho();
    const volScalarField rho = trho();
    scalar AVolAve = 0;
    scalar totalVol = 0;

  forAll(rho,celli)
  {
        A_[celli] = rho[celli];
        //AVolAve = A_[celli]*this->mesh().V()[celli];
        //totalVol = this->mesh().V()[celli];
  }

    //reduce(AVolAve, sumOp<scalar>());
    //reduce(totalVol, sumOp<scalar>());

    //AVolAve /= totalVol;
    //Info << "Volume averaged A is: " << AVolAve << endl;
}

The code compiles and runs perfectly in serial. When running in parallel, the reduce operations are necessary to ensure code executes on all processors. If I comment out he code as above, only the first processor is updated.

Any assistance would be welcome.

olesen March 16, 2024 06:52

Quote:

Originally Posted by mturcios777 (Post 865800)
The code compiles and runs perfectly in serial. When running in parallel, the reduce operations are necessary to ensure code executes on all processors. If I comment out he code as above, only the first processor is updated.

I think that you are much too quick jumping to the wrong conclusion - what makes you think that the code is only being executed on one rank?

If you replace the "Info" statement with "Pout" you will print the values on each rank, which will presumably contradict your conclusion.

olesen March 16, 2024 06:59

Quote:

Originally Posted by mturcios777 (Post 865800)
Code:

    tmp<volScalarField> trho(this->thermo().rho();
    const volScalarField rho = trho();


This is wasteful to make a full copy of the tmp field, please use a reference:
Code:

tmp<volScalarField> trho(this->thermo().rho();
const auto& rho = trho();



All times are GMT -4. The time now is 16:28.