CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   looping over cells (https://www.cfd-online.com/Forums/openfoam-solving/78819-looping-over-cells.html)

 Chrisi1984 August 3, 2010 08:42

looping over cells

Hi all,

I am using the chtMultiRegionSimpleFoam solver.
I want to define different properties (cp, K, rho) for different zones of my solid.
Therefor I edited the "solveSolid.H"-file in this way:

Quote:
 { for (int nonOrth=0; nonOrth<=nNonOrthCorr; nonOrth++) { fvScalarMatrix tEqn ( -fvm::laplacian(K, T) ); tEqn.relax(); eqnResidual = tEqn.solve().initialResidual(); maxResidual = max(eqnResidual, maxResidual); } Info<< "Min/max T:" << min(T).value() << ' ' << max(T).value() << endl; ////////////////////////////////////// forAll(solidRegions[i].cellZones(), iZone) //loop over all cellZones { if (solidRegions[i].cellZones()[iZone].name() == "s-oc") //with "bla" being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_1+cp1_1*T+cp2_1*T*T; K=K0_1+K1_1*T+K2_1*T*T; rho=rho_1; } } if (solidRegions[i].cellZones()[iZone].name() == "s-outer-jacket") //with "bla" being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_1+cp1_1*T+cp2_1*T*T; K=K0_1+K1_1*T+K2_1*T*T; rho=rho_1; } } if (solidRegions[i].cellZones()[iZone].name() == "s-insu-mat") //with "bla" being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_3; K=K0_3+K1_3*T+K2_3*T*T; rho=rho_2; } } if (solidRegions[i].cellZones()[iZone].name() == "s-inner-jacket") //with "bla" being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_1+cp1_1*T+cp2_1*T*T; K=K0_1+K1_1*T+K2_1*T*T; rho=rho_1; } } if (solidRegions[i].cellZones()[iZone].name() == "s-sm2") //with bla being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_3; K=K0_2+K1_2*T+K2_2*T*T; rho=rho_2; } } if (solidRegions[i].cellZones()[iZone].name() == "s-sm1") //with bla being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_3; K=K0_2+K1_2*T+K2_2*T*T; rho=rho_2; } } if (solidRegions[i].cellZones()[iZone].name() == "s-ic-oj") //with bla being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_1+cp1_1*T+cp2_1*T*T; K=K0_1+K1_1*T+K2_1*T*T; rho=rho_1; } } if (solidRegions[i].cellZones()[iZone].name() == "s-ic-ij") //with bla being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_1+cp1_1*T+cp2_1*T*T; K=K0_1+K1_1*T+K2_1*T*T; rho=rho_1; } } if (solidRegions[i].cellZones()[iZone].name() == "s-ip") //with bla being name of porous cellZone { forAll(solidRegions[i].cellZones()[iZone], iCell) //loop over all cells in Zone { cp=cp0_1+cp1_1*T+cp2_1*T*T; K=K0_1+K1_1*T+K2_1*T*T; rho=rho_1; } } } ///////////////////////////////////// }
I derive the parameters by an aditional dict. The solver compiles without any probleme.
But when I start the simulation, it lasts very long to solve my solid region. I think the loops are responsible for that.
An additinal problem is that, that the values for cp, K and rho are not calculated different in the different zones.

Is there a mistake in my loops construction?

Is there a better alternative to calculate the properties different in different zones of the solid instead of my loops over all cells, which lasts so long?

Best regards
Chrisi

 olesen August 4, 2010 05:55

Quote:
 Originally Posted by Chrisi1984 (Post 270027) Hi all, I am using the chtMultiRegionSimpleFoam solver. I want to define different properties (cp, K, rho) for different zones of my solid. Therefor I edited the "solveSolid.H"-file in this way: I derive the parameters by an aditional dict. The solver compiles without any probleme. But when I start the simulation, it lasts very long to solve my solid region. I think the loops are responsible for that. An additinal problem is that, that the values for cp, K and rho are not calculated different in the different zones. Is there a mistake in my loops construction? Is there a better alternative to calculate the properties different in different zones of the solid instead of my loops over all cells, which lasts so long? Thanks in advance! Best regards Chrisi

There are a number of problems. From what you write, you are trying to adjust the entire fields (rho, cp, K) within each loop and using entire fields for everything (including T) instead of doing it element-wise. This would certainly slow things down a lot, and probably is not what you want.

If you didn't make any other changes, you also have cp defined as const volScalarField& , so I don't see how any of it actually compiled at all.

 Chrisi1984 August 4, 2010 08:12

I now did it this way

Quote:
 forAll (solidRegions[i].cellZones(), zoneI) { if (solidRegions[i].cellZones()[zoneI].name() == ("s-oc")) //with "bla" being name cellZone { forAll(solidRegions[i].cellZones()[zoneI], iCells) //loop over all cells in Zone { cp[solidRegions[i].cellZones()[zoneI][iCells]]=380.68+0.35536*T[solidRegions[i].cellZones()[zoneI][iCells]]-0.00012713*T[solidRegions[i].cellZones()[zoneI][iCells]]*T[solidRegions[i].cellZones()[zoneI][iCells]]; K[solidRegions[i].cellZones()[zoneI][iCells]]=9.6995954+0.017429246*T[solidRegions[i].cellZones()[zoneI][iCells]]-2.0551036e-06*T[solidRegions[i].cellZones()[zoneI][iCells]]*T[solidRegions[i].cellZones()[zoneI][iCells]]; rho[solidRegions[i].cellZones()[zoneI][iCells]]=7920; } } }
It seems running correctly and much faster than before.

Best regards

Chrisi

 olesen August 4, 2010 08:26

Quote:
 Originally Posted by Chrisi1984 (Post 270215) Thank you for your hint! I now did it this way It seems running correctly and much faster than before.
For readability, don't be afraid to use intermediate variables. Most of them should be optimized away anyhow. For example,
Code:

```forAll(solidRegions[i].cellZones(), zoneI) {     const cellZone& cz = solidRegions[i].cellZones()[zoneI];     const labelList& cells = cz;     if (cz.name() == "s-oc")     {         forAll(cells, iCells)         {             const label cellId = cells[iCells];             const scalar& cellT = T[cellId];             cp[cellId] = 380.68 + cellT * (0.35536 - 0.00012713 * cellT);             K[cellId] = 9.6995954 + cellT * (0.017429246 - 2.0551036e-06 * cellT);             rho[cellId] = 7920;         }     } }```

 Chrisi1984 August 5, 2010 02:44

Thank you again.

I changed it.

Regards
Chrisi

 All times are GMT -4. The time now is 14:50.