Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

 

import os 

from math import log10, sqrt, tan, radians 

import numpy as np 

from scipy import sparse 

 

# NOTE: requires numpy npz file: calc_full_Cd.npz in local folder (i.e. here) 

here = os.path.abspath(os.path.dirname(__file__)) 

 

speciesL = ['na'] 

 

def calc_Cd( Pc=500.0, Rthrt=1.0, RWTU=1.0 ): 

 

 

##massWtD, masseFracD = ceaObj.get_SpeciesMassFractions( Pc=Pc, MR=MR, eps=eps,  

## frozen=0, frozenAtThroat=0, min_fraction=0.000005) 

 

#_, _, TcCham, MolWt, gammaInit = ceaObj.get_IvacCstrTc_ChmMwGam( Pc=Pc, MR=MR, eps=eps) 

 

#asonic = ceaObj.get_Chamber_SonicVel( Pc=Pc, MR=MR, eps=eps) 

#tauRt = Rthrt / asonic 

 

#z100 = Rthrt * ( sqrt(eps) - 1.0 ) / tan( radians(15.) ) 

#Lnoz = z100 * pcentBell / 100.0 

#tauLnoz = Lnoz / asonic 

 

# condition Pc, eps, Rthrt, pcentBell, gammaInit, TcCham 

inpL = [log10(Pc)/4.0, (2.0+log10(Rthrt))/4.0, RWTU/3.0] 

 

 

##for sp in speciesL: 

## vL = masseFracD.get( sp, [0.,0.] ) 

## inpL.append( vL[-1] - vL[1] ) # chamber mass frac difference for species 

 

ypred = predict( np.array(inpL).reshape(1, -1) ) 

return ypred 

 

 

def predict( X ): 

activations = [X] 

layer_units = [X.shape[1]] + hidden_layer_sizes + [n_outputs_] 

 

for i in range(n_layers_ - 1): 

activations.append(np.empty((X.shape[0], layer_units[i + 1]))) 

 

# ----------- start forward pass ------------ 

hidden_activation = relu 

for i in range(n_layers_ - 1): 

activations[i + 1] = safe_sparse_dot(activations[i], 

coefs_[i]) 

activations[i + 1] += intercepts_[i] 

# For the hidden layers 

if (i + 1) != (n_layers_ - 1): 

activations[i + 1] = hidden_activation(activations[i + 1]) 

# For the last layer 

output_activation = identity 

activations[i + 1] = output_activation(activations[i + 1]) 

 

y_pred = activations[-1][-1][-1] 

ypred = max(0., min(1.0, y_pred )) 

 

ypred = 0.06 * ypred + 0.94 

 

return ypred 

 

 

def relu(X): 

"""Compute the rectified linear unit function inplace. 

Parameters 

---------- 

X : array-like, sparse matrix, shape (n_samples, n_features) 

The input data. 

Returns 

------- 

X_new : array-like, sparse matrix, shape (n_samples, n_features) 

The transformed data. 

""" 

 

np.clip(X, 0, np.finfo(X.dtype).max, out=X) 

return X 

 

def identity(X): 

"""Simply return the input array. 

Parameters 

---------- 

X : array-like, sparse matrix, shape (n_samples, n_features) 

Data, where n_samples is the number of samples 

and n_features is the number of features. 

Returns 

------- 

X : array-like, sparse matrix, shape (n_samples, n_features) 

Same as the input data. 

""" 

return X 

 

def safe_sparse_dot(a, b, *, dense_output=False): 

"""Dot product that handle the sparse matrix case correctly 

Parameters 

---------- 

a : array or sparse matrix 

b : array or sparse matrix 

dense_output : boolean, (default=False) 

When False, ``a`` and ``b`` both being sparse will yield sparse output. 

When True, output will always be a dense array. 

Returns 

------- 

dot_product : array or sparse matrix 

sparse if ``a`` and ``b`` are sparse and ``dense_output=False``. 

""" 

if a.ndim > 2 or b.ndim > 2: 

if sparse.issparse(a): 

# sparse is always 2D. Implies b is 3D+ 

# [i, j] @ [k, ..., l, m, n] -> [i, k, ..., l, n] 

b_ = np.rollaxis(b, -2) 

b_2d = b_.reshape((b.shape[-2], -1)) 

ret = a @ b_2d 

ret = ret.reshape(a.shape[0], *b_.shape[1:]) 

elif sparse.issparse(b): 

# sparse is always 2D. Implies a is 3D+ 

# [k, ..., l, m] @ [i, j] -> [k, ..., l, j] 

a_2d = a.reshape(-1, a.shape[-1]) 

ret = a_2d @ b 

ret = ret.reshape(*a.shape[:-1], b.shape[1]) 

else: 

ret = np.dot(a, b) 

else: 

ret = a @ b 

if (sparse.issparse(a) and sparse.issparse(b) 

and dense_output and hasattr(ret, "toarray")): 

return ret.toarray() 

return ret 

 

n_layers_ = 5 

n_outputs_ = 1 

 

hidden_layer_sizes = (70, 70, 70) 

hidden_layer_sizes = hidden_layer_sizes 

if not hasattr(hidden_layer_sizes, "__iter__"): 

hidden_layer_sizes = [hidden_layer_sizes] 

hidden_layer_sizes = list(hidden_layer_sizes) 

 

 

activation = 'relu' 

if activation != 'relu': 

raise Exception('Need to add activation other than relu') 

 

out_activation_ = 'identity' 

if out_activation_ != 'identity': 

raise Exception('Need to add out_activation_ other than identity') 

 

npz_filename = os.path.join( here, 'calc_full_Cd.npz') 

with np.load(npz_filename, allow_pickle=True) as data: 

coefs_ = data['c'] 

intercepts_ = data['i'] 

 

if __name__ == "__main__": 

 

#from rocketcea.cea_obj import CEA_Obj 

 

#ceaObj = CEA_Obj(oxName='N2O4', fuelName='MMH', useFastLookup=0) 

 

ypred = calc_Cd( Pc=500.0, Rthrt=1.0, RWTU=1.0 ) 

print( 'ypred Cd =', ypred )