14#include "arcane/impl/internal/TimeHistoryMngInternal.h"
16#include "arcane/core/IMeshMng.h"
17#include "arcane/core/IPropertyMng.h"
19#include "arcane/utils/JSONWriter.h"
33 m_trace_mng->info() <<
"Add CurveWriter2 name=" << writer->name();
35 m_curve_writers2.insert(writer);
44 for (
auto& cw : m_curve_writers2)
45 if (cw->name() == name) {
59 meta_data_str() <<
"<?xml version='1.0' ?>\n";
60 meta_data_str() <<
"<curves>\n";
63 meta_data_str() <<
"<curve "
64 <<
" name='" << val->
name() <<
"'"
65 <<
" index='" << val->
index() <<
"'"
67 <<
" sub-size='" << val->subSize() <<
"'";
70 meta_data_str() <<
" support='" << val->
meshHandle().meshName() <<
"'";
76 meta_data_str() <<
"/>\n";
78 meta_data_str() <<
"</curves>\n";
81 String ss = meta_data_str.str();
95 m_observer_pool.addObserver(
this,
99 m_observer_pool.addObserver(
this,
101 m_variable_mng->writeObservable());
110 auto p = m_properties;
111 p->setInt32(
"version", m_version);
122 addValue(thpi, m_common_variables.globalTime());
176 Integer master_io_rank = m_parallel_mng->masterIORank();
179 for (
Integer i = 0; i < m_parallel_mng->commSize(); ++i) {
180 if (i != master_io_rank) {
183 for (
Integer icurve = 0; icurve < nb_curve; ++icurve) {
184 m_parallel_mng->recv(length, i);
190 m_parallel_mng->recv(buf, i);
191 m_parallel_mng->recv(iterations_to_write, i);
192 m_parallel_mng->recv(values_to_write, i);
196 if (length[4] != 0) {
198 m_parallel_mng->recv(buf2, i);
201 TimeHistoryCurveInfo curve_info(name, name_mesh, iterations_to_write, values_to_write, length[3], i);
217 Integer master_io_rank = m_parallel_mng->masterIORank();
226 th.
arrayToWrite(iterations_to_write, values_to_write, infos);
229 length[1] = iterations_to_write.
size();
230 length[2] = values_to_write.
size();
231 length[3] = th.subSize();
239 m_parallel_mng->send(length, master_io_rank);
241 m_parallel_mng->send(iterations_to_write, master_io_rank);
242 m_parallel_mng->send(values_to_write, master_io_rank);
289 bool need_update =
false;
291 auto p = m_properties;
294 if (!p->get(
"version", version)) {
296 m_trace_mng->info() <<
"The checkpoint contains legacy format of TimeHistory variables, updating...";
300 else if (version == 2) {
301 m_trace_mng->info() <<
"TimeHistory Variables version 2";
304 ARCANE_FATAL(
"Unknown TimeHistory Variables format -- Found version: {0}", version);
307 m_trace_mng->info(4) <<
"readVariables resizes m_global_time to " <<
m_th_global_time.size();
311 m_trace_mng->info() <<
"Reading the values history";
313 IIOMng* io_mng = m_parallel_mng->ioMng();
316 m_trace_mng->error() <<
" METADATA len=" <<
m_th_meta_data().length()
318 ARCANE_FATAL(
"The meta-data of TimeHistoryMng2 are invalid.");
320 XmlNode root_node = doc->documentNode();
326 String ustr_index(
"index");
327 String ustr_sub_size(
"sub-size");
328 String ustr_data_type(
"data-type");
331 String ustr_support(
"support");
332 String ustr_sub_domain(
"sub-domain");
335 String name = curve.attrValue(ustr_name);
336 Integer index = curve.attr(ustr_index).valueAsInteger();
337 Integer sub_size = curve.attr(ustr_sub_size).valueAsInteger();
338 String data_type_str = curve.attrValue(ustr_data_type);
340 String support_str = curve.attrValue(ustr_support,
false);
342 XmlNode sub_domain_node = curve.
attr(ustr_sub_domain);
343 Integer sub_domain = NULL_SUB_DOMAIN_ID;
344 if (!sub_domain_node.
null()) {
349 if (sub_domain != NULL_SUB_DOMAIN_ID && m_parallel_mng->commRank() != sub_domain) {
359 if (support_str.
null()) {
396 name = name +
"_" + mh.meshName();
398 if (sub_domain != NULL_SUB_DOMAIN_ID) {
399 name = name +
"_Local";
411 Integer current_iteration = m_common_variables.globalIteration();
419 if (n > current_iteration) {
420 n = current_iteration;
422 m_trace_mng->info() <<
"TimeHistoryRestore: truncating TimeHistoryGlobalTime array to size n=" << n <<
"\n";
429 i->second->removeAfterIteration(current_iteration);
439 m_directory = directory;
440 if (m_output_path.empty()) {
441 m_output_path = m_directory.path();
442 if (m_directory.createDirectory()) {
443 m_trace_mng->warning() <<
"Can't create the output directory '" << m_output_path <<
"'";
458 if (thpi.timeHistoryAddValueArg().isLocal() && thpi.timeHistoryAddValueArg().localSubDomainId() != m_parallel_mng->commRank()) {
466 String name_to_find = thpi.timeHistoryAddValueArg().name().
clone();
467 if (!thpi.meshHandle().
isNull()) {
470 name_to_find = name_to_find +
"_" + thpi.meshHandle().meshName();
472 if (thpi.timeHistoryAddValueArg().isLocal()) {
473 name_to_find = name_to_find +
"_Local";
480 hl->second->arrayToWrite(iterations, values, infos);
494 m_trace_mng->debug() <<
"Writing of the history of values path=" << m_output_path;
500 for (
auto& cw_ref : m_curve_writers2) {
502 m_trace_mng->debug() <<
"Writing curves with '" << writer->
name()
516 Integer master_io_rank = m_parallel_mng->masterIORank();
518 std::ofstream ofile(m_directory.file(
"time_history.xml").localstr());
519 ofile <<
"<?xml version='1.0' ?>\n";
520 ofile <<
"<curves>\n";
525 ofile <<
"<curve name='";
530 ofile <<
"SD" << master_io_rank <<
"_";
532 ofile << th.
name() <<
"'/>\n";
539 for (
Integer i = 0; i < m_parallel_mng->commSize(); ++i)
540 if (i != master_io_rank) {
543 for (
Integer icurve = 0; icurve < nb_curve; ++icurve) {
545 m_parallel_mng->recv(length, i);
548 m_parallel_mng->recv(buf, i);
549 ofile <<
"<curve name='";
551 if (length[1] != 0) {
553 m_parallel_mng->recv(buf2, i);
556 ofile <<
"SD" << i <<
"_";
561 ofile <<
"</curves>\n";
575 m_parallel_mng->send(length, master_io_rank);
581 m_parallel_mng->send(length, master_io_rank);
598 JSONWriter json_writer(JSONWriter::FormatFlags::None);
603 json_writer.write(
"version", 1);
605 Integer master_io_rank = m_parallel_mng->masterIORank();
606 json_writer.writeKey(
"curves");
607 json_writer.beginArray();
613 json_writer.write(
"name", th.
name());
618 json_writer.write(
"support", th.
meshHandle().meshName());
619 name = name + th.
meshHandle().meshName() +
"_";
623 json_writer.write(
"sub-domain", master_io_rank);
624 name = name +
"SD" + String::fromNumber(master_io_rank) +
"_";
627 json_writer.write(
"unique-name", name + th.
name());
635 for (
Integer i = 0; i < m_parallel_mng->commSize(); ++i) {
636 if (i != master_io_rank) {
639 for (
Integer icurve = 0; icurve < nb_curve; ++icurve) {
642 m_parallel_mng->recv(length, i);
645 m_parallel_mng->recv(buf, i);
650 if (length[1] != 0) {
652 m_parallel_mng->recv(buf2, i);
657 name = name +
"SD" + String::fromNumber(i) +
"_";
659 json_writer.write(
"sub-domain", i);
665 json_writer.endArray();
671 std::ofstream ofile(out_dir.
file(
"time_history.json").
localstr());
672 ofile << json_writer.getBuffer();
679 Integer master_io_rank = m_parallel_mng->masterIORank();
690 m_parallel_mng->send(length, master_io_rank);
696 m_parallel_mng->send(length, master_io_rank);
707template <
class DataType>
715 if (thpi.timeHistoryAddValueArg().isLocal()) {
723 if (thpi.timeHistoryAddValueArg().isLocal() && thpi.timeHistoryAddValueArg().localSubDomainId() != m_parallel_mng->commRank()) {
727 String name_to_find = thpi.timeHistoryAddValueArg().name().
clone();
728 if (!thpi.meshHandle().
isNull()) {
731 name_to_find = name_to_find +
"_" + thpi.meshHandle().meshName();
733 if (thpi.timeHistoryAddValueArg().isLocal()) {
734 name_to_find = name_to_find +
"_Local";
737 Integer iteration = m_common_variables.globalIteration();
739 if (!thpi.timeHistoryAddValueArg().endTime() && iteration != 0)
748 if (!thpi.meshHandle().
isNull()) {
758 if (values.
size() != th->subSize()) {
759 ARCANE_FATAL(
"Bad subsize for curve '{0}' current={1} old={2}",
760 name_to_find, values.
size(), th->subSize());
783 IVariable* ptr_old_global_time = m_variable_mng->findMeshVariable(default_mesh,
"TimeHistoryGlobalTime");
784 IVariable* ptr_old_meta_data = m_variable_mng->findMeshVariable(default_mesh,
"TimeHistoryMetaData");
785 if (ptr_old_global_time ==
nullptr || ptr_old_meta_data ==
nullptr)
786 ARCANE_FATAL(
"TimeHistoryGlobalTime or TimeHistoryMetaData is not found.");
795 old_global_time.
resize(0);
796 old_meta_data.
reset();
805 m_curve_writers2.erase(writer);
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Integer size() const
Number of elements in the vector.
Modifiable view of an array of type T.
constexpr Integer size() const noexcept
Returns the size of the array.
const T * unguardedBasePointer() const
void clear()
Removes the elements from the array.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Constant iterator interval.
Class managing a directory.
String file(const String &file_name) const override
Returns the full path of the file file_name in the directory.
Interface of the input/output manager.
virtual IXmlDocumentHolder * parseXmlString(const String &str, const String &name)=0
Reads and parses the XML file contained in the string str.
virtual MeshHandle * findMeshHandle(const String &name, bool throw_exception)=0
Searches for the mesh with name name.
Interface of the property manager.
virtual IObservable * writeObservable()=0
Observable for writing.
Interface for a curve writer.
virtual String name() const =0
Writer name.
virtual void endWrite()=0
Notify the end of writing.
virtual void beginWrite(const TimeHistoryCurveWriterInfo &infos)=0
Notify the start of writing.
virtual void writeCurve(const TimeHistoryCurveInfo &infos)=0
Write a curve.
Interface of an object transforming history curves.
Iterator intervalThis class manages an iterator pair allowing modification of the elements of the con...
bool isNull() const
Indicates if the handle is null (it does not reference any existing mesh or not).
Output stream linked to a String.
T * get() const
Returns the object referenced by the instance.
Reference to an instance.
Encapsulation of an automatically destructing pointer.
Unicode character string.
bool null() const
Returns true if the string is null.
Int64 length() const
Returns the length of the string.
String clone() const
Clones this string.
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
Class extending the arguments when adding a value to a value history.
Information for writing a curve.
Information about writing curves.
bool isShrinkActive() const override
Returns a boolean indicating if the history is compressed.
void updateMetaData() override
Method allowing the curve metadata to be updated.
bool m_is_master_io
True if I am the IO manager.
void addNowInGlobalTime() override
Method allowing the current GlobalTime to be added to the GlobalTimes array.
void addValue(const TimeHistoryAddValueArgInternal &thpi, Real value) override
Method allowing a value to be added to a history.
void _dumpSummaryOfCurves()
Method allowing a JSON file to be dumped with the name of each curve output in GNUPLOT format as well...
bool m_is_master_io_of_sd
True if I am the IO manager for my subdomain.
void _removeCurveWriter(const Ref< ITimeHistoryCurveWriter2 > &writer)
Method allowing removal of a writer.
void updateGlobalTimeCurve() override
Method allowing the GlobalTime array to be copied into the global GlobalTime variable.
void resizeArrayAfterRestore() override
Method allowing the value arrays to be resized after a restart.
void addObservers(IPropertyMng *prop_mng) override
Method allowing observers saving the history before a checkpoint to be added.
bool m_is_dump_active
Indicates if dumps are active.
void removeCurveWriter(const String &name) override
Method allowing a writer to be removed.
void readVariables(IMeshMng *mesh_mng, IMesh *default_mesh) override
Method allowing previously written curves to be retrieved during a restart.
void dumpHistory() override
Method allowing all curves to be written using all registered writers.
void _dumpSummaryOfCurvesLegacy()
Method allowing an XML file to be dumped with the name of each curve output in GNUPLOT format.
HistoryList m_history_list
List of histories.
void _addHistoryValue(const TimeHistoryAddValueArgInternal &thpi, ConstArrayView< DataType > values)
Method allowing values to be added to a value history.
VariableArrayReal m_th_global_time
Array of time instants.
bool m_enable_non_io_master_curves
Indicates if curve writing by non-io_master procs is possible.
void _dumpCurvesAllWriters()
Method allowing all curves to be dumped with all writers.
void applyTransformation(ITimeHistoryTransformer *v) override
Applies the transformation v to all curves.
void iterationsAndValues(const TimeHistoryAddValueArgInternal &thpi, UniqueArray< Int32 > &iterations, UniqueArray< Real > &values) override
Method allowing the iterations and values of a history to be outputted.
void dumpCurves(ITimeHistoryCurveWriter2 *writer) override
Method allowing curves to be written using the provided writer.
bool m_io_master_write_only
Indicates if writers must be called by all processes.
void _saveProperties()
Method allowing saving the properties of the metadata.
VariableScalarString m_th_meta_data
History info.
void _fromLegacyFormat(IMesh *default_mesh)
Method allowing conversion from the old format to the new.
void editOutputPath(const Directory &directory) override
Method allowing the curve output directory to be changed.
void _destroyAll()
Destructor.
RealUniqueArray m_global_times
List of global times.
bool m_need_comm
Indicates if at least one curve is non-local (thus requiring communications).
bool m_is_active
Indicates if the service is active.
void addCurveWriter(Ref< ITimeHistoryCurveWriter2 > writer) override
Method allowing a writer to be added for curve output.
void addValue(ConstArrayView< DataType > values, Integer iteration)
Method allowing the addition of values to an iteration.
Base class for a value history.
Integer index() const
History index in the list.
const MeshHandle & meshHandle() const
Method allowing the retrieval of the registered MeshHandle.
const String & name() const
History name.
eDataType dataType() const
History data type.
virtual void applyTransformation(ITraceMng *msg, ITimeHistoryTransformer *v)=0
Method allowing the application of a transformation on the values of the value history.
virtual void fromOldToNewVariables(IVariableMng *vm, IMesh *default_mesh)=0
Method allowing the conversion of variables from old saves to the new format.
Integer localSubDomainId() const
Method allowing the retrieval of the subdomain ID to which this history belongs.
virtual void arrayToWrite(UniqueArray< Int32 > &iterations, UniqueArray< Real > &values, const TimeHistoryCurveWriterInfo &infos) const =0
Method allowing the retrieval of iterations and values from a value history.
bool isLocal() const
Method allowing determination if it is a global history or local to a subdomain.
virtual void dumpValues(ITraceMng *msg, ITimeHistoryCurveWriter2 *writer, const TimeHistoryCurveWriterInfo &infos) const =0
Prints the history values using the writer writer.
1D data vector with value semantics (STL style).
virtual void resize(Integer new_size)
Resizes the array to contain new_size elements.
void reset()
Resets the variable to its default value.
List of nodes of a DOM tree.
XmlNode attr(const String &name, bool throw_exception=false) const
Returns the attribute of name name.
XmlNode child(const String &name) const
Child node of this node with name name.
XmlNodeList children(const String &name) const
Set of child nodes of this node having the name name.
bool null() const
True if the node is null.
Integer valueAsInteger(bool throw_exception=false) const
Node value converted to integer.
VariableRefArrayT< Real > VariableArrayReal
Array variable of real type.
VariableRefScalarT< String > VariableScalarString
Scalar variable of character string type.
@ ReduceMin
Minimum of values.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Integer arcaneCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as the size of an array....
Int32 Integer
Type representing an integer.
@ DT_Int32
32-bit integer data type
@ DT_Int64
64-bit integer data type
ARCANE_DATATYPE_EXPORT eDataType dataTypeFromName(const char *name, bool &has_error)
Finds the type associated with name.
const char * dataTypeName(eDataType type)
Data type name.
std::int32_t Int32
Signed integer type of 32 bits.