1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora, Radu Serban
13 // =============================================================================
14 
15 #include "chrono/motion_functions/ChFunction_Poly.h"
16 
17 namespace chrono {
18 
19 // Register into the object factory, to enable run-time dynamic creation and persistence
CH_FACTORY_REGISTER(ChFunction_Poly)20 CH_FACTORY_REGISTER(ChFunction_Poly)
21 
22 ChFunction_Poly::ChFunction_Poly() {
23     order = 0;
24     for (int i = 0; i < POLY_COEFF_ARRAY; i++) {
25         coeff[i] = 0;
26     }
27 }
28 
ChFunction_Poly(const ChFunction_Poly & other)29 ChFunction_Poly::ChFunction_Poly(const ChFunction_Poly& other) {
30     order = other.order;
31     for (int i = 0; i < POLY_COEFF_ARRAY; i++) {
32         coeff[i] = other.coeff[i];
33     }
34 }
35 
Get_y(double x) const36 double ChFunction_Poly::Get_y(double x) const {
37     double total = 0;
38     for (int i = 0; i <= order; i++) {
39         total += (coeff[i] * pow(x, (double)i));
40     }
41     return total;
42 }
43 
Get_y_dx(double x) const44 double ChFunction_Poly::Get_y_dx(double x) const {
45     double total = 0;
46     for (int i = 1; i <= order; i++) {
47         total += ((double)i * coeff[i] * pow(x, ((double)(i - 1))));
48     }
49     return total;
50 }
51 
Get_y_dxdx(double x) const52 double ChFunction_Poly::Get_y_dxdx(double x) const {
53     double total = 0;
54     for (int i = 2; i <= order; i++) {
55         total += ((double)(i * (i - 1)) * coeff[i] * pow(x, ((double)(i - 2))));
56     }
57     return total;
58 }
59 
ArchiveOUT(ChArchiveOut & marchive)60 void ChFunction_Poly::ArchiveOUT(ChArchiveOut& marchive) {
61     // version number
62     marchive.VersionWrite<ChFunction_Poly>();
63     // serialize parent class
64     ChFunction::ArchiveOUT(marchive);
65     // serialize all member data:
66     marchive << CHNVP(coeff);
67     marchive << CHNVP(order);
68 }
69 
ArchiveIN(ChArchiveIn & marchive)70 void ChFunction_Poly::ArchiveIN(ChArchiveIn& marchive) {
71     // version number
72     /*int version =*/ marchive.VersionRead<ChFunction_Poly>();
73     // deserialize parent class
74     ChFunction::ArchiveIN(marchive);
75     // stream in all member data:
76     marchive >> CHNVP(coeff);
77     marchive >> CHNVP(order);
78 }
79 
80 }  // end namespace chrono
81