Introduce a function to compute stress tensor - #1127
Conversation
and removed CAvgGrad_Base::GetMeanRateOfStrainMatrix, CSourcePieceWise_TurbSST::GetMeanRateOfStrainMatrix The new CNumerics member is initialized only if using_uq, in analogy to the member MeanReynoldsStress. Generalize this later.
and used in CAvgGrad_Base::SetReynoldsStressMatrix, CSourcePieceWise_TurbSST::SetReynoldsStressMatrix
this applies to the mean rate of strain matrix as well Currently they are only allocated if UQ methodology is used
The methods CNumerics::ComputeStressTensor and CNumerics::ComputeMeanRateOfStrainMatrix are now static, and their input (rate of strain or primitive variable gradients, respectively) and output (stress tensor or rate of strain, resp) are given as parameters. We try to use this for both laminar and turbulent stresses.
CNumerics::ComputeStressTensor takes primitive variable gradient instead of rate of strain matrix now
and conversions of multidim arrays into pointers were modified
pcarruscag
left a comment
There was a problem hiding this comment.
Absolutely awesome, this is my favorite kind of pull request!
If you feel like it, I think the QCR modification is also repeated in some places, and there is a bug in how it is computed (see #992).
See if the template suggestion makes sense to simplify things even a bit more. It should be fine to update the failing testcases (they are very sensitive ones).
| - TWO3*total_viscosity*div_vel*delta[iDim][jDim]; | ||
| // compute both parts in one step | ||
| const su2double total_viscosity = val_laminar_viscosity + val_eddy_viscosity; | ||
| ComputeStressTensor(nDim, tau, val_gradprimvar+1, total_viscosity, Density, 0.0); // TODO why ignore turb_ke? |
There was a problem hiding this comment.
That is a good question, you could try adding it to see how much it breaks the regressions. Maybe someone knows if it is for stability reasons.
|
Regarding finding all code blocks, there is numerics_simd/flow/diffusion/common.hpp :: stressTensor. |
from the other UQ quantities any more. This partly reverts commit c2ded5a.
|
Thanks for the contribution! This is a much needed change. I wanted to perform a refactoring like this myself. The viscous (or turbulent) stress calculation is repeated many times in the code. Changing that calculation (e.g. adding QCR or UQ) currently requires changing many files. In my humble opinion, the stress tensor calculation should have its own class at the lowest level of the code, like the gas model (e.g. ideal gas) or the viscosity calculation (e.g. Sutherland's law). The best way to make sure that both |
so that they accept arguments of type su2double**, su2double[3][3], etc.
|
Maybe CNumerics is not the perfect place, but it is good enough for government work (there are much more misplaced things in there). The "everything is class" OO approach applied to the lowest level of abstraction is... well I think it is terrible -- and it has taken me a mighty long time to get rid of it in CPoint and CVariable and to design alternative Numerics -- because:
My introduction to C++ was also the "everything is class", then one day I read "From Mathematics to Generic Programming" and well, I started liking C++ a whole lot more. OO and its patterns are very good high level tools to achieve encapsulation and to isolate code, which are very important for projects with millions of lines of code, but for low level things they are overkill. Sorry for the rant, I guess I have strong feelings about tiny classes. |
pcarruscag
left a comment
There was a problem hiding this comment.
Thank you for the changes @maxaehle , LGTM
|
@pcarruscag I agree about OO being overkill for the stress tensor. My main concerns are:
That can be accomplished in a number of ways, including functional or data-oriented programming. And, since I'm not the one submitting the PR, I understand that my opinions are just opinions. Pay as much or as little attention to them as you like. |
|
Ah! uff glad we agree (I do pay attention to what you think). |
|
I just poked around in In |
|
I searched for |
|
I'll wait for the regression tests to fail and then update |
There were minor differences in the residuals, probably because round-off errors in the computation of the stress tensor accumulate over the solver iterations. turb_naca0012_1c, _2c, _p1c1, _p1c2 in serial, parallel, hybrid regression poiseuille_profile in serial regression stat_fsi in hybrid regression
poiseuille_profile in parallel_regression.py
|
Now the tests are fine. Can I merge? |
|
You can replace the computation in python_wrapper_structure.cpp, the benefit of having a central definition outweighs the velocity divergence thing. |
this changes the arithmetics
|
I observed that sometimes only the product of the stress tensor with a vector is of interest, e.g.: Do you think it is reasonable to have another function, which takes |
|
Maybe if that was in the hot path of the code it would be worth trying. |
|
Ok, so I will merge the pull request now? |
|
I think you can merge since this is a much needed change (but usually we give it some time so everyone has a chance to review). |
oh man, I totally saw the unrolled loops in assembler as well 🤥 ... maybe one day 😢 |
|
Hi @maxaehle , I just looked over this PR (after merging 🐌 ) and thanks a lot, it looks really tidy and I appreciate the detailed function documentation. |
Proposed Changes
Provide a function
CNumerics::ComputeStressTensorto compute the laminar and turbulent stress tensor. Replace the code blocks currently computing it, which exist at many locations inside the code, by function calls.Related Work
?
To be discussed
Is CNumerics the best location for this function?
4 of the 5
rans_uqregression tests fail (in addition to those that failed before the change already). This seems to be due to floating-point errors inCSourcePieceWise_TurbSST::SetReynoldsStressMatrix,CAvgGrad_Base::SetReynoldsStressMatrix. These little deviations (relative difference around 1e-16) are probably amplified over the 10 iterations, so that the solver takes a notably different path to the steady state solution and the intermediate logarithmic residuals differ. I checked (for the1cand2ctest cases) that the converged solutions are nearly equal though. (I can supply vtu files and plots if requested.)Should I update the stored residual values in
serial_regression.pythen?Also I am not sure whether I have found all code blocks that can be replaced by the new function.
PR Checklist
Put an X by all that apply. You can fill this out after submitting the PR. If you have any questions, don't hesitate to ask! We want to help. These are a guide for you to know what the reviewers will be looking for in your contribution.