1

/* test */

2

#include "gen.h"

3

#include "table.h"

4

#include "nrroutines.h"

5


6

#define N 9

7

extern "C" void free_vector(float *data, int nl, int nh);

8

extern "C" float *vector(int nl, int nh);

9


10


11

int trombone(TROMB_STRUCT *tune_set);

12

/* Notes:

13

1. inputs >delta_q[2] for h/v

14

outputs >quad_trim[NKNOB] for NKNOB quad trims

15

all other members of the struct are inrelevent in the calc.

16

2. Calc results  dQh,dQv > I(quad i) is linear(a straight line fit)

17

*/

18

/************************************************************************************************/

19

/* obtain a new setting for the desired tunes by solving the equations obtained in

20

* the program in Mathematic. SVD(Singular Value Decomposition) is used to solve the

21

* equations.

22

* It turns out that 9 families of the trombone quads need to be adjusted independently. There are

23

* 6 constrains: betx, alfx, bety, alfy, at two ends of the trombone section, and d_vx, d_vy for

24

* the desired tunes.

25

* 9 veriables: K1, K2,K3,K4, K5,K6,K7,K8,K9(I=K/L/0.002976536)

26

* the 6X9 matrix has been obained from Mathematica program (tromb_fullsection9_2ORM.nb),

27

*

28

* { 26.0506, 831.49, 59.5737, 749.714, 27.5116, 868.038, 36.4185, 773.266, 12.0946},

29

* { 17.5588, 23.5745, 18.766, 23.7522, 17.585, 8.67871, 19.3967, 10.1126, 17.3344},

30

* {2304.8, 756.055, 1738.19 , 833.257, 1243.97, 829.713, 458.44, 881.6, 67.0388},

31

* { 81.5639, 8.81639, 85.9938, 6.52632 91.2513, 3.92004, 93.7124, 1.34981,95.9362},

32

* { 1.41081, 7.9777, 1.55587, 7.25293, 1.41061, 8.0431, 1.55596, 7.193, 1.38011},

33

* { 7.64076, 1.41846, 7.5213, 1.45272, 7.60481, 1.41309, 7.53389, 1.46471, 7.61725}},

34

*

35

* the right hand side is

36

* {0.00203665, 0.000291693, 0.156029, 0.000528379, d_Vx,d_vy}

37

* Meiqin Xiao, 05/08/2007

38

************************************************************************************************/

39


40

/************************************************************************************************/

41

/* This is for the new trombone section with 4 trim quads in a group for 601, 602,...609

42

* meiqin xiao, 09/07/2007

43

* obtain a new setting for the desired tunes by solving the equations obtained in

44

* the program in Mathematic. SVD(Singular Value Decomposition) is used to solve the

45

* equations.

46

* It turns out that 9 families of the trombone quads need to be adjusted independently. There are

47

* 6 constrains: betx, alfx, bety, alfy, at two ends of the trombone section, and d_vx, d_vy for

48

* the desired tunes.

49

* 9 veriables: K1, K2,K3,K4, K5,K6,K7,K8,K9(I=K/L/0.002976536)

50

* the 6X9 matrix has been obained from Mathematica program (tromb_fullsection9_2ORM.nb),

51

*

52

* {140.38, 1688.13, 187.477, 1449.79, 74.7654, 1810.12, 104.737, 1521.67, 49.9376}

53

* {33.8252, 68.4684, 35.4021, 50.3818, 36.5726, 37.2133, 37.8437, 26.0246, 35.9651},

54

* {5255.4, 1397.95, 3860.68, 1655.56, 2661.18, 1612.78, 1038.58, 1731.92, 267.428},

55

* {156.345, 21.1427, 170.255, 12.4532, 181.473, 11.8655,188.58, 6.90345, 191.288},

56

* {2.81678, 16.2875, 3.08217, 14.2611, 2.8848, 16.3927, 3.05359, 14.1942, 2.82223},

57

* {15.3701, 2.84064, 15.1444, 2.91699, 15.178, 2.83305, 15.182, 2.94246, 15.1916}},

58

*

59

* Singular values = {7991.96,2811.64,258.74,37.7007,30.575,23.7899}

60

* the right hand side is

61

*

62

* {0.104264, 0.018209, 0.173774, 0.00289952, dv_x, dv_y}}

63

*

64

* Meiqin Xiao, 09/07/2007

65

************************************************************************************************/

66

/************************************************************************************************/

67

/* This is for the new trombone section with 4 trim quads in a group for 601, 602,...609

68

* meiqin xiao, Updated on 10/07/2008

69

* found wierd error during a file transfering format on NewTM_4quad_09072007, resulting mimor

70

* changes in the coefficients of the matrix. Update the Console program durin 2008 October

71

* Shutdown. Meiqin Xiao, 10/07/2008

72

*

73

* obtain a new setting for the desired tunes by solving the equations obtained in

74

* the program in Mathematic. SVD(Singular Value Decomposition) is used to solve the

75

* equations.

76

* It turns out that 9 families of the trombone quads need to be adjusted independently. There are

77

* 6 constrains: betx, alfx, bety, alfy, at two ends of the trombone section, and d_vx, d_vy for

78

* the desired tunes.

79

* 9 veriables: K1, K2,K3,K4, K5,K6,K7,K8,K9(I=K/L/0.002976536)

80

* the 6X9 matrix has been obained from Mathematica program (tromb_fullsection9_2ORM.nb),

81

*

82

* {128.39, 1598.69, 162.702, 1460.88, 72.362, 1708.54, 82.1163, 1524.1, 48.2942}

83

* {32.9211, 62.243, 36.4847, 56.764, 35.2285, 31.0953, 38.943, 32.4888, 34.6596},

84

* {5183.72, 1391.87, 3870.19, 1642.90, 2646.52, 1606.33, 1064.32, 1719.30, 266.015},

85

* {155.951, 20.9515, 171.254, 12.6403, 180.476, 11.6747,189.573, 7.0916, 190.283},

86

* {2.76783, 15.9378, 3.12461, 14.5062, 2.82823, 16.074, 3.11296, 14.4111, 2.76134},

87

* {15.2873, 2.84634, 15.0437, 2.91068, 15.2113, 2.83732, 15.10694, 2.93703, 15.2331}},

88

*

89

* Singular values = {7927.03,2736.67,256.194,38.7208,27.5055,25.5385}

90

* the right hand side is

91

*

92

* {0.0106982, 0.00289685, 0.208795, 0.00382239, dv_x, dv_y}}

93

*

94

* Meiqin Xiao, 09/07/2007

95

************************************************************************************************/

96


97

/************************************************************************************************/

98

/* This is the correction of the coefficients for adjusting the trim quads settings in trombone

99

*section after 2009 Summer Shutdown. The closed orbit has changed, resulting in base tune changed

100

*from (0.417, 0.427) to (0.427,0.436), and betafunction changed about ~5%. Based on this measured

101

* batafunctions at 2 ends(MRK601 and MRK609) and the transfer matrices of the trombone section,

102

* the new coefficents for tunning the tunes has calculated and updated here.

103

* Meiqin Xiao, Nov.17,2009

104

*

105

* obtain a new setting for the desired tunes by solving the equations obtained in

106

* the program in Mathematic. SVD(Singular Value Decomposition) is used to solve the

107

* equations.

108

* It turns out that 9 families of the trombone quads need to be adjusted independently. There are

109

* 6 constrains: betx, alfx, bety, alfy, at two ends of the trombone section, and d_vx, d_vy for

110

* the desired tunes.

111

* 9 veriables: K1, K2,K3,K4, K5,K6,K7,K8,K9(I=K/L/0.002976536)

112

* the 6X9 matrix has been obained from Mathematica program (tromb_fullsection9_2ORM.nb),

113

*

114

* {122.497, 1615.65, 135.61, 1588.11, 65.9322, 1704.44, 47.0098, 1651.3, 49.7233}

115

* {34.1466, 59.8896, 36.2102, 52.1996, 35.2765, 27.7801, 38.4453, 26.4248, 35.546},

116

* {5480.38, 1461.7, 3851.28 , 1761.54, 2908.44, 1692.79, 1313.27, 1788.4, 280.657},

117

* {167.666, 21.7508, 162.204, 9.72964, 191.958, 13.9525,177.936, 4.82235, 200.808},

118

* {2.87133, 15.4478, 3.06023, 14.9975, 2.84258, 15.4768, 3.06936, 14.9874, 2.84597 },

119

* {15.9904, 2.88911, 14.3305, 2.90138, 16.001, 2.90307, 14.3319, 2.86806, 15.9923},

120

*

121

* Singular values = {8316.86,2860.86,250.133,38.3942,30.0983,24.16}

122

* the right hand side is

123

*

124

* {0.00258, 0.0000593, 0.0001, 0.00037726, dv_x, dv_y}}

125

*

126

* Meiqin Xiao, 09/07/2007

127

************************************************************************************************/

128


129


130

/* trombone program by solving the equations using SVD*/

131

int trombone(TROMB_STRUCT *tune_set)

132

{

133

int sts;

134

/* float b[9],u[9][9],w[9],v[9][9],x[9];*/

135


136

//float value;

137

//char numstr[9];

138


139


140

int i,j,ii;

141

float wmax,wmin;

142


143

float **u;

144

float **v;

145

float *w;

146

float *b;

147

float *x;

148


149

/* static float a[5][5]={

150

{ 40.33470, 44.4282, 32.6486, 9.4943, 19.9027},

151

{167.60400, 7.29578, 183.006, 3.73881, 87.5826},

152

{ 3.23443, 15.729, 2.63187, 15.0529, 1.73248},

153

{ 14.86990, 2.66075, 15.712, 3.10726, 7.18887},

154

{0. , 0. , 0. , 0. , 0.}

155

};*/

156


157

/****** the following asignment was for the old trombone section with 2 trim quads in a group********

158

* u=create_matrix(1,N, 1,N);

159

* u[1][1]= 26.0506;

160

* u[1][2]= 831.49;

161

* u[1][3]= 59.5737;

162

* u[1][4]= 749.714;

163

* u[1][5]= 27.5116;

164

* u[1][6]= 868.038;

165

* u[1][7]= 36.4185;

166

* u[1][8]= 773.266;

167

* u[1][9]= 12.0946;

168

*

169

* u[2][1]= 17.5588;

170

* u[2][2]= 23.5745 ;

171

* u[2][3]= 18.766;

172

* u[2][4]= 23.7522;

173

* u[2][5]= 17.585;

174

* u[2][6]= 8.67871;

175

* u[2][7]= 19.3967;

176

* u[2][8]= 10.1126;

177

* u[2][9]= 17.3344;

178

*

179

* u[3][1]= 2304.8;

180

* u[3][2]= 756.055;

181

* u[3][3]= 1738.19;

182

* u[3][4]= 833.257;

183

* u[3][5]= 1243.97;

184

* u[3][6]= 829.713;

185

* u[3][7]= 458.44;

186

* u[3][8]= 881.6;

187

* u[3][9]= 67.0388;

188

*

189

* u[4][1]=81.5639 ;

190

* u[4][2]= 8.81639;

191

* u[4][3]= 85.9938;

192

* u[4][4]= 6.52632;

193

* u[4][5]= 91.2513;

194

* u[4][6]= 3.92004;

195

* u[4][7]= 93.7124;

196

* u[4][8]= 1.34981;

197

* u[4][9]= 95.9362;

198

*

199

* u[5][1]= 1.41081;

200

* u[5][2]= 7.9777;

201

* u[5][3]= 1.55587;

202

* u[5][4]= 7.25293;

203

* u[5][5]= 1.41061;

204

* u[5][6]= 8.0431;

205

* u[5][7]= 1.55596;

206

* u[5][8]= 7.193;

207

* u[5][9]= 1.38011;

208

*

209

* u[6][1]=7.64076 ;

210

* u[6][2]= 1.41846;

211

* u[6][3]= 7.5213;

212

* u[6][4]= 1.45272;

213

* u[6][5]= 7.60481;

214

* u[6][6]= 1.41309;

215

* u[6][7]= 7.53389;

216

* u[6][8]= 1.46471;

217

* u[6][9]= 7.61725;

218

*

219

* u[7][1]=0.;

220

* u[7][2]=0.;

221

* u[7][3]=0.;

222

* u[7][4]=0.;

223

* u[7][5]=0.;

224

* u[7][6]=0.;

225

* u[7][7]=0.;

226

* u[7][8]=0.;

227

* u[7][9]=0.;

228

*

229

* u[8][1]=0.;

230

* u[8][2]=0.;

231

* u[8][3]=0.;

232

* u[8][4]=0.;

233

* u[8][5]=0.;

234

* u[8][6]=0.;

235

* u[8][7]=0.;

236

* u[8][8]=0.;

237

* u[8][9]=0.;

238

*

239

* u[9][1]=0.;

240

* u[9][2]=0.;

241

* u[9][3]=0.;

242

* u[9][4]=0.;

243

* u[9][5]=0.;

244

* u[9][6]=0.;

245

* u[9][7]=0.;

246

* u[9][8]=0.;

247

* u[9][9]=0.;

248

******* Meiqin Xiao, updated on 11/17/2009******************/

249

u=create_matrix(1,N, 1,N);

250

u[1][1]= 122.497;

251

u[1][2]= 1615.65 ;

252

u[1][3]= 135.61;

253

u[1][4]=1588.11;

254

u[1][5]= 65.9322;

255

u[1][6]= 1704.44;

256

u[1][7]= 47.0098;

257

u[1][8]= 1651.3;

258

u[1][9]= 49.7233;

259


260

u[2][1]= 34.1466;

261

u[2][2]= 59.8896 ;

262

u[2][3]= 36.2102;

263

u[2][4]= 52.1996;

264

u[2][5]= 35.2765;

265

u[2][6]= 27.7801;

266

u[2][7]= 38.4453;

267

u[2][8]= 26.4248;

268

u[2][9]= 35.546;

269


270

u[3][1]= 5480.38;

271

u[3][2]= 1461.7 ;

272

u[3][3]= 3851.28;

273

u[3][4]= 1761.54;

274

u[3][5]= 2908.44;

275

u[3][6]= 1692.79;

276

u[3][7]= 1313.27;

277

u[3][8]= 1788.4;

278

u[3][9]= 280.657;

279


280

u[4][1]= 167.666;

281

u[4][2]= 21.7508;

282

u[4][3]= 162.204;

283

u[4][4]= 9.72964;

284

u[4][5]= 191.958;

285

u[4][6]= 13.9525;

286

u[4][7]= 177.936;

287

u[4][8]= 4.82235;

288

u[4][9]= 200.808;

289


290

u[5][1]= 2.87133 ;

291

u[5][2]= 15.4478;

292

u[5][3]= 3.06023 ;

293

u[5][4]= 14.9975;

294

u[5][5]= 2.84258;

295

u[5][6]= 15.4768;

296

u[5][7]= 3.06936;

297

u[5][8]= 14.9874;

298

u[5][9]= 2.84597;

299


300

u[6][1]= 15.9904;

301

u[6][2]= 2.88911;

302

u[6][3]= 14.3305;

303

u[6][4]= 2.90138;

304

u[6][5]= 16.001;

305

u[6][6]= 2.90307;

306

u[6][7]= 14.3319;

307

u[6][8]= 2.86806;

308

u[6][9]= 15.9923;

309


310

u[7][1]=0.;

311

u[7][2]=0.;

312

u[7][3]=0.;

313

u[7][4]=0.;

314

u[7][5]=0.;

315

u[7][6]=0.;

316

u[7][7]=0.;

317

u[7][8]=0.;

318

u[7][9]=0.;

319


320

u[8][1]=0.;

321

u[8][2]=0.;

322

u[8][3]=0.;

323

u[8][4]=0.;

324

u[8][5]=0.;

325

u[8][6]=0.;

326

u[8][7]=0.;

327

u[8][8]=0.;

328

u[8][9]=0.;

329


330

u[9][1]=0.;

331

u[9][2]=0.;

332

u[9][3]=0.;

333

u[9][4]=0.;

334

u[9][5]=0.;

335

u[9][6]=0.;

336

u[9][7]=0.;

337

u[9][8]=0.;

338

u[9][9]=0.;

339


340


341

/* Copy a into u if you don't want it to be destroyed.*/

342

/* for (i=1;i<=N;i++)

343

for (j=1;j<=N;j++)

344

u[i][j]=a[i][j];*/

345

sts = 1;

346


347

v=create_matrix(1,N, 1,N);

348

w=vector( 1,N);

349

b=vector( 1,N);

350

x=vector( 1,N);

351


352

/* The following were for the old trombone section with 2 trim quads in a group

353

*

354

* b[1]=0.00203665;

355

* b[2]=0.000291693;

356

* b[3]=0.156029;

357

* b[4]=0.000528379;

358

* b[5]=tune_set>delta_q[0];

359

* b[6]=tune_set>delta_q[1];

360

* b[7]=0;

361

* b[8]=0;

362

* b[9]=0;

363

* assign value to b vector*/

364


365

/* assign value to b vector, 11/17/2009, Meiqin Xiao*/

366


367

b[1]= 0.00258321;

368

b[2]= 0.000059317;

369

b[3]=0.000148048;

370

b[4]= 0.000377263;

371

b[5]=tune_set>delta_q[0];

372

b[6]=tune_set>delta_q[1];

373

b[7]=0;

374

b[8]=0;

375

b[9]=0;

376


377

/*SVD the square matrix a.*/

378

svdcmp(u,N,N,w,v);

379

wmax=0.0; /* will be the maximum singular value obtained.*/

380

for (j=1;j<=N;j++) if (w[j]>wmax) wmax=w[j];

381

wmin=wmax*10e6;

382

/*This is where we set the threshold for singular values allowed to be nonzero. The constant is typical , but not universal. You have to experiment with your own application.*/

383

for (j=1;j<=N;j++) if (w[j]<wmin) w[j]=0.;

384

svbksb(u,w,v,N,N,b,x);

385


386

for (i=0;i< NKNOB;i++){

387

ii=i+1;

388

tune_set>quad_trim[i]=x[ii]/0.3048/0.00297;

389

if ( tune_set>quad_trim[i] > 6.5 ) {

390

error_message_c("Err: Maximum current exceed 6.5 Amps!",

391

ERR_SIMPLE_DISPLAY,RED);

392


393

tune_set>quad_trim[i] = 6.5;

394

error_message_c("Caution: The current is repalced by 6.5 Amps!",

395

ERR_SIMPLE_DISPLAY,RED);

396

}

397

if ( tune_set>quad_trim[i] < 6.5 ) {

398

error_message_c("Err: Maximum current exceed 6.5 Amps!",

399

ERR_SIMPLE_DISPLAY,RED);

400


401

tune_set>quad_trim[i] = 6.5;

402

error_message_c("Caution: The current is repalced by 6.5 Amps!",

403

ERR_SIMPLE_DISPLAY,RED);

404

}

405

}

406

sts = 0;

407

//error_message_c("GOOD SOLUTION!",ERR_SIMPLE_DISPLAY,BLUE);

408


409

/*

410

for (i = 0; i < 5; i++) {

411

value = tune_set>quad_trim[i];

412

sprintf(numstr,"%8.3f",value);

413

window_tvm_c(win_dis_id,i+12,10,numstr,8,GREEN);

414

}

415

for (i = 5; i <9; i++) {

416

value = tune_set>quad_trim[i];

417

sprintf(numstr,"%8.3f",value);

418

window_tvm_c(win_dis_id,20i,27,numstr,8,GREEN);

419


420

}

421

*/

422


423

//memcpy(q_amps,tune_set>quad_trim,NKNOB*sizeof(float));

424


425

return (sts);

426

}

427

