wavecyn.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /***********************************************************/
  2. /*** ***/
  3. /*** Resistance and Inductance using Coifman wavelets ***/
  4. /*** for the method of moments ***/
  5. /*** ***/
  6. /***********************************************************/
  7. #include "stdafx.h"
  8. /********************** Global variables *******************/
  9. int Nc; /* number of conductors ********/
  10. int Die, Rec, Cir, Tri, Ell, Pol, Gnd, Tra;
  11. Matrix Gnddi;
  12. Matrix Recdi;
  13. Matrix Cirdi;
  14. Matrix Tradix; /* Trapezoid x coordinates *****/
  15. Matrix Tradiy;
  16. Vector ag, bg, cg; /* gnd parameters **************/
  17. Vector a, b, c; /* rec parameters **************/
  18. Vector at, bt, ct; /* tra parameters **************/
  19. Vector Circum; /* Circumference of conductors */
  20. Vector Diec; /* circum of die conductors ****/
  21. Vector Recc; /* circum of rec conductors ****/
  22. Vector Circ; /* circum of cir conductors ****/
  23. Vector Trac; /* circum of tra conductors ****/
  24. Vector Tric; /* circum of tri conductors ****/
  25. Vector Ellc; /* circum of ell conductors ****/
  26. Vector Polc; /* circum of pol conductors ****/
  27. Vector Gndc; /* circum of gnd conductors ****/
  28. double freq, sqrtoms,sigma;
  29. double omega,oms,omegamu;
  30. double sft;
  31. int matr, Mt, Np;
  32. int Ns, Nsr, Ngx, Ngy, Nxt, Nyt, Ngr, Nrt;
  33. int Nh, Nc4, Nit, J, Nw, Nwx, Nwy, Nws;
  34. double hsrw;
  35. double Pi, Pih, Pit, wn, mu, cf;
  36. double step_w, hw, hwh, mmax;
  37. Complex Ic;
  38. double EPS;
  39. Vector Iq_sq;
  40. double Uniti;
  41. /********************** Main program ***********************/
  42. int main()
  43. {
  44. /************ to setup global parameters *********/
  45. getparam();
  46. /************ global variables *******************/
  47. int i, j, m, n, k, n0, j1, i1, ix, i2, j2, k2;
  48. int jn, kn, jm, km, jx, iy;
  49. double dlu;
  50. double t, tp, xt, yt;
  51. Complex tmp, tmp0, sum, sum0, tmpsum, suc;
  52. Vector xcnt(Nws), ycnt(Nws);
  53. Vector ptr(Ngr), whr(Ngr), tr(Nrt), wr(Nrt);
  54. Vector twr(Nrt), wwr(Nrt);
  55. Vector twx(Nwx), wwx(Nwx), twy(Nwy), wwy(Nwy);
  56. Vector ptwx(Nwx), whwx(Nwx), ptwy(Nwy), whwy(Nwy);
  57. Vector sxc4(Nc4), sc4(Nc4), wxc4(Nc4), wc4(Nc4);
  58. Vector ts(Nw), tn(Nw);
  59. Vector Indx;
  60. Matrix uns(3,Nw);
  61. CmplxVector Rhsw(2*Nc*Nw+Nc), Jnj(2*Nc*Nw+Nc), xc;
  62. CmplxMatrix Pw(2*Nc*Nw+Nc, 2*Nc*Nw+Nc), Pwt(2*Nc*Nw+Nc,2*Nc*Nw+Nc);
  63. Matrix matout;
  64. Vector Res(Nc),Ind(Nc);
  65. Matrix Res1(Nc,Nc),Ind1(Nc,Nc);
  66. Vector sum1(Nc);
  67. CmplxVector sum2(Nc);
  68. double rsum,isum;
  69. double tmpsum1;
  70. double tmpsum2;
  71. Complex temsum;
  72. Complex tmpv0,tmpvi,tmpu0,tmpui,tmp0v0,tmp0vi,tmp0u0,tmp0ui;
  73. Complex sumv0,sumvi,sumu0,sumui;
  74. double tst;
  75. double tempc;
  76. ofstream outdata( "data.out", ios::out );
  77. cout<<"start"<<endl;
  78. getcoif4( sxc4, sc4, wxc4, wc4, Nit );
  79. getsbs( uns );
  80. /** points and weights for Gauss the method */
  81. gauleg( -1.0, 1.0, ptwx, whwx );
  82. gauleg( -1.0, 1.0, ptwy, whwy );
  83. for( i = 0; i < Nw; i++ ) {
  84. tn[i] = i*step_w;
  85. }
  86. /***** to create system matrix **************/
  87. for( i1 = 0; i1 < Nc; i1++ ) {
  88. cout<<i1<<endl;
  89. for( i = 0; i < Nw; i++ ) {
  90. for( j1 = 0; j1 < Nc; j1++ ) {
  91. for( j = 0; j < Nw; j++ ) {
  92. sumv0 = 0.0;
  93. sumu0 = 0.0;
  94. sumvi = 0.0;
  95. sumui = 0.0;
  96. if( i1 ==j1 && i == j ) {
  97. for( m = 0; m < Nws; m++ ) {
  98. xcnt[m] = uns(1,j)+hw*(double(m)+0.5);
  99. ycnt[m] = uns(1,i)+hw*(double(m)+0.5);
  100. }
  101. for( iy = 0; iy < Nws; iy++ ) {
  102. for( jx = 0; jx < Nws; jx++ ) {
  103. intpnt( xcnt[jx], twx, wwx, ptwx, whwx,
  104. ycnt[iy], twy, wwy, ptwy, whwy,
  105. hwh );
  106. tmpv0 = 0.0;
  107. tmpu0 = 0.0;
  108. tmpvi = 0.0;
  109. tmpui = 0.0;
  110. for( m = 0; m < Nwy; m++ ) {
  111. tmp0v0 = 0.0;
  112. tmp0u0 = 0.0;
  113. tmp0vi = 0.0;
  114. tmp0ui = 0.0;
  115. for( n = 0; n < Nwx; n++ ) {
  116. tmp0v0 += kernel_v0( twy[m], twx[n], i1, j1 )*
  117. wwx[n]*scalcoif4( twx[n], J, j, sc4 );
  118. tmp0u0 += kernel_u0( twy[m], twx[n], i1, j1 )*
  119. wwx[n]*scalcoif4( twx[n], J, j, sc4 );
  120. tmp0vi += kernel_vi( twy[m], twx[n], i1, j1 )*
  121. wwx[n]*scalcoif4( twx[n], J, j, sc4 );
  122. tmp0ui += kernel_ui( twy[m], twx[n], i1, j1 )*
  123. wwx[n]*scalcoif4( twx[n], J, j, sc4 );
  124. }
  125. tmpv0 += tmp0v0*wwy[m]*scalcoif4( twy[m],J,i,sc4 );
  126. tmpu0 += tmp0u0*wwy[m]*scalcoif4( twy[m],J,i,sc4 );
  127. tmpvi += tmp0vi*wwy[m]*scalcoif4( twy[m],J,i,sc4 );
  128. tmpui += tmp0ui*wwy[m]*scalcoif4( twy[m],J,i,sc4 );
  129. }
  130. sumv0 += tmpv0;
  131. sumu0 += tmpu0;
  132. sumvi += tmpvi;
  133. sumui += tmpui;
  134. }
  135. }
  136. sumv0 = sumv0*Circum[j1]*Circum[i1];
  137. sumvi = sumvi*Circum[j1]*Circum[i1];
  138. sumu0 = sumu0*Circum[j1]*Circum[i1]-0.5*Circum[i1];
  139. sumui = sumui*Circum[j1]*Circum[i1]+0.5*Circum[i1];
  140. } else if ( i1 == j1 && i != j ) {
  141. sumv0 = kernel_v0( tn[i], tn[j], i1, j1 )*step_w*Circum[j1]*Circum[i1];
  142. sumvi = kernel_vi( tn[i], tn[j], i1, j1 )*step_w*Circum[j1]*Circum[i1];
  143. sumu0 = kernel_u0( tn[i], tn[j], i1, j1 )*step_w*Circum[j1]*Circum[i1];
  144. sumui = kernel_ui( tn[i], tn[j], i1, j1 )*step_w*Circum[j1]*Circum[i1];
  145. } else {
  146. sumv0 = kernel_v0( tn[i], tn[j], i1, j1 )*step_w*Circum[j1]*Circum[i1];
  147. sumvi = 0.0;
  148. sumu0 = kernel_u0( tn[i], tn[j], i1, j1 )*step_w*Circum[j1]*Circum[i1];
  149. sumui = 0.0;
  150. }
  151. Pwt(i1*Nw+i,j1*Nw+j) = sumv0;
  152. Pwt((Nc+i1)*Nw+i,j1*Nw+j) = sumvi;
  153. Pwt(i1*Nw+i,(Nc+j1)*Nw+j) = (-1.0)*sumu0;
  154. Pwt((Nc+i1)*Nw+i,(Nc+j1)*Nw+j) = (-1.0)*sumui;
  155. }
  156. }
  157. }
  158. }
  159. for( i = 0; i < Nc; i++ ) {
  160. if( i < Gnd ) {
  161. tempc=Gndc[i];
  162. } else if( i >= Gnd && i < (Gnd + Rec) ) {
  163. tempc=Recc[i-Gnd];
  164. } else if( i >= (Gnd + Rec) && i < (Gnd + Rec + Cir) ) {
  165. tempc=Circ[i-Gnd-Rec];
  166. } else if( i >= (Gnd + Rec + Cir) && i < (Gnd + Rec + Cir + Tra) ) {
  167. tempc=Trac[i-Gnd-Rec-Cir];
  168. }
  169. for( j = 0; j < Nw; j++ ) {
  170. Pwt(2*Nw*Nc+i,i*Nw+j)=sqv(tempc);
  171. Pwt(i*Nw+j,2*Nw*Nc+i)=tempc/power(2.0,double(J)/2.0);
  172. }
  173. }
  174. cerr << 2*Nc*Nw+Nc
  175. << "X"
  176. << 2*Nc*Nw+Nc
  177. << " System Matrix Created !!! "
  178. << endl;
  179. /* Solve equation for different current combination */
  180. for( i2 = 1; i2 < Nc; i2++ ) {
  181. for( j2 = i2; j2 < Nc; j2++ ) {
  182. cout<<i2<<endl;
  183. cout<<j2<<endl;
  184. if( i2==j2 ) {
  185. Iq_sq[0] = -1.0;
  186. for ( k2=1; k2 < Nc; k2++) {
  187. if( k2==j2 ) {
  188. Iq_sq[k2]=1.0;
  189. } else {
  190. Iq_sq[k2]=0.0;
  191. }
  192. }
  193. } else {
  194. for ( k2=0; k2 < Nc; k2++) {
  195. if( k2==i2 ) {
  196. Iq_sq[k2]=1.0;
  197. } else if( k2==j2 ){
  198. Iq_sq[k2]=-1.0;
  199. } else {
  200. Iq_sq[k2]=0.0;
  201. }
  202. }
  203. }
  204. for(i=0;i<2*Nc*Nw;i++) {
  205. Rhsw[i] = 0.0;
  206. }
  207. for(i=0;i<Nc;i++) {
  208. if( i < Gnd ) {
  209. tempc=Gndc[i];
  210. } else if( i >= Gnd && i < (Gnd + Rec) ) {
  211. tempc=Recc[i-Gnd];
  212. } else if( i >= (Gnd + Rec) && i < (Gnd + Rec + Cir) ) {
  213. tempc=Circ[i-Gnd-Rec];
  214. } else if( i >= (Gnd + Rec + Cir) && i < (Gnd + Rec + Cir + Tra) ) {
  215. tempc=Trac[i-Gnd-Rec-Cir];
  216. }
  217. Rhsw[2*Nc*Nw+i]=(-1.0)*Ic*Iq_sq[i]*oms*tempc/power(2.0,double(J)/2.0);
  218. }
  219. cerr << endl
  220. << " Right hand side vector created!! "
  221. << endl;
  222. for( i = 0; i < 2*Nc*Nw+Nc; i++ ) {
  223. for( j = 0; j < 2*Nc*Nw+Nc; j++ ) {
  224. Pw(i,j)=Pwt(i,j);
  225. }
  226. }
  227. /* By Using LU Decomposition */
  228. if( matr == 1 ) {
  229. ludcmp( Pw, Indx, dlu );
  230. lubksb( Pw, Rhsw, Indx );
  231. }
  232. /* By Using Stab */
  233. if( matr == 2 ) {
  234. xc.resize(2*Nc*Nw+Nc);
  235. bi_cgstab_c( Pw, xc, Rhsw, EPS );
  236. Rhsw = xc;
  237. }
  238. cerr << " System of linear equations for N = "
  239. << 2*Nc*Nw+Nc
  240. << " solved !!! "
  241. << endl;
  242. /* To calculate the resistance and inductance ****/
  243. for( i = 0; i < Nw; i++ ) {
  244. ts[i] = double(i)/double(Nw);
  245. }
  246. for( ix = 0; ix < Nc; ix++ ) { // to be revised by Nc
  247. for( i = 0; i < Nw; i++ ) {
  248. tmp = 0.0;
  249. tmp0 = 0.0;
  250. for( j = i-7; j <= i+4; j++ ) {
  251. if( j < 0 ) {
  252. tst = ts[i] + 1.0;
  253. j1 = j + Nw;
  254. } else if ( j > Nw-1) {
  255. tst = ts[i] - 1.0;
  256. j1 = j - Nw;
  257. } else {
  258. tst = ts[i];
  259. j1 = j;
  260. }
  261. tmp += scalcoif4( tst, J, j1, sc4 )*Rhsw[ix*Nw+j1];
  262. tmp0 += scalcoif4( tst, J, j1, sc4 )*Rhsw[(ix+Nc)*Nw+j1];
  263. }
  264. Jnj[ix*Nw+i]=tmp;
  265. Jnj[(ix+Nc)*Nw+i]=tmp0;
  266. }
  267. }
  268. for ( i = 0; i < Nc; i++ ) {
  269. Jnj[2*Nc*Nw+i]=Rhsw[2*Nc*Nw+i];
  270. }
  271. for ( i = 0; i < Nc; i++ ) {
  272. sum1[i] = 0.0;
  273. Res[i] = 0.0;
  274. Ind[i] = 0.0;
  275. }
  276. rsum = 0.0;
  277. isum = 0.0;
  278. for ( i=0; i<Nc; i++ ) {
  279. tmpsum1 = 0.0;
  280. tmpsum2 = 0.0;
  281. sum2[i] = cmplx( 0.0, 0.0 );
  282. for ( j=0; j<Nw; j++ ) {
  283. tmpsum1 += imag(Jnj[(i+Nc)*Nw+j]*conjg(Jnj[i*Nw+j]))*Circum[i]/double(Nw);
  284. tmpsum2 += real(Jnj[2*Nc*Nw+i]*conjg(Jnj[i*Nw+j]))*Circum[i]/double(Nw);
  285. sum2[i] += Jnj[i*Nw+j]*Circum[i]/double(Nw);
  286. }
  287. rsum += tmpsum1;
  288. isum += tmpsum2;
  289. sum1[i]=sqv(cabs(sum2[i]));
  290. }
  291. for ( i=0; i<Nc; i++ ) {
  292. Res[i]=omegamu*rsum/sum1[i];
  293. Ind[i]=(-1)*mu*isum/sum1[i];
  294. }
  295. if( i2 == j2 ) {
  296. Res1(i2,i2)=Res[i2];
  297. Ind1(i2,i2)=Ind[i2];
  298. } else {
  299. Res1(i2,j2)=Res[i2];
  300. Ind1(i2,j2)=Ind[i2];
  301. Res1(j2,i2)=Res[j2];
  302. Ind1(j2,i2)=Ind[j2];
  303. }
  304. }
  305. }
  306. for( i2 = 1; i2 < Nc; i2++ ) {
  307. for( j2 = 1; j2 < Nc; j2++ ) {
  308. if(i2!=j2) {
  309. Res1(i2,j2)=(Res1(i2,i2)+Res1(j2,j2)-Res1(i2,j2))/2.0;
  310. Ind1(i2,j2)=(Ind1(i2,i2)+Ind1(j2,j2)-Ind1(i2,j2))/2.0;
  311. }
  312. }
  313. }
  314. outdata << Nc-1 << ""
  315. << "X" << ""
  316. << Nc-1 << ""
  317. << " Resistance Matrix (Ohm/m):" << endl;
  318. outdata <<endl;
  319. for( i2 = 1; i2 < Nc; i2++ ) {
  320. for( j2 = 1; j2 < Nc; j2++ ) {
  321. outdata <<Res1(i2,j2)<< " ";
  322. }
  323. outdata <<endl;
  324. outdata <<endl;
  325. }
  326. outdata << Nc-1 << ""
  327. << "X" << ""
  328. << Nc-1 << ""
  329. << " Inductance Matrix (H/m):" << endl;
  330. outdata <<endl;
  331. for( i2 = 1; i2 < Nc; i2++ ) {
  332. for( j2 = 1; j2 < Nc; j2++ ) {
  333. outdata <<Ind1(i2,j2)<< " ";
  334. }
  335. outdata <<endl;
  336. outdata <<endl;
  337. }
  338. cout<<"Calculation Completed."<<endl;
  339. return 0;
  340. }