<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">use strict;
use Class::Struct;
use Math::Trig;
use File::Find;
use File::Basename;
use FileHandle;

struct cell_struct=&gt;{
    beta   =&gt; '$',
    a2     =&gt; '$',
    b2     =&gt; '$',
    t      =&gt; '$',
    zs     =&gt; '$',
    zc     =&gt; '$',
    ze     =&gt; '$',
    D      =&gt; '$',
    p0     =&gt; '$'
};

struct structure_struct=&gt;{
    start_str =&gt; '$',          # z-position of 1st cell center [mm]   *.in
    BaPhase   =&gt; '$',          # initial phase of buncher      [deg]  *.bat
    N         =&gt; '$'           # number of cells                      *.dat
};


our($structure, @cell);
our($structure_file, $GDF_in, $GDF_out, $temp_time, $temp_phase);
our($frequency, $omega, $omega_deg, $lambda, $k_deg, $c);

$structure = structure_struct-&gt;new();


&amp;initialize;
&amp;read_cell_data_file;
&amp;set_cell_pzD;
&amp;print_cell_zp;
&amp;print_structure_data;

&amp;gdf2a;
&amp;time2phase;
&amp;asci2gdf;
&amp;delete_temp;

#=============================================================================
#        read structure data
#=============================================================================
sub initialize{

    $structure_file = "../../TW/TW_structure.dat";
    $GDF_in         = "../TW_Buncher.gdf";
    $GDF_out        = "phase.gdf";
    $temp_time      = "temp_time.txt";
    $temp_phase     = "temp_phase.txt";

    $frequency = 2.856e9;
    $omega     = 2*pi*$frequency;
    $omega_deg = $frequency*360;

    $c      = 2.99792458e+8;   # speed of light [m/sec]
    $lambda = $c/$frequency;   # wavelength [m]
    $k_deg  = 360.0/$lambda;

    $structure-&gt;start_str(100);    # start point: center of 1st cell(input cpl)
    $structure-&gt;BaPhase(30);       # initial phase [deg]
    
}

#=============================================================================
#        read input file
#=============================================================================
sub read_cell_data_file{

    my($fh);
    my(@in_dat);

    $fh = new FileHandle;
    
    printf "read input data form %s\n", $structure_file;

    $fh-&gt;open("&lt;".$structure_file) or die "open: $!";

    while(&lt;$fh&gt;){
	chomp;
	if(/^#/){next;}               # 先頭に'#'があるとコメント文
	if(/#/){s/(^.+)#.*/$1/}       # '#' 以降はコメント
	s/^\s*(.*?)\s*$/$1/;          # 前後の空白を削除

	if(/^\$directory/){&amp;read_dir($fh);}
	if(/^\$input_RF/){&amp;read_RF($fh);}
	if(/^\$cell/){&amp;read_cell($fh);}
    }

    $fh-&gt;close;

}

#=============================================================================
#        read directory
#=============================================================================
sub read_dir{
    my $fh;

    $fh = $_[0];

    while(&lt;$fh&gt;){
	chomp;
	if(/^#/){next;}               # 先頭に'#'があるとコメント文
	if(/#/){s/(^.+)#.*/$1/}       # '#' 以降はコメント
	s/^\s*(.*?)\s*$/$1/;          # 前後の空白を削除
	if(/^\$end/){last;};

	printf "\tdirectrory:%s\n", $_;

	last;
    }    
}

#=============================================================================
#        read RF
#=============================================================================
sub read_RF{
    my $fh;

    $fh = $_[0];

    while(&lt;$fh&gt;){
	chomp;
	if(/^#/){next;}               # 先頭に'#'があるとコメント文
	if(/#/){s/(^.+)#.*/$1/}       # '#' 以降はコメント
	s/^\s*(.*?)\s*$/$1/;          # 前後の空白を削除
	if(/^\$end/){last;};

	printf "\tRF Power:%.3f [MW]\n", $_;

	last;
    }    
}

#=============================================================================
#        read cell
#=============================================================================
sub read_cell{
    my $fh;
    my @input;
    my $i;

    $fh = $_[0];

    printf "\tbeta\t2a\t2b\tt\trb\tn\n";

    $i=0;
    while(&lt;$fh&gt;){
	chomp;
	if(/^#/){next;}               # 先頭に'#'があるとコメント文
	if(/#/){s/(^.+)#.*/$1/}       # '#' 以降はコメント
	s/^\s*(.*?)\s*$/$1/;          # 前後の空白を削除
	if(/^\$end/){last;};
	@input = split(/\s+/,$_);

	our $one_cell = cell_struct-&gt;new();

	$one_cell-&gt;beta($input[0]);
	$one_cell-&gt;a2($input[1]);
	$one_cell-&gt;b2($input[2]);
	$one_cell-&gt;t($input[3]);

	$i++;
	$cell[$i]=$one_cell;

	printf "\t%.3f\t%.3f\t%.3f\t%.3f\n", 
	       $cell[$i]-&gt;beta, $cell[$i]-&gt;a2, $cell[$i]-&gt;b2, $cell[$i]-&gt;t;
    }

    $structure-&gt;N($i);
}

#=============================================================================
# check every cell data 
#=============================================================================
sub set_cell_pzD{
    my($D, $i, $N);
    
    #------------ sw region ---------
    our $one_cell = cell_struct-&gt;new();

    $D=$cell[1]-&gt;beta * $lambda /3.0;
    $one_cell-&gt;D($D);
    $one_cell-&gt;beta($cell[1]-&gt;beta);
    $one_cell-&gt;p0($structure-&gt;BaPhase);
    $one_cell-&gt;ze($structure-&gt;start_str*1e-3);
    $one_cell-&gt;zs($one_cell-&gt;ze-$D/2.0);
    $one_cell-&gt;zc(($one_cell-&gt;zs + $one_cell-&gt;ze)/2.0);

    $cell[0] = $one_cell;

    #------------  in tw region ---------

    for($i=1; $i&lt;=$structure-&gt;N; $i++){
	$D=$cell[$i]-&gt;beta * $lambda /3.0;

	$cell[$i]-&gt;D($D);
	$cell[$i]-&gt;p0($structure-&gt;BaPhase-($i-1)*120);
	$cell[$i]-&gt;zs($cell[$i-1]-&gt;ze);
	$cell[$i]-&gt;ze($cell[$i]-&gt;zs+$D);
	$cell[$i]-&gt;zc(($cell[$i]-&gt;zs + $cell[$i]-&gt;ze)/2.0);
    }

    #------------- sw region ---------
    $N=$structure-&gt;N;
    our $one_cell = cell_struct-&gt;new();

    $D=$cell[$N]-&gt;beta * $lambda /3.0;
    $one_cell-&gt;D($D);
    $one_cell-&gt;beta($cell[$N]-&gt;beta);
    $one_cell-&gt;p0($cell[$N]-&gt;p0-120);
    $one_cell-&gt;zs($cell[$N]-&gt;ze);
    $one_cell-&gt;ze($cell[$N]-&gt;ze+$D/2.0);
    $one_cell-&gt;zc(($one_cell-&gt;zs + $one_cell-&gt;ze)/2.0);

    $cell[$N+1] = $one_cell;

}

#=============================================================================
# print cell posiotion initial phase
#=============================================================================
sub print_cell_zp{
    my($i);

    printf "\n---------------------------------------------------------------------\n";
    printf "\t#\tzs\t\tzc\t\tze\t\tphase\n";
    printf "=====================================================================\n";
    

    for($i=0; $i&lt;=$structure-&gt;N+1; $i++){
	printf "\t%d\t", $i;
	printf "%10.3f\t", $cell[$i]-&gt;zs * 1e3;
	printf "%10.3f\t", $cell[$i]-&gt;zc * 1e3;
	printf "%10.3f\t", $cell[$i]-&gt;ze * 1e3;
	printf "%7.1f\n", $cell[$i]-&gt;p0;
    }

    printf "---------------------------------------------------------------------\n\n";

}

#=============================================================================
# print structure data 
#=============================================================================
sub print_structure_data{

    printf "\n-------------------------------------------------------\n";
    printf "\t 1st cell center position : %.3f[mm]\n",  $structure-&gt;start_str;
    printf "\t Buncher initial phase    : %.3f[deg]\n", $structure-&gt;BaPhase;
    printf "\t Numbe of cells           : %d\n",        $structure-&gt;N;
    printf "-------------------------------------------------------\n\n";

}




#=============================================================================
# convert gdf file to ascii file
#=============================================================================
sub gdf2a{

    system("gdf2a -w 15 -o $temp_time $GDF_in z t");

}


#=============================================================================
# convert to phase from time
#=============================================================================
sub time2phase{

    my($z, $time);
    my(@input);
    my($n, $dp);
    my $print_zp = 0;

    open(TIME, "&lt;$temp_time") or die "file:$temp_time\n $!";
    open(PHAS, "&gt;$temp_phase") or die "file:$temp_phase\n $!";

    foreach(&lt;TIME&gt;){
	if(/^\s*\d/){
	    chomp;
	    s/^\s+//;                        # 行頭の空白を削除
	    @input = split(/\s+/,$_);
	    $z    = $input[0];
	    $time = $input[1];
	    if( ($z&lt; $cell[0]-&gt;zs) || ($cell[$structure-&gt;N+1]-&gt;ze &lt; $z)){next;}
	    ($n, $dp) = &amp;z2phase($z, $time);
	    printf PHAS "   %f  %f\n", $z , $dp;
	}elsif(/^\s*z\s+t/){
	    if($print_zp !=1){
		print PHAS "                 z                phase\n";
	    }
	    $print_zp =1;
	}else{
	    print PHAS "#$_";
	}
    }

    close TIME;
    close PHAS;

}
#=============================================================================
# find cell 
#=============================================================================
sub z2phase{

    my $z = $_[0];
    my $t = $_[1];
    my($dz);
    my($n, $dp, $p);

    $n = &amp;find_cell($z);
    $dz = $z-$cell[$n]-&gt;zs;

    if($n==0 || $n==$structure-&gt;N+1){
	$p = $cell[$n]-&gt;p0 + 360*$frequency*$t;
    }else{
	$p = $cell[$n]-&gt;p0 + 360*($frequency*$t - $dz/($cell[$n]-&gt;beta * $lambda));
    }

    $dp = $p-int($p/360.0)*360;

    if($dp&lt;-180){$dp += 360.0;}
    if(180&lt;$dp) {$dp -= 360.0;}
 
    return ($n, $dp);

}

#=============================================================================
# find cell 
#=============================================================================
sub find_cell{
    my $z = $_[0];
    my($min, $max, $tst);

    $min = 0;
    $max = $structure-&gt;N+1;

     do{
	$tst = int(($max + $min)/2);

	if($cell[$tst]-&gt;ze &lt; $z){
	    $min = $tst+1;
	}else{
	    $max = $tst;
	}

    }while($max != $min);

    return $max;
}


#=============================================================================
# convert gdf file to ascii file
#=============================================================================
sub asci2gdf{

    system("asci2gdf -o $GDF_out $temp_phase");

}

#=============================================================================
# delete temp file
#=============================================================================
sub delete_temp{

    system("rm -f $temp_time");
    system("rm -f $temp_phase");

}
</pre></body></html>