Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Example No. 1

Let's start with Example 1. This simple example allows outputting a table that looks like this:

Results_Example1 Iteration 1 Iteration 2 Iteration 3
Nb de Fissions 36 0 85
Nb de Collisions 29 84 21

We have two rows and three columns here. The numbers present are generated randomly.

Initial entry point

Let's look at the start-init entry point:

SimpleTableOutputExample1Module.cc

void SimpleTableOutputExample1Module::
initModule()
{
// On utilise des valeurs (pseudo-)aléatoires.
srand(1234);
// Initialisation du service.
// On récupère un pointeur vers le singleton créé par Arcane.
ISimpleTableOutput* table = ServiceBuilder<ISimpleTableOutput>(subDomain()).getSingleton();
// On initialise le tableau grâce à un des initialisateurs.
// Le nom du tableau sera "Results" et le nom du fichier sortant sera
// "Results_Example1.X".
//
// On enregistrera le résultat dans le dossier "example1".
// Au final, on aura un fichier ayant comme chemin :
// ./output/csv/example1/Results_Example1.X
//
// X étant selon le format choisi (.csv par exemple).
table->init("Results_Example1", "example1");
// On print le tableau dans son état actuel (vide, avec un titre).
table->print();
}

In this example, we are in singleton mode. The .axl file for this example therefore does not contain options.

However, the .config configuration file references the singleton.

csv.config <time-loop name="example1">

<singleton-services>
<service name="SimpleCsvOutput" need="required" />
</singleton-services>
Note
To be able to separate several execution cases in the same code, I created several time-loops in the .config file.

Let's go back to the .cc file. We only retrieve the pointer to the singleton and initialize it with a table name (Results_Example1) and a subdirectory name (example1) (and we print the empty table).

Loop entry point

Let's look at the compute-loop entry point:

SimpleTableOutputExample1Module.cc

void SimpleTableOutputExample1Module::
loopModule()
{
// On récupère un pointeur vers le singleton créé par Arcane.
// (on pourrait aussi créer un attribut pour éviter de le récupérer
// à chaque fois).
ISimpleTableOutput* table = ServiceBuilder<ISimpleTableOutput>(subDomain()).getSingleton();
// On crée une colonne nommé "Iteration X" (avec X = itération actuelle).
table->addColumn("Iteration " + String::fromNumber(m_global_iteration()));
// On génère deux valeurs (c'est pour l'exemple, sinon oui, ça sert à rien).
Integer nb_fissions = rand()%99;
Integer nb_collisions = rand()%99;
// On ajoute deux valeurs à deux lignes (par défaut,
// si les lignes n'existe pas encore, elles sont créées).
table->addElementInRow("Nb de Fissions", nb_fissions);
table->addElementInRow("Nb de Collisions", nb_collisions);
// On print le tableau dans son état actuel.
table->print();
// On effectue trois itérations.
if (m_global_iteration() == 3)
subDomain()->timeLoopMng()->stopComputeLoop(true);
}

Here, we have a simple example of the typical usage imagined initially for this service.

We create a column named Iteration X (meaning a new column for each iteration), then we add the desired values to rows.

Do the rows not exist because they were not created during init? The service takes care of creating them before adding the value.

In this example, the rows will therefore be created during the first iteration, and afterwards, the service will simply fill them.

Note
We can interchange row and column; the functionality is identical for rows and columns (but the result will obviously be different).

Exporting values from code, for example during a debugging session, is therefore something that is very simple with this service.

Exit entry point

Finally, let's look at the exit entry point:

SimpleTableOutputExample1Module.cc

void SimpleTableOutputExample1Module::
endModule()
{
// On récupère un pointeur vers le singleton créé par Arcane.
ISimpleTableOutput* table = ServiceBuilder<ISimpleTableOutput>(subDomain()).getSingleton();
// On print le tableau dans son état actuel.
table->print();
// On demande l'écriture du fichier.
table->writeFile();
}

In this part, we simply request the writing of the CSV file whose location and name were defined during init. If this had not been done, there are the methods ISimpleTableOutput::setOutputDirectory() and ISimpleTableOutput::setTableName() to correct it (otherwise, the service defines default values).