-----------------------------------------------------------------------
--
-- Copyright (c) 2011 VINAS
-- All rights reserved.
--
-- This sample FVX script is not supported by VINAS and VINAS provides
-- no warranties or assurances about its fitness or merchantability.
-- It is provided at no cost and is for demonstration purposes only.
--
--                                      created on tue. mar. 29, 2011
-----------------------------------------------------------------------

-----------------------------------------------------------------------
--- function  UpdatePara2               ---- 該当パラメータの更新 -----
---                                     ---- restart file を指定して --
---                                     ----  任意パラメータを上書き --
---                                   ----   version 2 : 2009/01/27  --
---    ostype: 1: Windows,  2:Linux                               -----
---   orgFile: 更新ターゲットとなる restart file                  -----
---   Keyword: 設定パラメータを表すキーワード                     -----
---  charPath: 上書きする文字列                                   -----
-----------------------------------------------------------------------
	function UpdatePara2(ostype, orgFile, Keyword, charPatch)


	if (ostype == 1) then               ---- Windows であれば "copy"
		CommandString="copy "..orgFile.." ".."tmp"
	elseif (ostype == 2) then           ---- Linux であれば  "cp"
		CommandString="cp -f "..orgFile.." ".."tmp"
	end
	execute (CommandString)             ---- ファイルのコピー

	----                                ---- Row count
	fp_orig=openfile("tmp","r")
	nRow=0
	repeat
		ny=read(fp_orig,"*l")
		if (ny ~= nil) then
			nRow=nRow+1
		end
	until (ny == nil)
	closefile(fp_orig)
	print("nRow", nRow)

	----                                ---- store parameter line characters
	charString={}
	fp_orig=openfile("tmp","r")
	for i=1, nRow do
		charString[i]=read(fp_orig, "*l")
	end
	closefile(fp_orig)

	----                                ----  substitute for target parameter
	fp_upd =openfile(orgFile,"w")
	nKeyword=strlen(Keyword)
	for i=1, getn(charString) do
		print(charString[i])

		charTmp=strsub(charString[i], 1, nKeyword)
		print(charTmp)

		if( charTmp == Keyword )then
			write(fp_upd, Keyword.." "..charPatch, "\n")
		else
			write(fp_upd, charString[i], "\n")
		end

	end
	closefile(fp_upd)

	return 0
	end -- function UpdatePara2()

-----------------------------------------------------------------------
-----------------------------------------------------------------------



-----------------------------------------------------------------------
---                                                               -----
---                   main  program                               -----
---                                                               -----
-----------------------------------------------------------------------

	---- define parameters ----
	nRevolve=4                          -- revolving number
	dtheta = 5.0                        -- theta increment

	thresh_init=42.5
	thresh_term=48.0
	thresh_incr=0.085


	---- preparing input dataset 
	data_input_table = {
		data_format = "unstructured",
		input_parameters = {
				name = "./SD015_spiral_tube_sample.uns",
				options = {
					input_mode = "replace",
					transient = "off"
				},
		}
	}

	---- read dataset
	SampleTableHandle= read_dataset(data_input_table)

	---- read restart files
	-- fv_script("RESTART ALL sample.dat")
	fv_script("RESTART BOUNDARY sample.bnd")
	fv_script("RESTART VIEW sample.vct")




	---- define coordinate surface
	tmp_table = {
		dataset = 1,
		scalar_func = "p",
		-- vector_func = "U",

		-- visibility = "off",
		visibility = "on",

		axis = "T",
		T_axis = {current = 0.0},

		display_type = "smooth_shading",

	}


	---- handling coordinate surface
	tmp_handle=create_coord(tmp_table)
	coord_table=query(tmp_handle)
	modify(tmp_handle, coord_table)

	---- save restart file
	fv_script("SAVE COORD sample.crd")


	---- check
	curTheta=coord_table.T_axis.current
	print("curTheta:"..curTheta)


	---- animation recording ----
	fv_script("RECORD ON Coil_Coordinate.avi")


	for i= 1,  360.0*nRevolve, dtheta do

		theta=i
		if theta  > 360.0 then
			theta=theta - floor(theta/360)*360
            -- theta=abs(theta)
		end


		


		--- print("theta:"..theta)

		---- save restart file
		fv_script("SAVE COORD sample.crd")





		---------------------------------------
		---- update restart file
		---------------------------------------
		CrdRestart="sample.crd"

		----
		Keyword="zmin: "
		-- charPatch="1.331380e+001"
		charPatch=thresh_init
		nCheck=UpdatePara2(1, CrdRestart, Keyword, charPatch)

		----
		Keyword="zmax: "
		-- charPatch="2.645281e+001"
		charPatch=thresh_term
		nCheck=UpdatePara2(1, CrdRestart, Keyword, charPatch)

		----
		Keyword="tcur: "
		charPatch=theta
		nCheck=UpdatePara2(1, CrdRestart, Keyword, charPatch)

		----
		Keyword="use_local_scalar_minmax: "
		charPatch=1
		nCheck=UpdatePara2(1, CrdRestart, Keyword, charPatch)

		----
		fv_script("RESTART COORD sample.crd")

		thresh_init=thresh_init-thresh_incr
		thresh_term=thresh_term-thresh_incr

	end


	fv_script("RECORD OFF")


