Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WOUT2INDATA] add a tool that puts the plasma boundary geometry from a VMEC wout file into a new INDATA namelist #245

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ xvmec2stel
xvmec2v690
xvmec2xgc
xwallacc
xwout2indata
COILOPT++
GENE
TERPSICHORE
Expand Down
1 change: 1 addition & 0 deletions WOUT2INDATA/Debug/makewout2indata
2 changes: 2 additions & 0 deletions WOUT2INDATA/ObjectList
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ObjectFiles = \
wout2indata_main.o
1 change: 1 addition & 0 deletions WOUT2INDATA/Release/makewout2indata
132 changes: 132 additions & 0 deletions WOUT2INDATA/Sources/wout2indata_main.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
!-----------------------------------------------------------------------
! Program: WOUT2INDATA
! Authors: J. Schilling
! Date: 06/04/2024
! Description: The WOUT2INDATA code reads a VMEC2000 INDATA
! namelist and a VMEC2000 WOUT file from
! the current LIBSTELL library and writes out
! a new INDATA namelist with the plasma boundary
! from the WOUT file.
! References:
!-----------------------------------------------------------------------
PROGRAM WOUT2INDATA
!-----------------------------------------------------------------------
! Libraries
!-----------------------------------------------------------------------
USE vmec_input, ONLY: lasym, mpol, ntor, rbc, zbs, rbs, zbc, &
read_indata_namelist, &
write_indata_namelist
USE read_wout_mod, ONLY: lasym_in => lasym, &
nfp_in => nfp, &
mpol_in => mpol, &
ntor_in => ntor, &
ns_in => ns, &
mnmax_in => mnmax, &
xm_in => xm, &
xn_in => xn, &
rmnc_in => rmnc, &
zmns_in => zmns, &
rmns_in => rmns, &
zmnc_in => zmnc, &
read_wout_file
USE safe_open_mod
!-----------------------------------------------------------------------
! Local Variables
!-----------------------------------------------------------------------
IMPLICIT NONE
integer :: numargs, ierr, iunit
integer, parameter :: arg_len = 256, &
iunit_init = 11
character*(arg_len) :: input_filename
character*(arg_len) :: wout_filename
character*(arg_len) :: output_filename

character(len=1024) :: line
integer :: mn, m, n

!-----------------------------------------------------------------------
! Begin Program
!-----------------------------------------------------------------------

! First Handle the input arguments
CALL GETCARG(1, input_filename, numargs)
if (numargs .lt. 2) then
write(*,*) "usage: xwout2indata input.<ext> wout_<ext>.nc"
write(*,*) " will write to input.<ext>_new"
call exit(255)
end if
input_filename = TRIM(input_filename)
CALL GETCARG(2, wout_filename, numargs)
wout_filename = TRIM(wout_filename)

! Read the INDATA namelist.
iunit = iunit_init
CALL safe_open(iunit, ierr, input_filename, 'old', 'formatted')
IF (ierr .eq. 0) THEN
CALL read_indata_namelist(iunit, ierr)
if (ierr .ne. 0) then
backspace(iunit)
read(iunit, fmt='(A)') line
write(6,'(A)') 'Invalid line in INDATA namelist: '//TRIM(line)
call exit(ierr)
end if
ELSE
WRITE(*,*) 'Error reading input file: ',TRIM(input_filename)
call exit(ierr)
END IF
CLOSE(unit=iunit)

! Read the wout file.
CALL read_wout_file(wout_filename,ierr)
IF (ierr .ne. 0) THEN
WRITE(*,*) 'Error reading wout file: ',TRIM(wout_filename)
call exit(ierr)
END IF

! Note that we explicitly don't check
! if Fourier resolution, number of field periods, etc.
! in the `wout` file is equal to their equivalents
! in the given `indata` file.
! This is to allow maximum flexibility in the use of this tool.

! To make sure that the full set of Fourier coefficients
! is available in the new input file, we overwrite `lasym`,
! `mpol` and `ntor` with the values from the wout file.
lasym = lasym_in
mpol = mpol_in
ntor = ntor_in

! Replace the boundary geometry in the INDATA namelist
! with the plasma boundary coefficients from the wout file.
do mn = 1, mnmax_in
m = xm_in(mn)
n = xn_in(mn) / nfp_in

rbc(n, m) = rmnc_in(mn, ns_in)
zbs(n, m) = zmns_in(mn, ns_in)
if (lasym_in) then
rbs(n, m) = rmns_in(mn, ns_in)
zbc(n, m) = zmnc_in(mn, ns_in)
end if
end do

! Write the new INDATA namelist.
output_filename = trim(input_filename) // "_new"
iunit = iunit_init
CALL safe_open(iunit, ierr, output_filename, 'replace', 'formatted')
IF (ierr .eq. 0) THEN
CALL write_indata_namelist(iunit, ierr)
if (ierr .ne. 0) then
WRITE(*,*) 'Error writing INDATA namelist'
call exit(ierr)
end if
ELSE
WRITE(*,*) 'Error opening file for writing: ',TRIM(output_filename)
call exit(ierr)
END IF
CLOSE(unit=iunit)

!-----------------------------------------------------------------------
! End Program
!-----------------------------------------------------------------------
END PROGRAM WOUT2INDATA
4 changes: 4 additions & 0 deletions WOUT2INDATA/WOUT2INDATA.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wout2indata_main.o: \
../../LIBSTELL/$(LOCTYPE)/read_wout_mod.o \
../../LIBSTELL/$(LOCTYPE)/safe_open_mod.o \
../../LIBSTELL/$(LOCTYPE)/vmec_input.o
50 changes: 50 additions & 0 deletions WOUT2INDATA/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#Type "make release(debug)" at command line to make release(debug) version
#DO NOT alter the directory structure or move this makefile (must be in first subdirectory below top_level_dir)
include ../make.inc
DBG = Debug
REL = Release
REL_PATH = $(PWD1)/$(REL)
DBG_PATH = $(PWD1)/$(DBG)
EXE_F = xwout2indata
MAKE = makewout2indata
LIB_DIR = $(MYHOME)/libstell_dir

.PHONY: release debug clean_release clean_debug static_release

release:
@echo "Beginning release compilation and linking of wout2indata"
@cd $(REL); rm -f *.f *.f90 *.c; rm -f $(MAKE); ln -fs ../$(MAKE) .; make -f $(MAKE) $(EXE_F) FLAGS="$(FLAGS_R) $(MOD1_PATH)" TYPE="release" LOCTYPE="Release"
@rm -f $(REL)\*.f; rm -f $(REL)\*.f90
@rm -f $(MYHOME)/$(EXE_F)
@-(umask 022; rm -f $(MYHOME)/$(EXE_F); ln -fs $(REL_PATH)/$(EXE_F) $(MYHOME) 2> /dev/null; umask 077)
@echo "Release version - xwout2indata - is now updated"
@echo "To free up space, delete all *.o files from Release"
@echo ""

debug:
@echo "Beginning debug compilation and linking of wout2indata"
@cd $(DBG); rm -f *.f *.f90 *.c; rm -f $(MAKE); ln -fs ../$(MAKE) .; make -f $(MAKE) $(EXE_F) FLAGS="$(FLAGS_D) $(MOD1_PATH)" TYPE="debug" LOCTYPE="Debug"
@rm -f $(MYHOME)/$(EXE_F)
@-(umask 022; rm -f $(MYHOME)/$(EXE_F); ln -fs $(DBG_PATH)/$(EXE_F) $(MYHOME) 2> /dev/null; umask 077)
@echo "Debug version - xwout2indata - is now updated"
@echo "To free up space, delete all *.o files from Debug"
@echo ""

clean_release:
@echo "CLEAN REMAKE IN PROGRESS"
@cd $(REL); make -f $(MAKE) clean
@make release

clean_debug:
@echo "CLEAN REMAKE IN PROGRESS"
@cd $(DBG); make -f $(MAKE) clean
@make debug

static_release:
@echo "MAKING STATIC (RELEASE) EXECUTABLE"
@cd $(REL); rm -f *.f *.f90 *.c; rm -f $(MAKE); ln -fs ../$(MAKE) .; make -f $(MAKE) $(EXE_F) FLAGS="$(FLAGS_R) $(MOD1_PATH)" SFLAGS="" TYPE="release"
@rm -f $(MYHOME)/$(EXE_F)
@-(umask 022; rm -f $(MYHOME)/$(EXE_F); ln -fs $(REL_PATH)/$(EXE_F) $(MYHOME) 2> /dev/null; umask 077)
@echo "Static Release version - xwout2indata - is now updated"
@echo "To free up space, delete all *.o files from Release"
@echo ""
62 changes: 62 additions & 0 deletions WOUT2INDATA/makewout2indata
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#This makefile goes in dirs Debug and Release (first level below WOUT2INDATA)
include ../../make.inc
FFILE = '$*''.f'
CFILE = '$*''.c'
F90FILE = '$*''.f90'
SPATH = ../Sources
#Contains list of source files (.o) and dependencies
DEPLIST = ../WOUT2INDATA.dep
OBJLIST = ../ObjectList

#Includes source files and dependency list
include $(DEPLIST)
include $(OBJLIST)
VPATH = $(SPATH)

.SUFFIXES :
.SUFFIXES : .f .f90 .o
xwout2indata: $(LIB) $(ObjectFiles)
$(LINK) $@ $(ObjectFiles) $(LIB_LINK)
#Compile object files defined in OBJLIST.
.f.o :
@if grep -q '^!DEC\$$' $<; \
then \
awk -f $(HOME_BIN)/awk_cdir.awk $< > $(CFILE) ; \
echo '$(PRECOMP) $<'; $(PRECOMP) $(CFILE) $(FFILE); \
rm -f $(CFILE); echo '$(COMPILE) $(FLAGS) $(MOD_PATH).. -c $<'; \
$(COMPILE) $(FLAGS) $(MOD_PATH).. -c $(FFILE); \
elif grep -q '^#if' $<; \
then \
cp $< $(CFILE); \
echo '$(PRECOMP) $<'; $(PRECOMP) $(CFILE) $(FFILE); \
rm -f $(CFILE); echo '$(COMPILE) $(FLAGS) $(MOD_PATH).. -c $<'; \
$(COMPILE) $(FLAGS) $(MOD_PATH).. -c $(FFILE); \
else \
echo '$(COMPILE) $(FLAGS) $(MOD_PATH). -c $<'; \
$(COMPILE) $(FLAGS) $(MOD_PATH). -c $<; \
fi

.f90.o :
@if grep -q '^!DEC\$$' $<; \
then \
awk -f $(HOME_BIN)/awk_cdir.awk $< > $(CFILE) ; \
echo '$(PRECOMP) $<'; $(PRECOMP) $(CFILE) $(F90FILE); \
rm -f $(CFILE); echo '$(COMPILE_FREE) $(FLAGS) $(MOD_PATH).. -c $<'; \
$(COMPILE_FREE) $(FLAGS) $(MOD_PATH).. -c $(F90FILE); \
elif grep -q '^#if' $<; \
then \
cp $< $(CFILE); \
echo '$(PRECOMP) $<'; $(PRECOMP) $(CFILE) $(F90FILE); \
rm -f $(CFILE); echo '$(COMPILE_FREE) $(FLAGS) $(MOD_PATH).. -c $<'; \
$(COMPILE_FREE) $(FLAGS) $(MOD_PATH).. -c $(F90FILE); \
else \
echo '$(COMPILE_FREE) $(FLAGS) $(MOD_PATH). -c $<'; \
$(COMPILE_FREE) $(FLAGS) $(MOD_PATH).. -c $<; \
fi

#Check that library files are current.
$(LIB) :
@cd $(LIB_DIR); make $(TYPE)

clean:
- rm -f *.o *.mod
2 changes: 1 addition & 1 deletion build_all
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ shift $((OPTIND-1))

# Loop through all the available codes and make the ones specified on the
# command line. If no codes were specified, then make all of them.
for CODE in LIBSTELL ANIMEC BCYCLIC BEAMS3D BOOTSJ BNORM BOOZ_XFORM COBRAVMEC COILOPT DESCUR DESCUR_PLOT DIAGNO DKES FIELDLINES J_INVARIANT MAKEGRID NEO NESCOIL PENTA THRIFT TORLINES VMEC2000 VMEC2PIES VMEC2SPEC VMEC2STEL VMEC2V690 VMEC2XGC THRIFT STELLOPTV2 WALL_ACCELERATE
for CODE in LIBSTELL ANIMEC BCYCLIC BEAMS3D BOOTSJ BNORM BOOZ_XFORM COBRAVMEC COILOPT DESCUR DESCUR_PLOT DIAGNO DKES FIELDLINES J_INVARIANT MAKEGRID NEO NESCOIL PENTA THRIFT TORLINES VMEC2000 VMEC2PIES VMEC2SPEC VMEC2STEL VMEC2V690 VMEC2XGC THRIFT STELLOPTV2 WALL_ACCELERATE WOUT2INDATA
do
match=0

Expand Down