@@ -185,6 +185,56 @@ class DampingMatrixExpression
185185 PrintTo (self.m_dampings , os);
186186 }
187187
188+ /* *
189+ * serialize this.
190+ * @see epi::serialize
191+ */
192+ template <class IOContext >
193+ void serialize (IOContext& io) const
194+ {
195+ auto obj = io.create_object (" DampingMatrixExpression" );
196+ obj.add_element (" Baseline" , get_baseline ());
197+ obj.add_element (" Minimum" , get_minimum ());
198+ obj.add_list (" Dampings" , get_dampings ().begin (), get_dampings ().end ());
199+ }
200+
201+ protected:
202+ /* *
203+ * deserialize an object of a class derived from this.
204+ */
205+ template <class IOContext , class Derived >
206+ static IOResult<Derived> deserialize (IOContext& io, Tag<Derived>)
207+ {
208+ auto obj = io.expect_object (" DampingMatrixExpression" );
209+ auto b = obj.expect_element (" Baseline" , Tag<Matrix>{});
210+ auto m = obj.expect_element (" Minimum" , Tag<Matrix>{});
211+ auto d = obj.expect_list (" Dampings" , Tag<typename DampingsType::value_type>{});
212+ return apply (io, [](auto && b_, auto && m_, auto && d_) -> IOResult<Derived> {
213+ if (Shape::get_shape_of (b_) != Shape::get_shape_of (m_)) {
214+ return failure (StatusCode::InvalidValue, " Baseline and Minimum must have the same shape." );
215+ }
216+ auto r = Derived (b_, m_);
217+ for (auto && e : d_) {
218+ if (e.get_shape () != Shape::get_shape_of (b_)) {
219+ return failure (StatusCode::InvalidValue, " Dampings must have the same shape as the Baseline." );
220+ }
221+ r.add_damping (e);
222+ }
223+ return success (r);
224+ }, b, m, d);
225+ }
226+
227+ public:
228+ /* *
229+ * deserialize an object of this class.
230+ * @see epi::deserialize
231+ */
232+ template <class IOContext >
233+ static IOResult<DampingMatrixExpression> deserialize (IOContext& io)
234+ {
235+ return deserialize (io, Tag<DampingMatrixExpression>{});
236+ }
237+
188238private:
189239 Matrix m_baseline;
190240 Matrix m_minimum;
@@ -231,6 +281,14 @@ class DampingMatrixExpressionGroup
231281 assert (il.size () > 0 );
232282 }
233283
284+ private:
285+ DampingMatrixExpressionGroup (const std::vector<value_type>& v)
286+ : m_matrices(v)
287+ {
288+ assert (v.size () > 0 );
289+ }
290+
291+ public:
234292 /* *
235293 * access one matrix.
236294 */
@@ -351,6 +409,57 @@ class DampingMatrixExpressionGroup
351409 }
352410 }
353411
412+ /* *
413+ * serialize this.
414+ * @see epi::serialize
415+ */
416+ template <class IOContext >
417+ void serialize (IOContext& io) const
418+ {
419+ auto obj = io.create_object (" DampingMatrixExpressionGroup" );
420+ obj.add_list (" Matrices" , begin (), end ());
421+ }
422+
423+ protected:
424+ /* *
425+ * deserialize an object of a class derived from this.
426+ * @see epi::deserialize
427+ */
428+ template <class IOContext , class Derived >
429+ static IOResult<Derived> deserialize (IOContext& io, Tag<Derived>)
430+ {
431+ auto obj = io.expect_object (" DampingMatrixExpressionGroup" );
432+ auto m = obj.expect_list (" Matrices" , Tag<value_type>{});
433+ return apply (
434+ io,
435+ [](auto && m_) -> IOResult<Derived> {
436+ // validation
437+ if (m_.empty ()) {
438+ return failure (StatusCode::InvalidValue, " DampingMatrixExpressionGroup must have at least one matrix." );
439+ }
440+ auto shape = m_[0 ].get_shape ();
441+ for (size_t i = 1 ; i < m_.size (); ++i) {
442+ if (m_[i].get_shape () != shape) {
443+ return failure (StatusCode::InvalidValue, " Elements of DampingMatrixExpressionGroup must all have the same shape." );
444+ }
445+ }
446+
447+ return success (Derived (m_));
448+ },
449+ m);
450+ }
451+
452+ public:
453+ /* *
454+ * deserialize an object of this class.
455+ * @see epi::deserialize
456+ */
457+ template <class IOContext >
458+ static IOResult<DampingMatrixExpressionGroup> deserialize (IOContext& io)
459+ {
460+ return deserialize (io, Tag<DampingMatrixExpressionGroup>{});
461+ }
462+
354463private:
355464 std::vector<value_type> m_matrices;
356465};
@@ -380,6 +489,16 @@ class ContactMatrix : public DampingMatrixExpression<SquareDampings>
380489 Eigen::Index get_num_groups () const
381490 {
382491 return Base::get_shape ().rows ();
492+ }
493+
494+ /* *
495+ * deserialize an object of this class.
496+ * @see epi::deserialize
497+ */
498+ template <class IOContext >
499+ static IOResult<ContactMatrix> deserialize (IOContext& io)
500+ {
501+ return Base::deserialize (io, Tag<ContactMatrix>{});
383502 }
384503};
385504
@@ -401,6 +520,16 @@ class ContactMatrixGroup : public DampingMatrixExpressionGroup<ContactMatrix>
401520 {
402521 return (*this )[0 ].get_num_groups ();
403522 }
523+
524+ /* *
525+ * deserialize an object of this class.
526+ * @see epi::deserialize
527+ */
528+ template <class IOContext >
529+ static IOResult<ContactMatrixGroup> deserialize (IOContext& io)
530+ {
531+ return Base::deserialize (io, Tag<ContactMatrixGroup>{});
532+ }
404533};
405534
406535} // namespace epi
0 commit comments