2025년 2월 17일 월요일

vi column edit in WSL (windows subsystem linux)

 Ususal key binding ""ctrl+v"" in vim conflicts with Windows "paste". 

To avoid this, one can change the Windows terminal key binding for copy and paste 

into "ctrl+shift+c" and  "ctrl+shift+v"


Edit %LocalAppData%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json file.

And change

Original :

{
    "command": 
    {
        "action": "copy",
        "singleLine": false
    },
    "keys": "ctrl+c" 
},
{
    "command": "paste",
    "keys": "ctrl+v"
},

Modified :

{
    "command": 
    {
        "action": "copy",
        "singleLine": false
    },
    "keys": "ctrl+shift+c"
},
{
    "command": "paste",
    "keys": "ctrl+shift+v"

},

2025년 2월 11일 화요일

Principal value integration in scipy (dispersion relation)

 To compute a principal value integration

$\frac{E-E_s}{\pi} \int_0^\infty dx \frac{W(x)}{(x-E_s)(x-E)}$

One can change it into 

$\frac{1}{\pi} \int_0^\infty dx \frac{W(x)}{(x-E)} - \frac{W(x)}{(x-E_s)}$

then using weight='cauchy' in quadpack, ( which multiply 1/(x-wvar) ) 

one can compute the integration as 

quad(w,0.,200.,weight='cauchy',wvar=ee)[0]/np.pi -quad(w,0.,200.,weight='cauchy',wvar=es)[0]/np.pi


In case of Wolfram language, one can use 

NIntegrate[(ee-Es)/Pi*W[x]/(x-Es)/(x-ee),{x,0,400.},PrincipalValue->True,PrecisionGoal->10,Method->"GlobalAdaptive",Exclusions->{6.0,ee}]  

2025년 1월 15일 수요일

profile fortran + mpi

어떻게 fortran+mpi 프로그램을 프로파일링 하는지 공부.

인터넷을 찾아보니 주로 사용하는 방법은 

(1) gprof 과 gprof2dot 조합 : 컴파일 시 -pg option필요

(2) valgrind, callgrind, kcashegrind 조합 : 컴파일 시 -g option 필요. 

인 것 같다. 


How to use gprof 

(1) compile code with option -pg 

(2) run the code  : (mpirun generate multiple gmon.out )

    export GMON_OUT_PREFIX=gmon.out  #optional? 

    mpirun -np 3 ./run 

(3) once gmon.out file is created, run gprof.  

    gprof run  gmon.out.xxx > prof_result.txt 

(4) reading prof_result.txt seems to show which subroutine is most time-consumming. 


2024년 11월 13일 수요일

interactive python arrow key problem

 Sometimes, the arrow key in python shell does not act like usually. Pressing arrow key does not gives history(previous line) but gives characters. This can be fixed by installing "gnureadline" package. 


   pip3 install gnureadline  



2024년 11월 10일 일요일

Binary file problem with intel fortran (ifort) and gfortran (GNU fortran)

 I found that reading a binary file created with the same source file but compiled by ifort and gfortan give different results. By searching internet, I found that the way binary file is written in ifort is not the same sa gfortran. Thus, to make the same code gives the same results, one have to use open binary files with ACCESS='stream', FORM='unformatted'. 

  • Non-standard: open(unit=20,file=fname,form='binary')
  • Standard: open(unit=20,file=fname,form='unformatted',access='stream',status='replace')


But, this seems to be not the full story. 

I need to check the correct way to convert following gfortran code to ifort code.

 open(pin_file_id, file=trim(pin_file), status='unknown',form='unformatted', &

                 access='direct',action='write',recl=nsize,iostat=ios)


* After testing, following seems to be equivalent to the above one. 

open(pin_file_id, file=trim(pin_file), status='unknown',form='BINARY', &

                 access='direct',action='write',recl=nsize,iostat=ios)


* According to ChatGPT, ifort and gfortran interpret differently the recl= argument in open statement. In gfortran, 'recl' is interpreted as length of bytes. In ifort, 'recl' is interpreted as length of 4 bytes. Thus, to get the same results, if "recl= nsize" in gfortran , "recl= nsize/4" in ifort have to be used. Or/And(?) one have to use compile option in "ifort -assume byterecl .

This also imply that the "recl" in gfortran must be a multiple of 4. 


* But, modifying the source code depending on the compiler or machine is not desirable. access='stream' option without 'recl=' parameter will behave similarily in both ifort and gfortran. keep form='unformatted'.  status='replace' create a new file if it does not exist, or replace it if it exist. Thus, it may be better than status='unknown' to ensure freshfile in each run. (Or default status='unknown' may be okay??)

But, to access specific data, one have to use 'pos=' parameter which should be counted in whole file. For example, if data is stored as  'real(8), integer(4), real(4)', to read these 

read(file_id, pos=1) real_value

read(file_id, pos=9) int_value

read(file_id, pos=13) real4_value


In other words, 'pos' specify the starting byte position, and type of variable specify the length of the data to read. 




2024년 10월 29일 화요일

Gradient of eigenvalue eigenvector of Matrix

Matrix 의 eigenvalue 와 eigen vector 는 다음과 같은 관계가 있다. 

$A u = v u$. 

여기서, eigenvalue와 eigenvector 를 일종의 matrix A에 대한 함수로 생각하고($v(A)$ and $u(A)$.) , 미분 Gradient  을 정의할 수 있다고 볼 수 있다.  


문제는 이러한 미분을 일반적인 Matrix 에 대해서는 정확하게 정의하기가 어렵다는 것이다. 다만, real symmetric matrix인 경우에는 다음과 같은 정의를 할 수 있다. matrix $A_0$ 에 대해서 eigenvalue, eigenvector가 $v_0$, $u_0$라고 하자. 그러면 매우 작은 Matrix변화에 대해 

$ d v =  u_0^T (d A) u_0 $ 또는 $ dv/d(A_{ij}) = (u_0)_i (u_0)_j$.

$ d u = ( v_0 I - A_0) ( d A) u_0$ 또는 $ (d u)_i = ( v_0 I - A_0)_{ij} ( d A)_{jk} (u_0)_k$

와 같이 정의할 수 있다고 한다.  (real symmetric matrix임에 유의.)


REF: https://www.janmagnus.nl/papers/JRM011.pdf


즉, matrix $A_0$ 가 matrix $A_0 + d A$ 로 변할 때, eigen value와 eigen vector는 

$ v_0 -> v_0 + d v $,

$ u_0 -> u_0 + d u$

로 계산할 수 있다는 것이다. 하지만, 논문을 자세히 읽어보진 않았지만 아마도 degeneracy 가 없어야 한다는 조건이 붙을 것 같다.   





2024년 10월 20일 일요일

Baldur's Gate 2 hex edit

 How To hex edit Baludur's gate save file.

(1) Open Baldur.gam file in the save folder.

(2) money: convert gold in hex number. Edit by searching in reverse order.( For example,2000 -> 07D0. Search "D0 07"). 

(3) Character status: convert status in hex number. serach in order of 

"STR STRMOD INT WIS DEX CON CHA". (STRMOD is for STR=18 case.)  


어나더에덴

https://m.cafe.naver.com/ca-fe/web/cafes/29617015/articles/247442?tc=cafe_member_profile