trixi-framework / Trixi.jl

Trixi.jl: Adaptive high-order numerical simulations of conservation laws in Julia
https://trixi-framework.github.io/Trixi.jl
MIT License
527 stars 104 forks source link

Pointwise boundary surface values #1920

Open Arpit-Babbar opened 5 months ago

Arpit-Babbar commented 5 months ago

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

github-actions[bot] commented 5 months ago

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

Code quality

Documentation

Testing

Performance

Verification

Created with :heart: by the Trixi.jl community.

Arpit-Babbar commented 5 months ago

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).

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 91.41104% with 14 lines in your changes missing coverage. Please review.

Project coverage is 96.30%. Comparing base (f9c9107) to head (9d7231d).

Files with missing lines Patch % Lines
...rc/callbacks_step/analysis_surface_pointwise_2d.jl 92.24% 9 Missing :warning:
src/callbacks_step/analysis.jl 82.14% 5 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1920 +/- ## ========================================== - Coverage 96.32% 96.30% -0.02% ========================================== Files 470 471 +1 Lines 37447 37586 +139 ========================================== + Hits 36070 36195 +125 - Misses 1377 1391 +14 ``` | [Flag](https://app.codecov.io/gh/trixi-framework/Trixi.jl/pull/1920/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=trixi-framework) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/trixi-framework/Trixi.jl/pull/1920/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=trixi-framework) | `96.30% <91.41%> (-0.02%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=trixi-framework#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

DanielDoehring commented 5 months ago

I suggest we adapt/copy this code

https://github.com/trixi-framework/Trixi.jl/blob/655497374d9991a7bd814b08af6751e81becab7d/src/callbacks_step/analysis.jl#L583-L607

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:

https://github.com/trixi-framework/Trixi.jl/blob/655497374d9991a7bd814b08af6751e81becab7d/src/callbacks_step/analysis.jl#L487-L488

DanielDoehring commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

The values are now written off in the usual fashion (as for e.g. restart & save solution) callbacks.

Please have a look @sloede @Arpit-Babbar

DanielDoehring commented 4 months ago

Regarding one of the TODOs: With mean value you are probably referring to the mean value of the quantity within that element, right? I see your point that one might be interested in computing this as well, as this cannot be easily recovered at post-processing.

At the same time, I have no clear use-case for this in mind.

Concerning sorting with increasing x value: This sounds reasonable to me, as this adds at least some structure to the output.

DanielDoehring commented 4 months ago

The data-export looks good to me. For some reason one needs to transpose the array when using h5py.

import h5py
import numpy as np

# Open the file in read mode
with h5py.File('CF_x_000000.h5', 'r') as f:
  # List all groups
  print("Keys: %s" % f.keys())

  # Iterate over all keys
  for key in f.keys():
    data = f[key]

    data_array = np.array(data)
    if np.ndim(data_array) > 1:
      # Transpose to get dimensions right
      data_array = data_array.T

    print("Dimensions of array for key %s: %s" % (key, data_array.shape))
    print("Data for key %s: %s" % (key, data_array))
Arpit-Babbar commented 4 months ago

I have added a (passing) test that reads the final h5 file and checks the first ten values. Is that okay? (Note that there are no 'trivial values' on the surface of an airfoil, so the first ten values give a reasonable validation)

I have also updated the gist to read h5 files, which confirms that the forces are being computed correctly.

DanielDoehring commented 4 months ago

@Arpit-Babbar can you also upload the h5 files as gists (not sure if this is possible) that you are comparing against? I would like to take a look at them, just to verify everything is working :)

DanielDoehring commented 4 months ago

We should put this on hold until https://github.com/trixi-framework/Trixi.jl/pull/1959 is tackled / we decide how we address https://github.com/trixi-framework/Trixi.jl/issues/1955

DanielDoehring commented 2 months ago

@Arpit-Babbar : I did some changes to be AMR-ready, see https://github.com/trixi-framework/Trixi.jl/pull/1959 . Now the test fail, however. Can you take a look if we still match the reference data or if I messed something up?

Arpit-Babbar commented 2 months ago

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

I regenerated the data and saw that both the plots look exactly the same as this. Did we remove the reordering (following Michael's suggestion?)? That will need the CI to be updated.

DanielDoehring commented 2 months ago

I regenerated the data and saw that both the plots look exactly the same as this. Did we remove the reordering (following Michael's suggestion?)? That will need the CI to be updated.

That is good!

Yes, the sorting is not longer done. Take a look at the test files/CI test, I now sort there, but the values do not agree. This is why I asked you to check this again :)

Arpit-Babbar commented 2 months ago

Yes, the sorting is not longer done. Take a look at the test files/CI test, I now sort there, but the values do not agree. This is why I asked you to check this again :)

I see it now. The sorting

    @test sort(cp_vals[1:10]) ≈ [1.5152278762705806
           1.5182860925755697
           1.7417814818119175
           1.871513931283643
           1.874247697759559
           2.0354491762572806
           2.0400855926847936
           2.0956920203526193
           2.098639791879171
           2.1591959225932777]

does not look equivalent to sorting along the increasing x direction. For that, you will have to sort along read(Trixi.h5open("out/CP_x_000007.h5"))["point_coordinates"][:,1]

DanielDoehring commented 2 months ago

I see it now. The sorting does not look equivalent to sorting along the increasing x direction.

Yes, but this is also not intended. As you can see, the reference data is also sorting in ascending order, so I seek to do the same with the data, as I am not sure whether the order of the indices is deterministic.

We match some values, while some we miss, see : https://github.com/trixi-framework/Trixi.jl/actions/runs/9810227214/job/27089931612?pr=1920#step:7:8617

DanielDoehring commented 1 week ago

I guess we could also move this into a package called TrixiAero or something similar that implements this specialized analysis routines, thereby not polluting the analyze file even more.