#!/usr/bin/perl -w
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
my %commands = () ;

if ( @ARGV <= 0 ) { push ( @{$commands{'help'}}, ' ' ) ; } 
else { (%commands) = main_config::get_commands ( @ARGV ) ; }

my $conf_file ;
if ( -f "main_config.data" )
{    $conf_file = "main_config.data" ; }
else
{    $conf_file = join ('', $ENV{"MAIN_CONF"}, "main_config.data" ) ; }

my %my_main = main_config::read_main_config_file ( $conf_file ) ; 

#####################################################################

if ( !exists($my_main{"PREF_KICK_CR0"}  ) )  { push ( @{$my_main{"PREF_KICK_CR0"}} , 0 ) ; }
if ( !exists($my_main{"PREF_KICK_CR1"}  ) )  { push ( @{$my_main{"PREF_KICK_CR1"}} , 0 ) ; }
if ( !exists($my_main{"PREF_STEP_COOL"} ) )  { push ( @{$my_main{"PREF_STEP_COOL"}} , 0 ) ; }
if ( !exists($my_main{"PREF_STEP_MIN"}  ) )  { push ( @{$my_main{"PREF_STEP_MIN"}}  , 30 ) ; }
if ( !exists($my_main{"PREF_BVAL_MIN"}  ) )  { push ( @{$my_main{"PREF_BVAL_MIN"}}  , 5 ) ; }
if ( !exists($my_main{"PREF_BVAL_MAX"}  ) )  { push ( @{$my_main{"PREF_BVAL_MAX"}}  , 60 ) ; }
if ( !exists($my_main{"PREF_SCAL_XTARG"} ) )  { push ( @{$my_main{"PREF_SCAL_XTARG"}} , 1.5 ) ; }
if ( !exists($my_main{"PREF_MAP_XTARG"}  ) )  { push ( @{$my_main{"PREF_MAP_XTARG"}}  , 3 ) ; }
if ( !exists($my_main{"PREF_MAP_XDIFF"} ) )  { push ( @{$my_main{"PREF_MAP_XDIFF"}} , 3 ) ; }
if ( !exists($my_main{"PREF_MAP_DENS"}  ) )  { push ( @{$my_main{"PREF_MAP_DENS"}} , 1 ) ; }
if ( !exists($my_main{"PREF_NCS_FLAG"}  ) )  { push ( @{$my_main{"PREF_NCS_FLAG"}} , "OFF" ) ; }
if ( !exists($my_main{"PREF_NCS_FORCE"} ) )  { push ( @{$my_main{"PREF_NCS_FORCE"}} , 5.0 ) ; }
if ( !exists($my_main{"PREF_NCS_CRYST"}  ) )  { push ( @{$my_main{"PREF_NCS_CRYST"}} , "OFF" ) ; }
if ( !exists($my_main{"BREF_NCS_FORCE"} ) )  { push ( @{$my_main{"BREF_NCS_FORCE"}} , 0.004 ) ; }
if ( !exists($my_main{"PREF_TARGET"}  ) )  { push ( @{$my_main{"PREF_TARGET"}} , "ML" ) ; }
if ( !exists($my_main{"PREF_ML_MISSIN"}  ) )  { push ( @{$my_main{"PREF_ML_MISSIN"}} , "100" ) ; }
if ( !exists( $my_main{"BULK_SOLV_METHOD"} ) ) { push ( @{$my_main{"BULK_SOLV_METHOD"}}, "NONE" ) } ; 
if ( !exists( $my_main{"DFR_RFREE"} ) ) { push ( @{$my_main{"DFR_RFREE"}}, "OFF" ) } ; 

######################################################

my $MACRO_FILE = "refine.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 's' || substr($keyword, 0,4) eq "step" ) { goto STEP }
    if ( $keyword eq 'b' || substr($keyword, 0,5) eq "bramp" ) { goto BRAMP }
    if ( $keyword eq 'k' || substr($keyword, 0,4) eq "kick" ) { goto KICK }
    if ( $keyword eq 'c' || substr($keyword, 0,4) eq "cool" ) { goto COOL }

    if ( $keyword eq 't' || substr($keyword, 0,6) eq "target" ) { goto TARGET }
    if ( $keyword eq 'a' || substr($keyword, 0,5) eq "atoms" ) { goto ATOMS }
    if ( $keyword eq 'r' || substr($keyword, 0,5) eq "ratio"  ) { goto RATIO }
    if ( $keyword eq 'm' || substr($keyword, 0,3) eq "map"  ) { goto MAP }
    if ( $keyword eq 'x' || substr($keyword, 0,4) eq "xmap" ) { goto XMAP }
    if ( $keyword eq 'q' || substr($keyword, 0,4) eq "qmap" ) { goto QMAP }

    if ( $keyword eq 'n' || substr($keyword, 0,3) eq "ncs"  ) { goto NCS }
    if ( $keyword eq 'g' || substr($keyword, 0,4) eq "gncs" ) { goto GNCS }
    if ( $keyword eq 'y' || substr($keyword, 0,9) eq "cryst_ncs" ) { goto CRYST_NCS }

    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "free" ) { goto FREE }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

  HELP:
    print << "END_OF_TEXT" ;
-h|--help)      prints this message with available options and current status 
-s|--step)      sets number of minimization steps per cycle [@{$my_main{"PREF_STEP_MIN"}}]
-k|--kick)      sets starting and final maximal xyz kick [0.0 - 0.33]
                current = [@{$my_main{"PREF_KICK_CR0"}} - @{$my_main{"PREF_KICK_CR1"}}]
-c|--cool)      sets number of cooling cycles  [@{$my_main{"PREF_STEP_COOL"}}]
-b|--bramp)     sets minimal and maximal starting atomic B-value ramp [$my_main{"PREF_BVAL_MIN"}[0] - $my_main{"PREF_BVAL_MAX"}[0]]

-t|--target)    target function for refinement [MAP/DIFF/LSQ/ML/ML_EST/ML_APR]
                current = [@{$my_main{"PREF_TARGET"}}]
-a|--atoms)     missing atoms for estimation of ML [@{$my_main{"PREF_ML_MISSIN"}}]
-r|--ratio)     approximate ratio target for bond deviations  [0.005 - 0.1]
                current = [@{$my_main{"PREF_SCAL_XTARG"}}]
-m|--map)       MAP for DENSITY refinement [@{$my_main{"PREF_MAP_DENS"}}]
-x|--xmap)      MAP for X-DIFF refinement [@{$my_main{"PREF_MAP_XDIFF"}}]
-q|--qmap)      MAP for rec. space refinement [@{$my_main{"PREF_MAP_XTARG"}}]

-n|--ncs)       Non-Cryst-Similarity NCS energy flag [ON/OFF]
                current = [@{$my_main{"PREF_NCS_FLAG"}}]
-g|--gncs)      Non-Cryst-Similarity NCS geometry force = [@{$my_main{"PREF_NCS_FORCE"}}]
-y|--cryst_ncs) Refine in variety of crystal forms [ON/OFF]: current = [@{$my_main{"PREF_NCS_CRYST"}}]

-f|--free)      r-free flag [ON/OFF] current [@{$my_main{"DFR_RFREE"}}]
-o|--output)    set file name of the created macro [$MACRO_FILE]
   --doit)      create the macro
END_OF_TEXT
    exit ;
  OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

  STEP:  @{$my_main{"PREF_STEP_MIN"}} = shift ( @{$commands{$keyword}} ) ; next ;
  COOL:  @{$my_main{"PREF_STEP_COOL"}} = shift ( @{$commands{$keyword}} ) ; next ;
  BRAMP: 
    @{$my_main{"PREF_BVAL_MIN"}} = shift ( @{$commands{$keyword}} ) ; 
    @{$my_main{"PREF_BVAL_MAX"}} = shift ( @{$commands{$keyword}} ) ; 
    next ;
  KICK:  
    @{$my_main{"PREF_KICK_CR0"}} = shift ( @{$commands{$keyword}} ) ; 
    @{$my_main{"PREF_KICK_CR1"}} = shift ( @{$commands{$keyword}} ) ; 
    next ;

  TARGET: @{$my_main{"PREF_TARGET"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  ATOMS:  @{$my_main{"PREF_ML_MISSIN"}} = shift ( @{$commands{$keyword}} ) ; next ;
  MAP:   @{$my_main{"PREF_MAP_DENS"}} = shift ( @{$commands{$keyword}} ) ; next ;
  XMAP:  @{$my_main{"PREF_MAP_XDIFF"}} = shift ( @{$commands{$keyword}} ) ; next ;
  QMAP:  @{$my_main{"PREF_MAP_XTARG"}} = shift ( @{$commands{$keyword}} ) ; next ;
  RATIO:   @{$my_main{"PREF_SCAL_XTARG"}} = shift ( @{$commands{$keyword}} ) ; next ;

  NCS:   @{$my_main{"PREF_NCS_FLAG"}} = shift ( @{$commands{$keyword}} ) ; next ;
  GNCS:    @{$my_main{"PREF_NCS_FORCE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  CRYST_NCS:    @{$my_main{"PREF_NCS_CRYST"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  FREE:  @{$my_main{"DFR_RFREE"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
}

if ( $PROC_ERROR eq "TRUE" ) { exit ; }

#######################################


$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

###  . ${MAIN_CONF}main_config.data.save.sh

if ( $doit eq "FALSE" )  { exit ; }

######################################

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

my $BVAL_MAX = ${$my_main{"PREF_BVAL_MAX"}}[0] ;
my $BVAL_MIN = ${$my_main{"PREF_BVAL_MIN"}}[0] ;

print MACRO << "END_OF_TEXT" ;
subroutine char SEGMENTS int THE_MAP 

! renames new waters to prevent deletions of already accepted solvents
rename segm WAT sele segm name WAT2 end 
set vari II int segm sele segm name WAT end
if ( II .gt. 0 ) then
set vari WORK_SEGM char global - 
	segm sele segm name WORK_SEGM .o segm name WAT end
! X set vari SEGMENTS char global - 
! X 	segm sele segm name SEGMENTS .o segm name WAT end
end_if

END_OF_TEXT


# multi crystals
my ( $IGR, $NCRYST, $CRYST, $GROUP );

$NCRYST = $my_main{"SG_NCRYST"}[0] ;

if ( $NCRYST > 1 )
{
    print MACRO "define init constr inter \n" ;
    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
    {
	$GROUP = join ( '', "SG_CRYST" , $CRYST+1 ) ;
	print MACRO 
	    "set vari SEG_CRYST = \"", join(" ", @{$my_main{$GROUP}}), "\"\n",
	    "define constr inter \\\n",
	    "sele segm name SEG_CRYST end sele segm name SEG_CRYST end\n" ;
    }
    print MACRO "\n" ;

}

if (  $my_main{"PREF_NCS_CRYST"}[0] ne "ON" ) { $NCRYST = 1 ; } 

#energy stuff and parameters

if ( $NCRYST == 1 ) 
{
print MACRO "<DEF_ALL WORK_SEGM\n" ;
}
else
{
print MACRO "set vari SEGMENTS = \"\\\n" ;
    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
    {
	$GROUP = join ( '', "SG_CRYST" , $CRYST+1 ) ;
	print MACRO join(" ", @{$my_main{$GROUP}}), " \\\n" ;
    }
print MACRO "\"\n" ;
print MACRO "<DEF_ALL SEGMENTS\n" ;


}

print MACRO << "END_OF_TEXT" ;

ener pair off diel = 2.0 ! reduces electrostatics
set weight sele atom name H* end = 0.0 ! kick out Hs from calculation
set temp sele atom name H* end = 0.0 
set temp sele temp -0.1 0.1 .a .not atom name H* end = 30.0 ! sets Bs for new atoms
set temp sele temp $BVAL_MAX   1000. end = $BVAL_MAX  
set temp sele temp $BVAL_MIN  -10000 end = $BVAL_MIN

END_OF_TEXT

# MAPs

my $MAP_CREATED = "NO" ;
my $MAP = $my_main{"PREF_MAP_XTARG"}[0] ;
my $REF_TARGET = $my_main{"PREF_TARGET"}[0] ;

if ( $NCRYST == 1 ) 
{
    if ( $REF_TARGET ne "MAP" )
    {

	if ( $my_main{"PREF_MAP_XTARG"}[0] !~ m"[0-9]" )
	{
	    print MACRO << "END_OF_TEXT" ;
	    ! nmaps + 1	
		set vari MAP_TARG = nmaps + 1
		
END_OF_TEXT
	}
	else
	{
	    if ( $my_main{"SG_NCRYST"}[0] > 1 && $MAP <= $my_main{"SG_NCRYST"}[0] * 2 ) { $MAP = $my_main{"SG_NCRYST"}[0] * 2 + 1 ; }
print MACRO << "END_OF_TEXT" ;

! MAP_TARG as specified
 set vari MAP_TARG = $MAP

END_OF_TEXT
	}
    }

    if ( $REF_TARGET eq "MAP" ) 
    {
	print MACRO << "END_OF_TEXT" ;

 ener  dens on dens map $my_main{"PREF_MAP_DENS"}[0] \\ 
       dens poly 2

END_OF_TEXT

    }
    elsif ( $REF_TARGET eq "DIFF" ) 
    {
    $MAP_CREATED = "YES" ;
    print MACRO << "END_OF_TEXT" ;

 ener  dens on dens map @{$my_main{"PREF_MAP_XDIFF"}}[0] \\ 
       dens poly 2

END_OF_TEXT
    }
    else
    {
	$MAP_CREATED = "YES" ;

	print MACRO << "END_OF_TEXT" ;

  ener x-targ ( \\
                on \\ 
                map MAP_TARG \\
              )

END_OF_TEXT
    }

    if ( $MAP_CREATED eq "YES" ) 
    {
	
	print MACRO << "END_OF_TEXT" ;

 make map MAP_TARG auto_grid 4.0 init cell real 
  if ( MAP_TARG .ne. nmaps ) return

END_OF_TEXT
    }
}
# several crystal forms
else
{
    print MACRO "define init constr map \n" ;
    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
    {
	$MAP =  $CRYST * 2 + 2 ;

	$GROUP = join ( '', "SG_CRYST" , $CRYST+1 ) ;
	print MACRO 
	    "set vari SEG_CRYST = \"", join(" ", @{$my_main{$GROUP}}), "\"\n",
	    "define constr map ", $MAP, 
	    " sele segm name SEG_CRYST end \n",
	    "energy dens ( map $MAP scale 100.0 ) \n" ;
    }
    print MACRO "\n" ;
    if ( $REF_TARGET eq "MAP" ) 
    {

print MACRO << "END_OF_TEXT" ;

 ener  dens on dens map $my_main{"PREF_MAP_DENS"}[0] \\ 
       dens poly 2

END_OF_TEXT

    }
    elsif ( $REF_TARGET eq "DIFF" ) 
    {

	print MACRO << "END_OF_TEXT" ;

 ener  dens on dens map @{$my_main{"PREF_MAP_XDIFF"}}[0] \\ 
       dens poly 2

END_OF_TEXT

    }
    else
    {

	my $macro_file = join ('', $ENV{"MAIN_CONF"}, "create_multi_deriv_maps.pl" ) ; 
	system ( "$macro_file" ) ; 
	print MACRO << "END_OF_TEXT" ;
  set vari MAP_TARG = $MAP
  ener x-targ ( \\
                on \\ 
                map MAP_TARG \\
              )

END_OF_TEXT
    }
}


######## NCS region definition
if ( $my_main{"PREF_NCS_FLAG"}[0] eq "ON" ) 
{
# print " groups @{$my_main{\"SG_NMOL\"}}[0] < @{$my_main{\"SG_NGROUP\"}}[0]  \n" ;
if ( @{$my_main{"SG_NMOL"}}[0] > @{$my_main{"SG_NGROUP"}}[0] )
{
    print MACRO
	"! include all the non equivalent residues here\n", 
	" key out sele .not all end\n",
	" <define_key_out.com\n\n",
	"! defining groups \n" ;

    my ( $IGR, $GROUP );
    my $NCS_START = "FALSE" ;
    for ( $IGR = 1; $IGR <= @{$my_main{"SG_NGROUP"}}[0] ; $IGR++ )
    {
	$GROUP= join ( '', "SG_GROUP" , $IGR ) ;
#	print " groups: $IGR, <$GROUP> \n" ;
	if ( $NCS_START eq "FALSE" )
	{ 
		print MACRO " define init constr ncs \n" ;
		$NCS_START = "TRUE" ;
	    }
	    else
	    { 
		print MACRO " define constraint ncs next \n" ; 
	    }
	
	my $MEMBER ; my $NN_GROUP=0 ;
	    foreach $MEMBER ( @{$my_main{$GROUP}} )
	    {
		$NN_GROUP++;
#	print " member: $MEMBER> \n" ;
#		if ( $NN_GROUP ne 1 ) 
#		{
#		    print MACRO
#			" rms coor all eigen \\ \n",
#			"    sele atom name CA N C O H CB %G* %D* \\ \n",
#			"   .a ( segm name  $MEMBER .a .not out ) end sele int_ncs__1 end \n" ;
#		    print MACRO " define constr ncs sele atom name CA N C O H CB %G* %D* \\ \n",
#		    "          .a ( segm name $MEMBER .a .not out ) end  last_rms polar \n"
#		    } 
#		else
#		{
		    print MACRO " define constr ncs sele atom name CA N C O H CB %G* %D* \\ \n",
		    "          .a ( segm name $MEMBER .a .not out ) end \n"
#		}
	    }
	    print MACRO " define constr ncs force @{$my_main{\"PREF_NCS_FORCE\"}}[0] \n", 
	    " define constr ncs b-force @{$my_main{\"BREF_NCS_FORCE\"}}[0] \n\n" ;
	}
    print MACRO " ener ncs @{$my_main{\"PREF_NCS_FLAG\"}}[0] \n" ;
}
}

###########  KICKS and REFINEMENT

my $STEP_COOL ; my $NCOOL ;

#print " pref_step_cool @{$my_main{\"PREF_STEP_COOL\"}} \n";
if ( @{$my_main{"PREF_STEP_COOL"}}[0] > 1 )
{
    $STEP_COOL = @{$my_main{"PREF_STEP_COOL"}}[0] ;
    $NCOOL = $STEP_COOL ;
# print " pref_step_cool minus 1 @{$my_main{\"PREF_STEP_COOL\"}}[0] \n";
}
else { $NCOOL = 0 ; }

print MACRO << "END_OF_TEXT" ;

mini adjust ( bond = @{$my_main{"PREF_SCAL_XTARG"}}[0] step = 100 )

END_OF_TEXT

my $I=0 ; 
for ( $I=0 ; $I <= $NCOOL ; $I++ )
{

## print " in III  $I cool is $NCOOL - @{$my_main{\"PREF_STEP_COOL\"}}[0] \n";

if ( @{$my_main{"PREF_STEP_COOL"}}[0] == 1 )
{
    print MACRO
	" \n",
	" ! kick : 0.02 very small: 0.33 the largest managable by minimizer \n",
	" set coor kick @{$my_main{\"PREF_KICK_CR0\"}}[0] sele segm name SEGMENTS end \n\n";
}
elsif ( @{$my_main{"PREF_STEP_COOL"}}[0] > 1 )
{
    my $PX = ( $STEP_COOL - $I ) / $STEP_COOL ;
#    print " PARTIAL: ( $STEP_COOL - $I) / $STEP_COOL = $PX \n";
    my $KICK = ( @{$my_main{"PREF_KICK_CR0"}}[0] - @{$my_main{"PREF_KICK_CR1"}}[0] ) *
	$PX + @{$my_main{"PREF_KICK_CR1"}}[0] ;
#    print " PARTIAL: ( $STEP_COOL - $I) / $STEP_COOL = $PX > $KICK \n";
    printf MACRO
	"\n set coor kick %6.3f sele segm name SEGMENTS end \n", $KICK ;
}

else { print MACRO "\n ! no kick applied \n" ; }

if ( $REF_TARGET eq "ML_EST" ) 
{
print MACRO << "END_OF_TEXT" ;

< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK
refl 
move fobs fsave
exit

END_OF_TEXT
}

if ( $REF_TARGET eq "MAP" ) 
{
print MACRO << "END_OF_TEXT" ;

 mini position  sele segm name = SEGMENTS end \\ 
 	 step = $my_main{"PREF_STEP_MIN"}[0] \\ 
 	 writ = 5 

END_OF_TEXT
goto FINISH 
}

print MACRO << "END_OF_TEXT" ;

mini position  sele segm name = SEGMENTS end \\
     step = $my_main{"PREF_STEP_MIN"}[0] \\
     phase \( sele WORK_REFL end \\
END_OF_TEXT
    print MACRO << "END_OF_TEXT" ;
             each_step \\
END_OF_TEXT

	     
if ( $REF_TARGET eq "DIFF" ) 
{    
    print MACRO << "END_OF_TEXT" ;
             step = 15 \\
             gain = 600000.0 \\ 
END_OF_TEXT
}
if ( $my_main{"DFR_RFREE"}[0] eq "ON" )
{    print MACRO "             free sele TEST end \\ \n" ; }
if ( $REF_TARGET eq "MAP" )
{    print MACRO " 	     target ( map ) \\ \n" ; }
elsif ( $REF_TARGET eq "DIFF" )
{    print MACRO " 	     target ( difference ) \\ \n" ; }
elsif ( $REF_TARGET eq "LSQ" )
{    print MACRO " 	     target ( least_square ) \\ \n" ; }
elsif ( $REF_TARGET eq "ML" )
{    print MACRO " 	     target ( max_like ) \\ \n" ; 
     if ( $my_main{"DFR_RFREE"}[0] ne "ON" ) 
     { print "ML refinement - R-FREE FLAG NOT SET?\n" ; }
}
elsif ( $REF_TARGET eq "ML_EST" )
{    print MACRO " 	     target ( estim_ml missing @{$my_main{\"PREF_ML_MISSIN\"}}[0] ) \\ \n" ; }
elsif ( $REF_TARGET eq "ML_APR" )
{    print MACRO " 	     target ( aprox_ml ) \\ \n" ; }

# mutli crystal phasing
if ( $NCRYST > 1 ) 
{    print MACRO " 	     macro = multi_deriv_maps.com \\ \n" ; }

print MACRO "            ) \\ \n" ; 
if ( $REF_TARGET eq "DIFF" ) 
{    
    print MACRO << "END_OF_TEXT" ;
             writ = 5 

END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
     writ = 1  

END_OF_TEXT
}




FINISH:
} 

####### return to interactive local minimizer

if ( $MAP_CREATED eq "YES" ) 
{
print MACRO << "END_OF_TEXT" ;
delete map MAP_TARG
END_OF_TEXT
}

if ( $NCRYST > 1 ) 
{
print MACRO << "END_OF_TEXT" ;
define init constr map
END_OF_TEXT
}

if ( $my_main{"PREF_NCS_FLAG"}[0] eq "ON" ) 
{
print MACRO << "END_OF_TEXT" ;
energy ncs off
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;
 ! reset scale for MINIMIZE (later on ...)
 ener  dens ( on map MAP_2FOFC poly 2 ) \\ 
       ncs off
 mini position write 10 step 200
mini adjust ( bond = 0.02 step = 100 )

return
END_OF_TEXT

close(MACRO) ;

