# needs description
#  E = - grad \phi
#  f = q E 

# NOTE tangent is constant for LAGRANGIAN but not exact...
# NOTE try one atom and one free node

# issue is one of magnitude of E since
# tangent = perm BB - dn/dphi NN
# CHECK CONDITIONING?

echo both
units		metal
atom_style	atomic

lattice       diamond  3.6
boundary      f f f

#region  box block -4.3458792459312239 4.3458792459312328 -4.3458792459312168 4.3458792459312203 0.0 104.29551668 units box
#region  box block 0 10 0 10 0 10
#create_box 1 box
#group   box region box
#atom_modify  sort 0 1

pair_style    tersoff
read_data  tube_8_4.data
pair_coeff    * * ../../../../potentials/SiC.tersoff  C
mass          *  12.01

# PARAMETERS-----------------------------
variable L equal zhi-zlo
variable R equal 12.1/2
variable xhiFE equal 5.0*$R
variable xloFE equal -${xhiFE}
variable yhiFE equal $R
variable yloFE equal -${xhiFE}
variable zhiFE equal zhi
variable zloFE equal zlo+10
print "Length $L [${zloFE}, ${zhiFE}]"

variable E equal  0.01  # 1.0 10.0 0.01
variable V equal  $E*${zloFE} 
variable V equal  2
print "Electric field $E ref.voltage $V"
variable s equal  20
# END -----------------------------------

# all atoms simulation
region		feRegion block ${xloFE} ${xhiFE} ${yloFE} ${yhiFE} ${zloFE} ${zhiFE} units box
group		internal region feRegion

variable nAll   equal count(all)
variable nGhost equal count(all)-count(internal)
print ">>> number of stationary ghosts:  ${nGhost} of ${nAll}"

#neighbor	5. bin
#neigh_modify	every 10 delay 0 check no
timestep        0.0005

# coupling
fix         AtC internal   atc electrostatic-equilibrium CNT_electrostatic2.mat
fix_modify  AtC omit atomic_charge 
#fix_modify  AtC internal_quadrature off <<<< ???
variable alat equal 1.42
variable w equal ${alat}*${alat}*3.*sqrt(3.)/4.0
#variable w equal 10
fix_modify  AtC atom_weight constant internal $w
fix_modify  AtC source_integration atom
fix_modify  AtC atom_element_map eulerian 1
fix_modify  AtC mesh create 5  1  12  feRegion f p f
fix_modify  AtC control  momentum flux
#fix_modify  AtC extrinsic poisson_solver max_iterations 5

# initial conditions
fix_modify  AtC  initial displacement x all 0.0
fix_modify  AtC  initial displacement y all 0.0
fix_modify  AtC  initial displacement z all 0.0
fix_modify  AtC  initial velocity x all 0.0
fix_modify  AtC  initial velocity y all 0.0
fix_modify  AtC  initial velocity z all 0.0

#variable a equal -$R-0.1
#variable b equal  $R+0.1
#fix_modify  AtC mesh create_nodeset tube $a $b $a $b ${zloFE} ${zhiFE} units box
fix_modify  AtC mesh create_nodeset lbc ${xloFE} ${xhiFE} ${xloFE} ${xhiFE} ${zloFE} ${zloFE} units box
fix_modify  AtC mesh create_nodeset rbc ${xloFE} ${xhiFE} ${xloFE} ${xhiFE} ${zhiFE} ${zhiFE} units box
fix_modify  AtC mesh create_nodeset bot ${xloFE} ${xloFE} ${yloFE} ${yhiFE} ${zloFE} ${zhiFE} units box
fix_modify  AtC mesh create_nodeset top ${xhiFE} ${xhiFE} ${yloFE} ${yhiFE} ${zloFE} ${zhiFE} units box

# boundary conditions
fix_modify AtC  fix displacement x lbc 0.
fix_modify AtC  fix displacement y lbc 0.
fix_modify AtC  fix displacement z lbc 0.
fix_modify AtC  fix velocity     x lbc 0.
fix_modify AtC  fix velocity     y lbc 0.
fix_modify AtC  fix velocity     z lbc 0.
fix_modify AtC  fix electric_potential lbc linear 0 0 0 $E 0 0 $V
#fix_modify AtC  fix electric_potential rbc linear 0 0 0 $E 0 0 $V
##fix_modify AtC  fix electric_potential top linear 0 0 0 $E 0 0 $V
fix_modify AtC  fix electric_potential bot linear 0 0 0 $E 0 0 $V

# run
thermo_style    custom step cpu etotal ke
thermo          $s
fix_modify      AtC  output cnt_electrostatic2FE $s full_text # binary
fix_modify      AtC  output index step      
# NOTE not recognized as vector by paraview
variable uX atom x-f_AtC[1]
variable uY atom y-f_AtC[2]
variable uZ atom z-f_AtC[3]
variable rho atom mass*f_AtC[4]
dump  CONFIG all custom $s cnt_electrostatic2.dmp id type x y z v_uX v_uY v_uZ v_rho
log cnt_electrostatic2.log

#run   	        $s
run   	        100

# NOTE try fix charge on tip
