ppap4lmp  0.7.2
Element Class Reference

Element inherits Generator class and contains data element (or entity) as a Json object. More...

#include <element.h>

Inheritance diagram for Element:
Collaboration diagram for Element:

Public Member Functions

 Element ()
 Constructor of Element class. More...
 
virtual ShPtr< Elementget_element (const Json &name=nullptr) override
 Get this object as a shared pointer of Element class. More...
 
virtual ShPtr< Generatorget_generator (const Json &name=nullptr) override
 Get this object as a shared pointer of Generator class. More...
 
virtual void accessed_by_instance_of (const Str &classname) override
 Set class name of an instance calling Element::check_required_keys. More...
 
ShPtr< Elementappend_updater (const ShPtr< Updater > &upd)
 Append an Updater object to this Element object: the only way to extend an updating process associated with this object. More...
 
const Jsonget_data ()
 Get a reference to data of this object. More...
 
Set< Strget_keys ()
 Get keys of the data of this object. More...
 
Json make_reduced_data (const Json &key_)
 Get reduced data of this object. More...
 
template<typename T >
void make_1darray_from_data (T &array, const Str &key)
 Extract values of a property in data as a one-dimensional Eigen-Array. More...
 
template<typename T >
void make_2darray_from_data (T &array, const Vec< Str > &keys)
 Extract values of properties in data as a two-dimensional Eigen-Array. More...
 
void update_keys ()
 Update datakeys. This method must be called after setting new properties to data. More...
 
void check_required_keys (const Json &key_)
 Check if this object has required key(s). More...
 
bool check_optional_keys (const Json &key_)
 Check if this object has optional key(s). More...
 
const Jsonget_data_py ()
 Get a reference to data of this object. More...
 
Set< Strget_keys_py ()
 Get keys of the data of this object. More...
 
ArrayXi get_1d_int_py (const Str &key)
 Get integer values stored in Element::data of this object as a one-dimensional Numpy-Array. More...
 
ArrayXd get_1d_float_py (const Str &key)
 Get float values stored in Element::data of this object as a one-dimensional Numpy-Array. More...
 
ArrayXXi get_2d_int_py (const py::args &args)
 Get integer values stored in Element::data of this object as a two-dimensional Numpy-Array. More...
 
ArrayXXd get_2d_float_py (const py::args &args)
 Get float values stored in Element::data of this object as a two-dimensional Numpy-Array. More...
 
- Public Member Functions inherited from Generator
void book_to_generate_data ()
 Appoint to this Generator object. More...
 
void generate_data ()
 Hello to this Generator object. More...
 
void finish_using_generated_data ()
 Goodbye to this object. More...
 
const Vec< UpdatePair > & get_update_chain ()
 Get update_chain of this object. More...
 

Private Member Functions

void increment_bookings ()
 
void decrement_bookings ()
 
void update_data (const ShPtr< Updater > &upd)
 
Vec< std::pair< Str, int > > get_distances_between_keys (const Json &key_)
 To speed up accessing values in Json. More...
 
void init_for_python ()
 

Private Attributes

int n_bookings = 0
 
int elementid
 
Json data
 
Vec< Strdatakeys
 
Str accessing_classname
 
omp_lock_t omp_lock
 

Static Private Attributes

static int n_element_instances = 0
 

Friends

class Generator
 To use increment_bookings, decrement_bookings and update_data.
 

Additional Inherited Members

- Protected Member Functions inherited from Generator
void merge_update_chain (const Vec< UpdatePair > &new_chain)
 
- Protected Attributes inherited from Generator
Vec< UpdatePairupdate_chain
 

Detailed Description

Element inherits Generator class and contains data element (or entity) as a Json object.

This class inherits Generator class and std::enable_shared_from_this<Element>.

Element class is different from Generator, GenDict and GenList by owning data element (or entity). The data is stored as data, and a set of keys for properties in the data is stored as datakeys. Note that data can be either an array Json object or a non-array Json object. A non-array Json object consists of items: pairs of a string key and int/bool/double/string/array value. The key represents a property name. For array data, it contains non-array Json objects. Each non-array Json object should have the same set of keys; this set is the one stored as datakeys.

An object of this class can update itself by calling its update_data where a shared pointer of this object is passed to an Updater object paired as UpdatePair.

About usage in Python, please see pybind::py_element.

Definition at line 42 of file element.h.

Constructor & Destructor Documentation

◆ Element()

Element::Element ( )

Constructor of Element class.

This constructor is thread-unsafe because it accesses its members thread-globally. Ensure this constructor is not called in a multithreading context.

Definition at line 22 of file element.cpp.

Member Function Documentation

◆ accessed_by_instance_of()

void Element::accessed_by_instance_of ( const Str classname)
overridevirtual

Set class name of an instance calling Element::check_required_keys.

Parameters
classnameA string to be assigned to Element::accessing_classname.
Returns
None.

Implements Generator.

Reimplemented in PyElement.

Definition at line 146 of file element.cpp.

◆ append_updater()

ElPtr Element::append_updater ( const ShPtr< Updater > &  upd)

Append an Updater object to this Element object: the only way to extend an updating process associated with this object.

Parameters
updAn Adder or Filter object to be paired with this Element object as an UpdatePair object, which is appended to a sequence of UpdatePair objects describing the updating process of this object.
Returns
This Element object.

To update the data, one needs to create an Updater object and then appends it to an Element object using this method. In this method, Generator::update_chain stored in Updater::ext_generator of the upd is merged to Generator::update_chain of this object; then an UpdatePair consisting of this object and the upd is appended to Generator::update_chain of this object.

Definition at line 157 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_optional_keys()

bool Element::check_optional_keys ( const Json key_)

Check if this object has optional key(s).

Parameters
key_A Json object for either a key or an array of keys for optional property.
Returns
A boolean.

If this object has the given key(s), it returns true. If not, it returns false.

Definition at line 315 of file element.cpp.

◆ check_required_keys()

void Element::check_required_keys ( const Json key_)

Check if this object has required key(s).

Parameters
key_A Json object for either a key or an array of keys for required property.
Returns
None.

If this object does not have the given required key(s), a runtime error is thrown in C++ (and also raised in Python).

Definition at line 284 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decrement_bookings()

void Element::decrement_bookings ( )
private

This method decrements n_bookings.

Returns
None.

If one increments n_bookings of this object before using data and then decrements it after using it, the data is cleared to save memory. This system is convenient to access data multiple times while saving memory.

Definition at line 39 of file element.cpp.

Here is the call graph for this function:

◆ get_1d_float_py()

ArrayXd Element::get_1d_float_py ( const Str key)

Get float values stored in Element::data of this object as a one-dimensional Numpy-Array.

Parameters
keyA string key specifying a property.
Returns
ArrayXd (Numpy-Array in Python).

Definition at line 366 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_1d_int_py()

ArrayXi Element::get_1d_int_py ( const Str key)

Get integer values stored in Element::data of this object as a one-dimensional Numpy-Array.

Parameters
keyA string key specifying a property.
Returns
ArrayXi (Numpy-Array in Python).

Definition at line 351 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_2d_float_py()

ArrayXXd Element::get_2d_float_py ( const py::args &  args)

Get float values stored in Element::data of this object as a two-dimensional Numpy-Array.

Parameters
*argsAn ordered list of keys specifying properties (Python's variable number arguments).
Returns
ArrayXXd (Numpy-Array in Python).

Definition at line 399 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_2d_int_py()

ArrayXXi Element::get_2d_int_py ( const py::args &  args)

Get integer values stored in Element::data of this object as a two-dimensional Numpy-Array.

Parameters
*argsAn ordered list of keys specifying properties (Python's variable number arguments).
Returns
ArrayXXi (Numpy-Array in Python).

Definition at line 381 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_data()

const Json & Element::get_data ( )

Get a reference to data of this object.

Returns
Constant reference to this Element::data.

One can refer the whole data stored in this object by this method.

Definition at line 174 of file element.cpp.

◆ get_data_py()

const Json & Element::get_data_py ( )

Get a reference to data of this object.

Returns
Constant reference to this Element::data.

One can refer the whole data stored in this object by this method.

Definition at line 333 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_distances_between_keys()

Vec< std::pair< Str, int > > Element::get_distances_between_keys ( const Json key_)
private

To speed up accessing values in Json.

Returns
List of pairs: key and distance in datakeys.

The pairs are sorted by their keys in the same order with datakeys. The i th distance of the list is measured from the i-1 th key to i th key in datakeys (distance from the beginning for the first item).

Definition at line 96 of file element.cpp.

Here is the caller graph for this function:

◆ get_element()

ElPtr Element::get_element ( const Json name = nullptr)
overridevirtual

Get this object as a shared pointer of Element class.

Parameters
nameLeave it as it is (default is nullptr).
Returns
Shared pointer to this Element object.

One needs to get as Element when using this data.

Implements Generator.

Reimplemented in PyElement.

Definition at line 120 of file element.cpp.

Here is the call graph for this function:

◆ get_generator()

ShPtr< Generator > Element::get_generator ( const Json name = nullptr)
overridevirtual

Get this object as a shared pointer of Generator class.

Parameters
nameLeave it as it is (default is nullptr).
Returns
Shared pointer to this object up-casted to Generator.

One needs to get as Generator when storing this object.

Implements Generator.

Reimplemented in PyElement.

Definition at line 133 of file element.cpp.

Here is the call graph for this function:

◆ get_keys()

Set< Str > Element::get_keys ( )

Get keys of the data of this object.

Returns
Set of keys stored in datakeys.

One can refer the keys (property names) in this object by this method.

Definition at line 181 of file element.cpp.

Here is the caller graph for this function:

◆ get_keys_py()

Set< Str > Element::get_keys_py ( )

Get keys of the data of this object.

Returns
Set of keys stored in datakeys.

One can refer the keys (property names) in this object by this method.

Definition at line 342 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ increment_bookings()

void Element::increment_bookings ( )
private

This method increments n_bookings.

Returns
None.

If one increments n_bookings of this object before using data and then decrements it after using it, the data is cleared to save memory. This system is convenient to access data multiple times while saving memory.

Definition at line 31 of file element.cpp.

◆ init_for_python()

void Element::init_for_python ( )
private

Routines to be conducted when a wrapper of getter is called from Python.

Returns
None.

This method runs updating process associated with this object so that a wrapper of getter can access the filled data.

Definition at line 417 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_1darray_from_data()

template<typename T >
void Element::make_1darray_from_data ( T &  array,
const Str key 
)

Extract values of a property in data as a one-dimensional Eigen-Array.

Parameters
arrayMutable reference to ArrayXi or ArrayXd.
keyA string key for a property to be extracted.
Returns
None.

If one passed ArrayXi or ArrayXd as a reference, this method fills it by values of a property specified by key.

Definition at line 218 of file element.cpp.

Here is the caller graph for this function:

◆ make_2darray_from_data()

template<typename T >
void Element::make_2darray_from_data ( T &  array,
const Vec< Str > &  keys 
)

Extract values of properties in data as a two-dimensional Eigen-Array.

Parameters
arrayMutable reference to ArrayXXi or ArrayXXd.
keysList of string keys for properties to be extracted.
Returns
None.

If one passed ArrayXXi or ArrayXXd as a reference, this method fills it by values of some properties specified by keys.

Definition at line 238 of file element.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_reduced_data()

Json Element::make_reduced_data ( const Json key_)

Get reduced data of this object.

Parameters
key_Either a key or an array of keys.
Returns
Reduced data (not a reference).

Returned Json object has items whose key is or is in key_ only.

Note
If you'd like to modify (sorting etc.) contents of data, please use this method insted of get_data.

Definition at line 188 of file element.cpp.

Here is the call graph for this function:

◆ update_data()

void Element::update_data ( const ShPtr< Updater > &  upd)
private

This method updates data of this object.

Parameters
updShared pointer to an Updater object updating the data.
Returns
None.

This method updates data using an object of Updater taken as upd. This object and the Updater object are paired as UpdatePair by append_updater in advance. In this method, a shared pointer of this object is passed to Updater::compute of the Updater object.

Definition at line 84 of file element.cpp.

◆ update_keys()

void Element::update_keys ( )

Update datakeys. This method must be called after setting new properties to data.

Returns
None.

If data is an array, only the first element is used for investigating keys.

Definition at line 272 of file element.cpp.

Here is the caller graph for this function:

Member Data Documentation

◆ accessing_classname

Str Element::accessing_classname
private

Class name of instance accessing this Element instance. The name is shown in error messages thrown by check_required_keys.

Definition at line 79 of file element.h.

◆ data

Json Element::data
private

Data element (or entity) itself. This member is a Json object, which is a very flexible data container consisting of hierarchically organized items: pairs of a string key and int/bool/double/string/array value. Thanks to this flexibility, this class can store data for box, atoms, molecules, beads, cube, ... and more!!

Definition at line 69 of file element.h.

◆ datakeys

Vec<Str> Element::datakeys
private

List of keys of the data. This member stores string keys which data has.

Definition at line 74 of file element.h.

◆ elementid

int Element::elementid
private

Unique ID for an object of Element class. This member is used for preventing duplication of updating process of data. For more details, please see Updater class.

Definition at line 60 of file element.h.

◆ n_bookings

int Element::n_bookings = 0
private

The number of times this object will be used in the future. This member can be incremented by increment_bookings and decremented by decrement_bookings. If this member becomes 0 in decrement_bookings, data is cleared to save memory.

Definition at line 54 of file element.h.

◆ n_element_instances

int Element::n_element_instances = 0
staticprivate

The number of created objects (a.k.a. instances) of Element class. This member is only used for setting elementid.

Definition at line 47 of file element.h.

◆ omp_lock

omp_lock_t Element::omp_lock
private

A variable used by OpenMP. In this program, data is updated in a multithreading context. To prevent the data from being updated from multiple threads at the same time, this member must be locked in update_data, increment_bookings and decrement_bookings.

Definition at line 86 of file element.h.


The documentation for this class was generated from the following files: