Skip to content

Commit

Permalink
Tentatively working version
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Oct 20, 2022
1 parent 434c7a1 commit ad9cd98
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 24 deletions.
1 change: 1 addition & 0 deletions include/openPMD/RecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ OPENPMD_protected

inline internal::RecordComponentData &get()
{
datasetDefined();
return *m_recordComponentData;
}

Expand Down
2 changes: 1 addition & 1 deletion include/openPMD/backend/Attributable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Attributable
friend class BaseRecord;
template <typename T_elem>
friend class BaseRecordInterface;
template <typename>
template <typename, typename>
friend class internal::BaseRecordData;
template <typename T, typename T_key, typename T_container>
friend class Container;
Expand Down
57 changes: 45 additions & 12 deletions include/openPMD/backend/BaseRecord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace internal
*/
template <
typename T_elem_maybe_void,
typename T_RecordComponent = RecordComponent>
typename T_RecordComponent = T_elem_maybe_void>
class BaseRecord
: public Container<
typename auxiliary::OkOr<T_elem_maybe_void, BaseRecord<void> >::type>
Expand All @@ -91,18 +91,25 @@ class BaseRecord
std::shared_ptr<internal::BaseRecordData<T_elem, T_RecordComponentData> >
m_baseRecordData{
new internal::BaseRecordData<T_elem, T_RecordComponentData>()};
using Data_t = internal::BaseRecordData<T_elem, T_RecordComponentData>;

inline internal::BaseRecordData<T_elem, T_RecordComponentData> &get()
inline Data_t &get()
{
return *m_baseRecordData;
}

inline internal::BaseRecordData<T_elem, T_RecordComponentData> const &
get() const
inline Data_t const &get() const
{
return *m_baseRecordData;
}

inline void setData(std::shared_ptr<Data_t> data)
{
m_baseRecordData = std::move(data);
T_Container::setData(m_baseRecordData);
T_RecordComponent::setData(m_baseRecordData);
}

BaseRecord();

public:
Expand All @@ -119,6 +126,14 @@ class BaseRecord
using iterator = typename Container<T_elem>::iterator;
using const_iterator = typename Container<T_elem>::const_iterator;

// this avoids object slicing
operator T_RecordComponent() const
{
T_RecordComponent res;
res.setData(m_baseRecordData);
return res;
}

virtual ~BaseRecord() = default;

mapped_type &operator[](key_type const &key) override;
Expand Down Expand Up @@ -160,11 +175,13 @@ class BaseRecord
// BaseRecord(internal::BaseRecordData<T_elem> *);
void readBase();

void datasetDefined() override;

private:
void flush(std::string const &, internal::FlushParams const &) final;
virtual void
flush_impl(std::string const &, internal::FlushParams const &) = 0;
virtual void read() = 0;
// virtual void read() = 0;

/**
* @brief Check recursively whether this BaseRecord is dirty.
Expand Down Expand Up @@ -195,7 +212,8 @@ namespace internal
template <typename T_elem, typename T_RecordComponent>
BaseRecord<T_elem, T_RecordComponent>::BaseRecord()
{
Container<T_elem>::setData(m_baseRecordData);
T_Container::setData(m_baseRecordData);
T_RecordComponent::setData(m_baseRecordData);
}

template <typename T_elem, typename T_RecordComponent>
Expand All @@ -214,12 +232,12 @@ BaseRecord<T_elem, T_RecordComponent>::operator[](key_type const &key)
"A scalar component can not be contained at "
"the same time as one or more regular components.");

mapped_type &ret = Container<T_elem>::operator[](key);
if (keyScalar)
{
get().m_containsScalar = true;
ret.parent() = this->parent();
datasetDefined();
}
mapped_type &ret = keyScalar ? static_cast<mapped_type &>(*this)
: T_Container::operator[](key);
return ret;
}
}
Expand All @@ -240,12 +258,15 @@ BaseRecord<T_elem, T_RecordComponent>::operator[](key_type &&key)
"A scalar component can not be contained at "
"the same time as one or more regular components.");

mapped_type &ret = Container<T_elem>::operator[](std::move(key));
if (keyScalar)
{
get().m_containsScalar = true;
ret.parent() = this->parent();
datasetDefined();
}
/*
* datasetDefined() inits the container entry
*/
mapped_type &ret = keyScalar ? T_Container::at(std::move(key))
: T_Container::operator[](std::move(key));
return ret;
}
}
Expand Down Expand Up @@ -406,4 +427,16 @@ inline bool BaseRecord<T_elem, T_RecordComponent>::dirtyRecursive() const
}
return false;
}

template <typename T_elem, typename T_RecordComponent>
void BaseRecord<T_elem, T_RecordComponent>::datasetDefined()
{
// If the RecordComponent API of this object has been used, then the record
// is a scalar one
T_RecordComponent copy;
copy.setData(m_baseRecordData);
T_Container::emplace(RecordComponent::SCALAR, std::move(copy));
get().m_containsScalar = true;
T_RecordComponent::datasetDefined();
}
} // namespace openPMD
2 changes: 2 additions & 0 deletions include/openPMD/backend/BaseRecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class BaseRecordComponent : virtual public Attributable
Attributable::setData(m_baseRecordComponentData);
}

virtual void datasetDefined();

BaseRecordComponent();
}; // BaseRecordComponent

Expand Down
5 changes: 5 additions & 0 deletions include/openPMD/backend/MeshRecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MeshRecordComponent : public RecordComponent
{
template <typename T, typename T_key, typename T_container>
friend class Container;
template <typename, typename>
friend class BaseRecord;

friend class Mesh;

Expand Down Expand Up @@ -70,6 +72,9 @@ class MeshRecordComponent : public RecordComponent
*/
template <typename T>
MeshRecordComponent &makeConstant(T);

protected:
void datasetDefined() override;
};

template <typename T>
Expand Down
14 changes: 7 additions & 7 deletions include/openPMD/backend/PatchRecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ OPENPMD_private
// clang-format on

void flush(std::string const &, internal::FlushParams const &);
void read();
virtual void read();

/**
* @brief Check recursively whether this RecordComponent is dirty.
Expand All @@ -104,8 +104,8 @@ OPENPMD_private
*/
bool dirtyRecursive() const;

std::shared_ptr<internal::PatchRecordComponentData>
m_patchRecordComponentData{new internal::PatchRecordComponentData()};
std::shared_ptr<internal::PatchRecordComponentData> m_recordComponentData{
new internal::PatchRecordComponentData()};

PatchRecordComponent();

Expand All @@ -115,19 +115,19 @@ OPENPMD_protected

inline internal::PatchRecordComponentData const &get() const
{
return *m_patchRecordComponentData;
return *m_recordComponentData;
}

inline internal::PatchRecordComponentData &get()
{
return *m_patchRecordComponentData;
return *m_recordComponentData;
}

inline void
setData(std::shared_ptr<internal::PatchRecordComponentData> data)
{
m_patchRecordComponentData = std::move(data);
BaseRecordComponent::setData(m_patchRecordComponentData);
m_recordComponentData = std::move(data);
BaseRecordComponent::setData(m_recordComponentData);
}
}; // PatchRecordComponent

Expand Down
3 changes: 3 additions & 0 deletions src/RecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ RecordComponent &RecordComponent::resetDataset(Dataset d)
throw error::WrongAPIUsage(
"[RecordComponent] Must set specific datatype.");
}

datasetDefined();
// if( d.extent.empty() )
// throw std::runtime_error("Dataset extent must be at least 1D.");
if (std::any_of(
Expand Down Expand Up @@ -167,6 +169,7 @@ RecordComponent &RecordComponent::makeEmpty(Dataset d)
if (rc.m_dataset.extent.size() == 0)
throw std::runtime_error("Dataset extent must be at least 1D.");

datasetDefined();
rc.m_isEmpty = true;
dirty() = true;
if (!written())
Expand Down
4 changes: 4 additions & 0 deletions src/backend/BaseRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ BaseRecordComponent &BaseRecordComponent::resetDatatype(Datatype d)
"A Records Datatype can not (yet) be changed after it has been "
"written.");

datasetDefined();
get().m_dataset.dtype = d;
return *this;
}
Expand Down Expand Up @@ -69,4 +70,7 @@ BaseRecordComponent::BaseRecordComponent()
{
Attributable::setData(m_baseRecordComponentData);
}

void BaseRecordComponent::datasetDefined()
{}
} // namespace openPMD
14 changes: 11 additions & 3 deletions src/backend/MeshRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
namespace openPMD
{
MeshRecordComponent::MeshRecordComponent() : RecordComponent()
{
setPosition(std::vector<double>{0});
}
{}

void MeshRecordComponent::read()
{
Expand Down Expand Up @@ -64,6 +62,16 @@ MeshRecordComponent &MeshRecordComponent::setPosition(std::vector<T> pos)
return *this;
}

void MeshRecordComponent::datasetDefined()
{
if (access::write(IOHandler()->m_frontendAccess) &&
!containsAttribute("position"))
{
setPosition(std::vector<double>{0});
}
RecordComponent::datasetDefined();
}

template MeshRecordComponent &
MeshRecordComponent::setPosition(std::vector<float> pos);
template MeshRecordComponent &
Expand Down
3 changes: 2 additions & 1 deletion src/backend/PatchRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ PatchRecordComponent &PatchRecordComponent::resetDataset(Dataset d)
throw std::runtime_error(
"Dataset extent must not be zero in any dimension.");

datasetDefined();
get().m_dataset = d;
dirty() = true;
return *this;
Expand All @@ -68,7 +69,7 @@ Extent PatchRecordComponent::getExtent() const

PatchRecordComponent::PatchRecordComponent()
{
BaseRecordComponent::setData(m_patchRecordComponentData);
BaseRecordComponent::setData(m_recordComponentData);
setUnitSI(1);
}

Expand Down
2 changes: 2 additions & 0 deletions test/CoreTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,8 @@ TEST_CASE("empty_record_test", "[core]")
"RecordComponents: E"));
o.iterations[1].meshes["E"][RecordComponent::SCALAR].resetDataset(
Dataset(Datatype::DOUBLE, {1}));
auto B = o.iterations[1].meshes["B"];
B.resetDataset(Dataset(Datatype::DOUBLE, {1}));
o.flush();
}

Expand Down

0 comments on commit ad9cd98

Please sign in to comment.