Arcane  4.2.2.0
Developer documentation
Loading...
Searching...
No Matches
ParamFile.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ParamFile.cc (C) 2000-2026 */
9/* */
10/* Reader of parameter files. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/launcher/internal/ParamFile.h"
15
16#include "arcane/launcher/ArcaneLauncher.h"
17
18#include "arcane/utils/PlatformUtils.h"
19#include "arcane/utils/UniqueArray.h"
20#include "arcane/utils/FatalErrorException.h"
21#include "arcane/utils/StringBuilder.h"
22#include "arcane/utils/CommandLineArguments.h"
23#include "arcane/utils/JSONReader.h"
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arcane
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34/*
35 * Example of parameter file (WiP) :
36{
37 "versions": {
38 "file": 0,
39 "arcane": 0,
40 "run_opt": 0
41 },
42
43 "dataset": "./dataset.arc",
44 // Coucou
45
46 "common": {
47 "name": "Common",
48 "arcane": {
49 "options": {
50 "//meshes/mesh/filename": "aaa.msh",
51 "T": 4
52 }
53 },
54 "run_opt": {
55 "mpi": 4
56 }
57 },
58
59 "variations": {
60 "variation1": {
61 "name": "Variation1",
62 "arcane": {
63 "options": {
64 "//meshes/mesh/filename": "aaa.msh",
65 "T": 4
66 }
67 },
68 "run_opt": {
69 "mpi": 4
70 }
71 },
72 "variation2": {
73 "name": "Variation2",
74 "arcane": {
75 "options": {
76 "//meshes/mesh/filename": "bbb.msh",
77 "T": 8
78 }
79 }
80 }
81 }
82}
83 */
84
85void ParamFile::
86editParams(const String& param_file_name, const String& variation)
87{
88 CommandLineArguments cargs = ArcaneLauncher::applicationInfo().commandLineArguments();
89
90 UniqueArray<Byte> bytes;
91
92 if (platform::readAllFile(param_file_name, false, bytes)) {
93 ARCANE_FATAL("Param file not available");
94 }
95
96 JSONDocument json_doc;
97 json_doc.parse(bytes, param_file_name, (JSONDocument::ParseCommentsFlag | JSONDocument::ParseNumbersAsStringsFlag));
98
99 const JSONValue root = json_doc.root();
100
101 {
102 const JSONValue dataset = root.child("dataset");
103 if (!dataset.isNull()) {
104 cargs.addParameterLine(String::format("CaseDatasetFileName={0}", dataset.value()));
105 // std::cout << "Dataset : " << dataset.value() << std::endl;
106 }
107 }
108
109 StringBuilder name;
110
111 {
112 JSONValue variations = root.child("common");
113 if (!variations.isNull()) {
114 {
115 JSONValue name_var = variations.child("name");
116 if (!name_var.isNull()) {
117 name += name_var.valueAsStringView();
118 name += ".";
119 }
120 }
121
122 JSONValue arcane_params = variations.child("arcane").child("options");
123 if (!arcane_params.isNull()) {
124 const JSONKeyValueList params = arcane_params.keyValueChildren();
125
126 for (auto elem : params) {
127 cargs.addParameterLine( String::format("{0}={1}", elem.name(), elem.value().valueAsStringView()));
128
129 // std::cout << "Elem : " << elem.name() << " -- val : " << elem.value().valueAsStringView() << std::endl;
130 }
131 }
132 }
133 }
134
135 if (!variation.empty()) {
136 const JSONValue variations = root.child("variations");
137 if (!variations.isNull()) {
138 const JSONValue asked_var = variations.expectedChild(variation);
139
140 {
141 const JSONValue name_var = asked_var.child("name");
142 if (!name_var.isNull()) {
143 name += name_var.valueAsStringView();
144 }
145 }
146
147 const JSONValue arcane_params = asked_var.child("arcane").child("options");
148 if (!arcane_params.isNull()) {
149 const JSONKeyValueList params = arcane_params.keyValueChildren();
150
151 for (auto elem : params) {
152 cargs.addParameterLine( String::format("{0}={1}", elem.name(), elem.value().valueAsStringView()));
153
154 // std::cout << "Elem : " << elem.name() << " -- val : " << elem.value().valueAsStringView() << std::endl;
155 }
156 }
157 }
158 }
159
160 // std::cout << "Name variation : " << name << std::endl;
161
162 // StringList names;
163 // StringList values;
164 // cargs.fillParameters(names, values);
165 // for (Integer i = 0, n = names.count(); i < n; ++i) {
166 // std::cout << "Final Elem : " << names[i] << " -- val : " << values[i] << std::endl;
167 // }
168
170}
171
172/*---------------------------------------------------------------------------*/
173/*---------------------------------------------------------------------------*/
174
175} // End namespace Arcane
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
void setCommandLineArguments(const CommandLineArguments &args)
Sets the command line arguments.
const CommandLineArguments & commandLineArguments() const
Command line arguments.
static ApplicationInfo & applicationInfo()
Application information.
@ ParseNumbersAsStringsFlag
Parse all numbers (ints/doubles) as strings.
@ ParseCommentsFlag
Allow one-line (//) and multi-line (/‍**/) comments.
bool readAllFile(StringView filename, bool is_binary, ByteArray &out_bytes)
Reads the content of a file and stores it in out_bytes.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --