-
Notifications
You must be signed in to change notification settings - Fork 447
/
demo_CH_functions.py
144 lines (116 loc) · 4.21 KB
/
demo_CH_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# =============================================================================
# PROJECT CHRONO - http://projectchrono.org
#
# Copyright (c) 2014 projectchrono.org
# All rights reserved.
#
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file at the top level of the distribution and at
# http://projectchrono.org/license-chrono.txt.
#
# =============================================================================
import pychrono as chrono
import errno
import os
import math
#------------------------------------------------------------------------------
# Define a custom function
class MyFunction(chrono.ChFunction):
def GetVal(self, x):
y = math.cos(math.pi * x)
return y
#------------------------------------------------------------------------------
print ('Demonstration of functions for y = f(x)')
# Create the output directory
out_dir = os.path.join(os.path.dirname(__file__), "Functions_demo")
try:
os.mkdir(out_dir)
except OSError as exc:
if exc.errno != errno.EEXIST:
print("Error creating output directory " )
# Ramp function
# -------------
f_ramp = chrono.ChFunctionRamp()
f_ramp.SetAngularCoeff(0.1) # angular coefficient
f_ramp.SetStartVal(0.1) # y value at x = 0
# Evaluate the function and its first derivative at a given value
y = f_ramp.GetVal(10)
yd = f_ramp.GetDer(10)
print(' Ramp function f(10) = ', y, ', 1st derivative df/dx(10) = ', yd)
# Sine function
# -------------
f_sine = chrono.ChFunctionSine()
f_sine.SetAmplitude(2) # amplitude
f_sine.SetFrequency(1.5) # frequency
# Evaluate the function and its derivatives at 101 points in [0,2] and write to file
sine_file = open(out_dir + "/f_sine.out","w+")
for i in range(101):
x = i / 50.0
y = f_sine.GetVal(x)
yd = f_sine.GetDer(x)
ydd = f_sine.GetDer2(x)
sine_file.write("%f %f %f %f\n" % (x, y, yd, ydd))
sine_file.close()
# Custom function
# ---------------
# Create a custom function for y = f(pi*x)
f_test = MyFunction()
# Evaluate the function and its derivatives at 101 points in [0,2] and write to file
test_file = open(out_dir + "/f_test.out", "w+")
for i in range(101):
x = i / 50.0
y = f_test.GetVal(x)
yd = f_test.GetDer(x)
ydd = f_test.GetDer2(x)
test_file.write("%f %f %f %f\n" % (x, y, yd, ydd))
test_file.close()
# Function sequence
# -----------------
f_seq = chrono.ChFunctionSequence()
f_const_acc1 = chrono.ChFunctionConstAcc()
f_const_acc1.SetDuration(0.5) # ramp length
f_const_acc1.SetDisplacement(0.3) # ramp height
f_seq.InsertFunct(f_const_acc1, 0.5, 1, False, False, False, 0)
f_const = chrono.ChFunctionConst()
f_seq.InsertFunct(f_const, 0.4, 1, True, False, False, -1)
f_const_acc2 = chrono.ChFunctionConstAcc()
f_const_acc2.SetDuration(0.6) # ramp length
f_const_acc2.SetFirstAccelerationEnd(0.3) # acceleration ends after 30% length
f_const_acc2.SetSecondAccelerationStart(0.7) # deceleration starts after 70% length
f_const_acc2.SetDisplacement(-0.2) # ramp height
f_seq.InsertFunct(f_const_acc2, 0.6, 1, True, False, False, -1)
f_seq.Setup();
# Evaluate the function and its derivatives at 101 points in [0,2] and write to file
seq_file = open(out_dir + "/f_seq.out", "w+")
for i in range(101):
x = i / 50.0
y = f_seq.GetVal(x)
yd = f_seq.GetDer(x)
ydd = f_seq.GetDer2(x)
seq_file.write("%f %f %f %f\n" % (x, y, yd, ydd))
seq_file.close()
# Repeating sequence
# ------------------
f_part1 = chrono.ChFunctionRamp()
f_part1.SetAngularCoeff(0.50)
f_part2 = chrono.ChFunctionConst()
f_part2.SetConstant(1.0)
f_part3 = chrono.ChFunctionRamp()
f_part3.SetAngularCoeff(-0.50)
f_seq = chrono.ChFunctionSequence()
f_seq.InsertFunct(f_part1, 1.0, 1, True)
f_seq.InsertFunct(f_part2, 1.0, 1., True)
f_seq.InsertFunct(f_part3, 1.0, 1., True)
f_rep_seq = chrono.ChFunctionRepeat(f_seq)
f_rep_seq.SetSliceWidth(3.0)
f_rep_seq.SetSliceStart(0.0)
f_rep_seq.SetSliceShift(3.0)
# Evaluate the function and its derivatives at 101 points in [0,2] and write to file
rep_file = open(out_dir + "/f_rep.out", "w+")
for i in range(1001):
x = i / 50.0
y = f_rep_seq.GetVal(x)
yd = f_rep_seq.GetDer(x)
ydd = f_rep_seq.GetDer2(x)
rep_file.write("%f %f %f %f\n" % (x, y, yd, ydd))
rep_file.close()