Skip to content

Commit 012fb22

Browse files
committed
Short mode in default in openPMD >= 2.
1 parent 5c0fb89 commit 012fb22

5 files changed

Lines changed: 57 additions & 4 deletions

File tree

include/openPMD/IO/IOTask.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_FILE>
125125
}
126126

127127
std::string name = "";
128+
std::string openPMDversion; // @todo: Maybe move this to AbstractIOHandler
128129
};
129130

130131
template <>

include/openPMD/RecordComponent.tpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#pragma once
2323

24+
#include "openPMD/Datatype.hpp"
25+
#include "openPMD/Error.hpp"
2426
#include "openPMD/RecordComponent.hpp"
2527
#include "openPMD/Span.hpp"
2628
#include "openPMD/auxiliary/Memory.hpp"
@@ -93,12 +95,38 @@ inline std::shared_ptr<T> RecordComponent::loadChunk(Offset o, Extent e)
9395
#endif
9496
}
9597

98+
namespace detail
99+
{
100+
template <typename To>
101+
struct do_convert
102+
{
103+
template <typename From>
104+
static std::optional<To> call(Attribute &attr)
105+
{
106+
if constexpr (std::is_convertible_v<From, To>)
107+
{
108+
return std::make_optional<To>(attr.get<From>());
109+
}
110+
else
111+
{
112+
return std::nullopt;
113+
}
114+
}
115+
116+
static constexpr char const *errorMsg = "is_conversible";
117+
};
118+
} // namespace detail
119+
96120
template <typename T>
97121
inline void
98122
RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
99123
{
100124
Datatype dtype = determineDatatype(data);
101-
if (dtype != getDatatype())
125+
/*
126+
* For constant components, we implement type conversion, so there is
127+
* a separate check further below.
128+
*/
129+
if (dtype != getDatatype() && !constant())
102130
if (!isSameInteger<T>(getDatatype()) &&
103131
!isSameFloatingPoint<T>(getDatatype()) &&
104132
!isSameComplexFloatingPoint<T>(getDatatype()) &&
@@ -160,10 +188,25 @@ RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
160188
for (auto const &dimensionSize : extent)
161189
numPoints *= dimensionSize;
162190

163-
T value = rc.m_constantValue.get<T>();
191+
std::optional<T> val =
192+
switchNonVectorType<detail::do_convert</* To = */ T>>(
193+
/* from = */ getDatatype(), rc.m_constantValue);
164194

165-
T *raw_ptr = data.get();
166-
std::fill(raw_ptr, raw_ptr + numPoints, value);
195+
if (val.has_value())
196+
{
197+
T *raw_ptr = data.get();
198+
std::fill(raw_ptr, raw_ptr + numPoints, *val);
199+
}
200+
else
201+
{
202+
std::string const data_type_str = datatypeToString(getDatatype());
203+
std::string const requ_type_str =
204+
datatypeToString(determineDatatype<T>());
205+
std::string err_msg =
206+
"Type conversion during chunk loading not possible! ";
207+
err_msg += "Data: " + data_type_str + "; Load as: " + requ_type_str;
208+
throw error::WrongAPIUsage(err_msg);
209+
}
167210
}
168211
else
169212
{

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ void JSONIOHandlerImpl::createFile(
455455
access::write(m_handler->m_backendAccess),
456456
"[JSON] Creating a file in read-only mode is not possible.");
457457

458+
if (m_attributeModeSpecificationVia == SpecificationVia::DefaultValue)
459+
{
460+
m_attributeMode = parameters.openPMDversion >= "2."
461+
? AttributeMode::Short
462+
: AttributeMode::Long;
463+
}
464+
458465
if (!writable->written)
459466
{
460467
std::string name = parameters.name + m_originalExtension;

src/Iteration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ void Iteration::flushFileBased(
206206
/* create file */
207207
Parameter<Operation::CREATE_FILE> fCreate;
208208
fCreate.name = filename;
209+
fCreate.openPMDversion = s.openPMD();
209210
IOHandler()->enqueue(IOTask(&s.writable(), fCreate));
210211

211212
/* create basePath */

src/Series.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ void Series::flushGorVBased(
11741174
}
11751175
Parameter<Operation::CREATE_FILE> fCreate;
11761176
fCreate.name = series.m_name;
1177+
fCreate.openPMDversion = openPMD();
11771178
IOHandler()->enqueue(IOTask(this, fCreate));
11781179
}
11791180

0 commit comments

Comments
 (0)