[問題]請問如何作次數未知的加法?

看板Fortran作者 (卡進無底洞)時間16年前 (2008/12/09 15:32), 編輯推噓9(9019)
留言28則, 4人參與, 最新討論串1/1
以下是程式碼 CALL JUMP(DSMAX(MAT),DS) ! Determines segment length. CALL STEP(DS,DSEF,NCROSS) ! Moves particle to end of step. WRITE(17,*) N,MIN(DS,DSEF) ! 決定把DS 跟 DSEF的最小值寫入檔案 但是現在有一個問題是, 我想把每一顆粒子的總距離加起來,即是 第一顆的總距離,及第二顆的總距離 例如 1 1.451336171889607E-007 1 1.625982843357572E-007 1 7.786647901384987E-008 1 6.001349813026089E-008 1 2.501697761188227E-008 2 1.989400815159079E-008 2 3.863321521894446E-008 2 2.358943701411900E-008 可是在N=1時 有5個小距離 N=2 有3個小距離 在程式裡要如何知道每一次所有的小距離,然後相加起來 得到各別的N總距離 謝謝 N=0 ! Shower counter. C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C ------------------------ Shower simulation starts here. C 101 CONTINUE N=N+1.0D0 C C ********** Set the initial state of the primary particle. C KPAR=KPARP X=X0 Y=Y0 Z=Z0 E=E0 CALL GCONE(U,V,W) ! Initial direction. C C **** Check if the trajectory intersects the material system. C CALL LOCATE IF(MAT.EQ.0) THEN CALL STEP(1.0D30,DSEF,NCROSS) IF(MAT.EQ.0) THEN ! The particle does not enter the system. PRIM(1)=PRIM(1)+1.0D0 GO TO 105 ENDIF ENDIF C C ************ Primary particle counters. C ILB(1)=1 ! Identifies primary particles. ILB(2)=0 ILB(3)=0 ILB(4)=0 ILB(5)=0 DO IC=1,7 COL(IC)=0.0D0 ! Numbers of events. ENDDO DO I=1,NBOD EDEP(I)=0.0D0 ! Energy deposited in each body. ENDDO C --------------------------------------------------------------------- C ------------------------ Track simulation begins here. C COMMON/TRACK/E,X,Y,Z,U,V,W,WGHT,KPAR,IBODY,MAT,ILB(5) CALL CLEANS ! Cleans the secondary stack. 102 CONTINUE CALL START ! Starts simulation in current medium. C 103 CONTINUE CALL JUMP(DSMAX(MAT),DS) ! Determines segment length. CALL STEP(DS,DSEF,NCROSS) ! Moves particle to end of step. WRITE(17,*) N,MIN(DS,DSEF) DS0=DS0+MIN(DS,DSEF) WRITE(36,*) N,DS0 ! DDSS=DDSS+MIN(DS,DSEF) C IF(MAT.EQ.0) THEN ! The particle has left the material system. IEXIT=1 ! Labels emerging particles. GO TO 104 ! Exit. ENDIF C IF(NCROSS.GT.0) GO TO 102 ! The particle crossed an interface. C CALL KNOCK(DE,ICOL) ! Simulates the interaction event. COL(ICOL)=COL(ICOL)+1.0D0 ! The interaction is counted. EDEP(IBODY)=EDEP(IBODY)+DE ! Deposited energy counter. IF(E.LT.EABS(KPAR,mat)) THEN ! The particle has been absorbed. IEXIT=2 ! Labels absorbed particles. GO TO 104 ! Exit. ENDIF GO TO 103 C ------------------------ The simulation of the track ends here. C --------------------------------------------------------------------- 104 CONTINUE C C ************ Increment particle counters. C IF(ILB(1).EQ.1) THEN PRIM(IEXIT)=PRIM(IEXIT)+1.0D0 DO KC=1,6 AVNI(KC,IEXIT)=AVNI(KC,IEXIT)+COL(KC) AVNI2(KC,IEXIT)=AVNI2(KC,IEXIT)+COL(KC)**2 ENDDO ELSE SEC(KPAR,IEXIT)=SEC(KPAR,IEXIT)+1.0D0 C C ------------------------ The simulation of the shower ends here. CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C C **** Energies deposited in different bodies. C DO KB=1,NBOD EDEPT(KB)=EDEPT(KB)+EDEP(KB) VEDEPT(KB)=VEDEPT(KB)+EDEP(KB)**2 ENDDO C **** Deposited energy distributions in selected bodies or groups of C bodies (specific of each geometry). C In this case, EDEP(1,*) = Energy in body 1. EDISTR=EDEP(1) IF(EDISTR.GT.1.0D0) THEN ICH=1.0D0+EDISTR/DEBIN ICH=MIN(NCHD,ICH) EDEPD(1,ICH)=EDEPD(1,ICH)+1.0D0 ENDIF C 105 CONTINUE C C ---- End the simulation after the alloted time or after completing C DSIMS showers. CALL TIMER(TSEC) IF(TSEC.LT.TSECA.AND.N.LT.NTOT) GO TO 101 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.106.110

12/09 17:40, , 1F
DS2=DS2+DS
12/09 17:40, 1F

12/09 17:42, , 2F
我覺得你還是把CODE寫完整一點出來 CALL放在DO loap裡嗎?
12/09 17:42, 2F
※ 編輯: nestea911399 來自: 140.114.106.110 (12/09 18:21)

12/09 18:22, , 3F
N代表的意思是顆還是粒?
12/09 18:22, 3F

12/09 18:23, , 4F
是代表粒子顆數,謝謝
12/09 18:23, 4F

12/09 18:26, , 5F
我的問題是不知道每一個顆的DS會有幾個,謝謝
12/09 18:26, 5F

12/09 18:27, , 6F
例如的結果是編號17的檔案結果嗎?
12/09 18:27, 6F

12/09 18:32, , 7F
對,那是我簡化的,原本有40或50幾個,只是不能確定N=1
12/09 18:32, 7F

12/09 18:32, , 8F
有幾個,N=2時又有幾個?
12/09 18:32, 8F

12/09 18:33, , 9F
然後只紀錄加到最後一個的總DS,再把總DS寫在檔案裡
12/09 18:33, 9F

12/09 18:38, , 10F
這樣的話能不能把JUMP的SUB. 貼出來看一下
12/09 18:38, 10F

12/09 18:41, , 11F
你試著在WRITE前面加上判斷式判斷N=1時 sum=sum+1
12/09 18:41, 11F

12/09 19:06, , 12F
不好意思.可是我現在就沒辦法知道N=1的DS有幾個因為
12/09 19:06, 12F

12/09 19:08, , 13F
每次累加的結果都會寫進檔案,我只要最後的總DS
12/09 19:08, 13F

12/09 19:16, , 14F
所以其實你的問題是希望他不要每次累加都輸出
12/09 19:16, 14F

12/09 19:16, , 15F
而是只要他累加過後的結果?
12/09 19:16, 15F

12/09 21:15, , 16F
恩...因為我沒辦法知道他每一次會有幾個DS出現
12/09 21:15, 16F

12/10 00:23, , 17F
我的想法是想辦法將N=1要轉成2前 做輸出
12/10 00:23, 17F

12/10 00:25, , 18F
所以可以放在N=N+1DO 這行下面 打上
12/10 00:25, 18F

12/10 00:25, , 19F
WRITE(17,*) N-1,MIN(DS,DSEF)
12/10 00:25, 19F

12/10 00:32, , 20F
看到整個都花了XDDDDDDDD
12/10 00:32, 20F

12/10 00:34, , 21F
謝謝,那請問第一顆怎麼辦
12/10 00:34, 21F

12/10 00:44, , 22F
不行捏,因為要是把WRITE放在N=N+1DO下面DS會等於零
12/10 00:44, 22F

12/10 02:29, , 23F
那放在104 continue下呢? 試試看 n不用-1
12/10 02:29, 23F

12/10 10:12, , 24F
簡單 你讀完第一行之後再回去讀第一行就好啦
12/10 10:12, 24F

12/10 12:54, , 25F
請問你的第一行是指哪裡?
12/10 12:54, 25F

12/10 23:25, , 26F
資料的第一行
12/10 23:25, 26F

12/11 00:00, , 27F
我想雪大的說法應該是檔案輸出後 去讀每個N的第一行
12/11 00:00, 27F

12/11 11:06, , 28F
讀進資料後 弄兩個變數 一個當計數器 一個當作判定式
12/11 11:06, 28F
文章代碼(AID): #19FXyc8M (Fortran)
文章代碼(AID): #19FXyc8M (Fortran)