Hi Pham!
From your description I do not really get why you are specifically doing this, so maybe consider the following:
I assume, you are flagging cells material ids on one locally owned part due to some custom condition - lets say some stress or function,
you cannot formulate in the global coordinate system, so that you cannot decide on the material id simply using cell->center() or similar strategies.
The workaround i came up with is as follows:
Every cell is only flagged from the owning processor and needs to be communicated to other parts of the
p::d:tria afterwards, where those elements are ghosts.
->As a consequence, ghost cells have different material ids depending on the side, from which they are viewed from,
which is what we want to get rid of.
So firstly, create - in my case I already had such a space in use - the simplest possible discontinuous (!) function space,
e.g. with FE_DGQ(degree=0).
Then, you loop over the locally owned cells and assemble simply the material id. - similar to
assembling e.g. the right hand side. (Here the discontinuity of the function space in use comes into play, since you do not
want to mix contributions from different elements (or you consider the internal nodes for C0, but at least 2nd order functions).
After that, you of course compress and convert to a ghosted vector - meaning, that you now have access to the entries of the vectors
in the ghost cells as well - which now contain your material ids.
So finally, you loop over locally owned AND ghosted cells - and set the material id in both from the vector you got,
which is now accessible from the cells you need.
The above approach might not be the fastest one, but if you can reuse some space it might not be too bad.
If someone reading this sees any flaw I am currently not aware of, please let me know! - I am new to both C++ and dealii ; )
Kind regards & good luck coding that up!
Richard