Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointwise boundary surface values #1920

Open
wants to merge 57 commits into
base: main
Choose a base branch
from

Conversation

Arpit-Babbar
Copy link
Member

@Arpit-Babbar Arpit-Babbar commented Apr 26, 2024

This PR add pointwise values on the surface like the coefficient of pressure and friction.

The computation has been verified using the reference data from Roy Charles Swanson, Stefan Langer, Structured and Unstructured Grid Methods (2016)

The verification requires plotting along with reference data, for which the script is available at https://gist.github.com/Arpit-Babbar/924823c08f4be962856a99c107b18fb5

TODO

  • Add tests for pointwise surface values (How to do it?)
  • The many TODOs within the code

Copy link
Contributor

Review checklist

This checklist is meant to assist creators of PRs (to let them know what reviewers will typically look for) and reviewers (to guide them in a structured review process). Items do not need to be checked explicitly for a PR to be eligible for merging.

Purpose and scope

  • The PR has a single goal that is clear from the PR title and/or description.
  • All code changes represent a single set of modifications that logically belong together.
  • No more than 500 lines of code are changed or there is no obvious way to split the PR into multiple PRs.

Code quality

  • The code can be understood easily.
  • Newly introduced names for variables etc. are self-descriptive and consistent with existing naming conventions.
  • There are no redundancies that can be removed by simple modularization/refactoring.
  • There are no leftover debug statements or commented code sections.
  • The code adheres to our conventions and style guide, and to the Julia guidelines.

Documentation

  • New functions and types are documented with a docstring or top-level comment.
  • Relevant publications are referenced in docstrings (see example for formatting).
  • Inline comments are used to document longer or unusual code sections.
  • Comments describe intent ("why?") and not just functionality ("what?").
  • If the PR introduces a significant change or new feature, it is documented in NEWS.md.

Testing

  • The PR passes all tests.
  • New or modified lines of code are covered by tests.
  • New or modified tests run in less then 10 seconds.

Performance

  • There are no type instabilities or memory allocations in performance-critical parts.
  • If the PR intent is to improve performance, before/after time measurements are posted in the PR.

Verification

  • The correctness of the code was verified using appropriate tests.
  • If new equations/methods are added, a convergence test has been run and the results
    are posted in the PR.

Created with ❤️ by the Trixi.jl community.

@Arpit-Babbar Arpit-Babbar changed the title Add analysis_surface_2d.jl WIP: Compute pointwise values on boundary surface Apr 26, 2024
@Arpit-Babbar Arpit-Babbar changed the title WIP: Compute pointwise values on boundary surface WIP: Pointwise boundary surface values Apr 26, 2024
@Arpit-Babbar
Copy link
Member Author

Arpit-Babbar commented Apr 26, 2024

I currently have two issues

  1. The output of pointwise values is saved into a .txt file. Does that need to be changed?

  2. The pointwise surface values are currently being computed as analysis_integral quantities in the AnalysisCallback. Where else can they belong? I think that it should be given its own place within the AnalysisCallback, one that does not print any values to the screen (because there are no values to be printed).

Copy link

codecov bot commented Apr 26, 2024

Codecov Report

Attention: Patch coverage is 7.92683% with 151 lines in your changes are missing coverage. Please review.

Project coverage is 86.39%. Comparing base (2da0863) to head (0374850).
Report is 1 commits behind head on main.

Files Patch % Lines
...rc/callbacks_step/analysis_surface_pointwise_2d.jl 0.00% 119 Missing ⚠️
src/callbacks_step/analysis.jl 25.00% 21 Missing ⚠️
src/callbacks_step/analysis_surface_integral_2d.jl 35.29% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1920      +/-   ##
==========================================
- Coverage   96.11%   86.39%   -9.72%     
==========================================
  Files         460      461       +1     
  Lines       36926    37054     +128     
==========================================
- Hits        35490    32011    -3479     
- Misses       1436     5043    +3607     
Flag Coverage Δ
unittests 86.39% <7.93%> (-9.72%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@andrewwinters5000 andrewwinters5000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not do a full review, just left a question about the need to add another package dependency. As far as your other concerns here are my preliminary thoughts.

  1. A .txt file is fine as ACSII data can be post-processed by nearly any program. However, if we are willing to use DelimitedFiles would something like .csv be better? Just thinking out loud.
  2. Since no integral is computed, then, as far as I understand it, the analysis_integral is basically being overloaded for easier dispatch purposes. Then the pointwise values are computed on the fly with similar drag and lift functions and the write-to-file occurs within the loop over all nodes instead of afterwards. My initial reaction is that this could be confusing. It would be better to be more explicit and create a new generic function called something like analysis_pointwise that could still reuse the caches and such from the AnalysisCallback struct.

src/callbacks_step/analysis_surface_2d.jl Outdated Show resolved Hide resolved
@DanielDoehring DanielDoehring self-assigned this Apr 27, 2024
@DanielDoehring
Copy link
Contributor

DanielDoehring commented Apr 28, 2024

I suggest we adapt/copy this code

# Iterate over tuples of analysis integrals in a type-stable way using "lispy tuple programming".
function analyze_integrals(analysis_integrals::NTuple{N, Any}, io, du, u, t,
semi) where {N}
# Extract the first analysis integral and process it; keep the remaining to be processed later
quantity = first(analysis_integrals)
remaining_quantities = Base.tail(analysis_integrals)
res = analyze(quantity, du, u, t, semi)
if mpi_isroot()
@printf(" %-12s:", pretty_form_utf(quantity))
@printf(" % 10.8e", res)
@printf(io, " % 10.8e", res)
end
mpi_println()
# Recursively call this method with the unprocessed integrals
analyze_integrals(remaining_quantities, io, du, u, t, semi)
return nothing
end
# terminate the type-stable iteration over tuples
function analyze_integrals(analysis_integrals::Tuple{}, io, du, u, t, semi)
nothing
end

to non-integrated quantities, i.e., some which are not returning a scalar value possibly like this:

# Iterate over tuples of analysis integrals in a type-stable way using "lispy tuple programming".
function analyze_pointwise(analysis_quantities::NTuple{N, Any}, io, du, u, t,
                           semi) where {N}

    # Extract the first analysis integral and process it; keep the remaining to be processed later
    quantity = first(analysis_quantities)
    remaining_quantities = Base.tail(analysis_quantities)

    analyze(quantity, du, u, t, semi)

    # Recursively call this method with the unprocessed integrals
    analyze_integrals(remaining_quantities, io, du, u, t, semi)
    return nothing
end

and then call this here:

# additional integrals
analyze_integrals(analysis_integrals, io, du, u, t, semi)

@DanielDoehring
Copy link
Contributor

Regarding the type of file, I think we should aim for something that is easily post-processed, as now one does not get a single value but instead many, which are most likely also accompanied by spatial positions.

@sloede
Copy link
Member

sloede commented Apr 29, 2024

Regarding the type of file, I think we should aim for something that is easily post-processed, as now one does not get a single value but instead many, which are most likely also accompanied by spatial positions.

I recommend to take a look at how I/O is implemented in the TimeSeriesCallback, which allows to record data at each time step at a given set of "recording points". We use an internal caching infrastructure and write all data to HDF5 files. There's also the output of the AnalysisCallback, which writes integral quantities to an ASCII file using CSV formatting.

Overall, the decision on which file format to use IMHO depends heavily on data set size: If either the number of recording points or the number of recordings is at most 10-20, an ASCII file might still work. However, if you want to record at, say, 100 points, and do so for 1000 times over the course of a simulation, I'd probably opt for an HDF5 file.

@DanielDoehring DanielDoehring marked this pull request as ready for review May 13, 2024 15:09
@DanielDoehring DanielDoehring self-requested a review May 13, 2024 15:10
DanielDoehring
DanielDoehring previously approved these changes May 14, 2024
Copy link
Contributor

@DanielDoehring DanielDoehring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me! As I was involved in the coding, it would be nice to get an additional review, preferably by someone who coded up some h5 export as well, such as @sloede and/or @andrewwinters5000

These are the plots from Arpits gist:

c_p
c_f

@sloede sloede changed the title WIP: Pointwise boundary surface values Pointwise boundary surface values May 14, 2024
@DanielDoehring DanielDoehring added the enhancement New feature or request label May 14, 2024
DanielDoehring
DanielDoehring previously approved these changes May 23, 2024
Copy link
Member

@sloede sloede left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is a nice feature addition to Trixi.jl, thanks a lot for your effort!

Generally, I am not 100% sure if the current implementation is really the optimal way to go: Naively, I assume much more code could probably be shared with the AnalysisSurfaceIntegral implementation. Also, I am wondering if it really makes sense to write out these rather heavy files from the AnalysisCallback or whether they would make more sense in the TimeSeriesCallback.

I've left a few more comments (some rather general ones, some more specific ones). I do not want to slow down your development pace, but on the other hand I'd like to avoid that we have a multitude of (or too many) different analysis-like tools (integral quantities over the entire domain, integral quantities over surfaces, pointwise time series over the whole domain, pointwise time series over surfaces) with vastly differing implementations, making it harder to maintain in the intermediate and long term.

src/callbacks_step/analysis.jl Outdated Show resolved Hide resolved
@@ -26,8 +26,8 @@ or `extra_analysis_errors = (:conservation_error,)`.
If you want to omit the computation (to safe compute-time) of the [`default_analysis_errors`](@ref), specify
`analysis_errors = Symbol[]`.
Note: `default_analysis_errors` are `:l2_error` and `:linf_error` for all equations.
If you want to compute `extra_analysis_errors` such as `:conservation_error` solely, i.e.,
without `:l2_error, :linf_error` you need to specify
If you want to compute `extra_analysis_errors` such as `:conservation_error` solely, i.e.,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't attach this to the proper line, but the docstring should include the pointwise analysis in its list of arguments and ideally also (briefly) explain how it can be properly used.

@@ -48,31 +48,31 @@ struct AnalysisSurfaceIntegral{Variable}
end
end

struct ForceState{RealT <: Real}
struct FlowStateDirectional{RealT <: Real}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the name change, or at least why the "Directional" suffix? Are their other states that might be considered? Maybe "FreestreamFlowState" would be more appropriate?

Also, what does psi stand for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the name change, I felt FlowState would be more descriptive.

Directional is since this flow state carries the vector of the free-stream velocity uinf.

The non-directional version of this has only the remaining three quantities.

values, t, iter)
end

varname(::Any) = @assert false "Surface variable name not assigned" # This makes sure default behaviour is not overwriting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
varname(::Any) = @assert false "Surface variable name not assigned" # This makes sure default behaviour is not overwriting

Why define a default at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default is defined to give a helpful error message in case a user forgets to define varname for their new surface quantity. The comment answers the question “Why not set the default name to something trivial like 'variable'?". I am open to doing it differently or removing it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to particular lines of code, but more a general remark:

Does it really make sense to have a completely separate implementation for pointwise surface data and surface integrals? Wouldn't they naturally share a lot of the implementation, maybe except the integration part?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is certainly true. We could try to refactor the current implementation by calling different functions after

    index_range = eachnode(dg)
    for boundary in indices
        element = boundaries.neighbor_ids[boundary]
        node_indices = boundaries.node_indices[boundary]
        direction = indices2direction(node_indices)

        i_node_start, i_node_step = index_to_start_step_2d(node_indices[1], index_range)
        j_node_start, j_node_step = index_to_start_step_2d(node_indices[2], index_range)

        i_node = i_node_start
        j_node = j_node_start
        for node_index in index_range
            u_node = Trixi.get_node_vars(cache.boundaries.u, equations, dg, node_index,
                                         boundary)

since this part is shared among all 4 versions (integrals/points, viscous/inviscid)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be able to call this from one function we would need to add variables to the AnalysisSurfaceIntegral/Pointwise structs.
For Integral, this would be surface_integral = zero(eltype(u)) and for the pointwise stuff

coordinates = Matrix{real(dg)}(undef, n_elements * n_nodes, dim) # physical coordinates of indices
values = Vector{real(dg)}(undef, n_elements * n_nodes) # variable values at indices

Then we still have to figure out how to treat the passing in of the iteration number.
Having said this, while we certainly have code-duplication in the boilerplate part, we currently have analyze methods with 4 different function signatures.

return SurfaceFrictionCoefficient(FlowState(rhoinf, uinf, linf))
end

function (pressure_coefficient::SurfacePressureCoefficient)(u, equations)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function (and most others in this file) would benefit from having at least a short, one-line comment on what they are doing or when they are called. For example, below there are two fairly complex analyze methods that have very similar arguments and it takes a while to figure out their different use cases.

src/callbacks_step/analysis_surface_2d.jl Outdated Show resolved Hide resolved
src/callbacks_step/analysis_surface_2d.jl Outdated Show resolved Hide resolved
Comment on lines 281 to 293
function pretty_form_ascii(::AnalysisSurface{<:SurfacePressureCoefficient{<:Any}})
"CP(x)"
end
function pretty_form_utf(::AnalysisSurface{<:SurfacePressureCoefficient{<:Any}})
"CP(x)"
end

function pretty_form_ascii(::AnalysisSurface{<:SurfaceFrictionCoefficient{<:Any}})
"CF(x)"
end
function pretty_form_utf(::AnalysisSurface{<:SurfaceFrictionCoefficient{<:Any}})
"CF(x)"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If "CF_x" are changed above, they should be adapted here as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants