Deltares / xugrid

Xarray and unstructured grids
https://deltares.github.io/xugrid/
MIT License
64 stars 8 forks source link

[help wanted] understand the optional_attributes #140

Closed xldeltares closed 1 year ago

xldeltares commented 1 year ago

In hydromt-delft3dfm, when creating Ugrid1d and converts it back to a Ugrid dataset to_dataset(), we encountered the use of optional_attributes. By default, optional_attributes is set to False. in that case, network1d_edge_x and network1d_edge_y are not written into the dataset. (But if the Ugrid1d is parsed from a file, then the network1d_edge_x and network1d_edge_y seem to become not optional anymore).

The question is: is that indeed the desired behavior? for our case, we always needed these optional_attributes therefore we needed to set it to True explicitly wherever applicable. Therefore, we figure optional_attributes as True would be more suitable. But maybe there are cases where this argument should be False? Could more info be provided please?

Huite commented 1 year ago

Hi @xldeltares,

As the name implies, the optional attributes are optional. They are not required to define the topology unambiguously: e.g. if you have a Ugrid1d topology, all that is needed is the node_x, the node_y, and the edge_node_connectivity. All other attributes can be derived from these values. Similarly for Ugrid2d and node_x, node_y, and the face_node_connectivity.

However, the UGRID conventions also describe "Optionally required attributes": http://ugrid-conventions.github.io/ugrid-conventions/

I think the best example is for the edge_node_connectivity for a Ugrid2d topology. Let's say we have a grid with a single face:

3 -- 2
|    |
0 -- 1

face_node_connectivity = [[0, 1, 2, 3]]

This contains four edges which are unique:

edge_node_connectivity = [
    [0 , 1],
    [1,  2],
    [2,  3],
    [3,  0],
]

However, the order isn't prescribed, this is valid too:

edge_node_connectivity = [
    [3,  0],
    [1,  2],
    [2,  3],
    [0 , 1],
]

Now let's say you have a value on every edge (e.g. the amount of water flowing). To interpret those correctly, you need to know how the edges are ordered... you need to know the associated edge_node_connectivity.

I've added the optional_attributes flag specifically because DELWAQ requires edge_coordinates and even edge_face_connectivity, and DELWAQ apparently doesn't derive them itself.

Anyway, the basic logic is as follows:

Of course I may have made some mistakes where it doesn't quite behave like this. Different software also has different expectations (e.g. DELWAQ requires all these optional attributes, but other software might only accept the required attributes...).

Huite commented 1 year ago

Hi @xldeltares,

I'm closing this for now. If you have more question, feel free to re-open.