-----------------------------------------------------------------------
--
-- Copyright (c) 2012 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.
--
--
--
--                                      T.Yoshida   Tue. Jan. 17, 2012
-----------------------------------------------------------------------


-----------------------------------------------------------------------
--- function  DetectMaxPosition                                   -----
---                                                               -----
---   ExpFile: target Export file                                 -----
---      rMax: reference max value (not actual max value)         -----
---    posMax: {x=,  y=, z=}                                      -----
---                                                               -----
-----------------------------------------------------------------------
	function DetectMaxPosition(ExpFile, rMax, posMax)


	dumMax=rMax-1000.0

	fp_exp=openfile(ExpFile,"r")
	charLine=read(fp_exp,"*l")          -- skip 1st-header
	nRow=read(fp_exp,"*l")              -- detect data row number
	charLine=read(fp_exp,"*l")          -- skip 3rd-header

	for i=1, nRow  do                   -- max detecting loop 

		-- print("nRow: "..i)
		x=read(fp_exp,"*n")
		y=read(fp_exp,"*n")
		z=read(fp_exp,"*n")
		scalar=read(fp_exp,"*n")
		u=read(fp_exp,"*n")
		v=read(fp_exp,"*n")
		w=read(fp_exp,"*n")
		threshold=read(fp_exp,"*n")

		if  scalar > dumMax then        -- in case of max detection
			dumMax=scalar
			posMax.x=x
			posMax.y=y
			posMax.z=z
		end

	end

	closefile(fp_exp)

	return 0
	end -- function DetectMaxPosition()





--------------------------------------------------------------------------------
--            DATA INPUT
--------------------------------------------------------------------------------
local dataset_1_info_table = read_dataset( {
    data_format = "unstructured",
    input_parameters = {
--        name = "./sample.uns",
        name = "./FF065_sample.uns",
        options = {
            input_mode = "replace",
            boundary_only = "off"
        } -- options
    } -- input_parameters
} ) -- read_dataset

-- print_dataset_table( dataset_1_info_table )


--------------------------------------------------------------------------------
--            BOUNDARY SURFACES
--------------------------------------------------------------------------------

bound_handle = create_boundary(
    {
        geometric_color = 4,
        types = {
            "tube",
        }, -- types
		transparency=0.5,
        display_type = "smooth_shading",
        contours = "none",
        show_mesh = "off",
        number_of_contours = 16,
        vector_func = "none",
        scalar_func = "none",
        visibility = "on",
        threshold_func = "none",
        line_type = "thin",
        dataset = 1,
    }
) -- bound_table


--------------------------------------------------------------------------------
--            COORDINATE SURFACES
--------------------------------------------------------------------------------

coord_handle = create_coord(
    {
		axis="X",
        transparency = 0,
        show_mesh = "off",
        number_of_contours = 16,
        scalar_range = {use_local = "on"},
        visibility = "on",
        line_type = "thin",
        display_type = "smooth_shading",
        contours = "none",
        scalar_func = "p",
        vector_func = "none",
        dataset = 1,
    }
) -- coord_table



	extract_table=query(coord_handle)
	Xmin=extract_table.X_axis.min
	Xmax=extract_table.X_axis.max


	nDiv=5
	deltaX=(Xmax - Xmin)/nDiv
	Xcur=Xmin

	print("-------------------")
	print("Xmin:"..Xmin)
	print("Xmax:"..Xmax)
	print("deltaX:"..deltaX)
	print("-------------------")


	fp =openfile("./MaxProfile.txt","w")
	ExpFile="./coord_export.txt"

	write(fp, "  X,  x,  y,  z, max. pressure\n")
	write(fp, "  --------------------------------------\n")

	for i=1, nDiv  do                  -- loop for Z-direc

		fp2 =openfile(ExpFile,"w")


		Xcur=Xcur + deltaX             -- sweeping position
		print("Xcur:"..Xcur)


                                       -- update surface
		modify(coord_handle, {X_axis = { current = Xcur}})

                                       -- export "surface" data 
		fv_script("EXPORT COORD "..ExpFile)

                                       -- extract min. max. value
		extract_table=query(coord_handle)
		TempMax=extract_table.scalar_range.max


		posMax={x=-999.9,
				y=-999.9,
				z=-999.8}              -- initialize table

                                       -- detect max position
		DetectMaxPosition(ExpFile, TempMax, posMax)


		strLine=format(" %8.5e  %8.5e  %8.5e  %8.5e  %8.5e \n", Xcur, posMax.x, posMax.y, posMax.z, TempMax)
		write(fp, strLine)

		closefile(fp2)

	end


	write(fp, "\n")
	closefile(fp)

