Before being able to code an operation, you must understand how a loop over a list of mesh entities, such as cells or nodes, is written. Indeed, almost all operations performed are done on a set of entities and therefore involve a loop over a list of entities. For example, calculating the mass of the cells consists of looping over all the cells and performing the product of its volume by its density for each one. Conventionally, this can be written as follows:
The for loop consists of three parts separated by a semicolon. The first is the initialization, the second is the loop exit test, and the third is the operation performed between two iterations.
The previous writing has several disadvantages:
Considering that the list of entities is always traversed in the same order, it is possible to model the previous behavior using four operations:
The mechanism is then general and independent of the container type: the set of entities could be implemented as an array or a list without changing this formalism. In the architecture, the counter above is called an iterator and iterating over the set of elements is done by providing a start and end iterator, otherwise called an enumerator.
In Arcane, this enumerator derives from the base class ItemEnumerator and has the following methods:
To add an additional level of abstraction and to allow code instrumentation, Arcane provides a function in the form of a macro for each enumerator type. It is therefore not necessary to use the methods of ItemEnumerator. This function has the following prototype:
with:
When you are in a module (whose base class is BasicModule) or a service (whose base class is BasicService), Arcane provides methods to access the group containing all entities of a given entity type. For example, the method BasicModule::allCells() allows retrieving the group of all cells. Thus, to iterate over all cells, with i as the name of the iterator, you can do this:
The mass calculation loop described previously then becomes:
The type of an enumerator depends on the type of the mesh element: an enumerator over a group of nodes is not the same type as an enumerator over a group of cells, and they are therefore incompatible. For example, if velocity is a node variable, the following example causes a compilation error:
Similarly, it is impossible to write:
because BasicModule::allNodes() is a group of nodes and i is an enumerator over a group of cells.
Note that the enumerator's '*' operator allows access to the current element:
It is possible to use the entity itself to retrieve the value of a variable, but for performance reasons, you must prioritize access via the iterator: