#!/usr/local/bin/psmod

# {} - section delimiters
# [] - table delimiters
# () - controller delimiters

global
{
	alias	dc distrubution_constrain
	prepend_filenames_with_source_name	true
	random_seed	3
}

particles "sys1_particles"
{
	dc
	{
		range 1 149
		linear interval 2700 2700
	}
	
	density [uniform ]
	dc { range 0 0 }
	density [5100] # 'dc reset constrains' unnecessary: constant table
	dc
	{
		range 1 149
		linear union
		{
			interval 1e-6 1.5e-6
			interval 2e-6 3e-6
		}
	}
	radius [uniform]
	dc { range 0 0 }
	radius [1e-5]
}


/*
typedef struct linear_dist_s
{
	char		name[MAX_ID];
	vec_t		min, max, spacing;
	vboolean	(*generate)(struct linear_dist_s *self,vsize_t cur_idx,vec_t *base);
}	linear_dist_t, *linear_dist_p;

typedef struct spatial_dist_s
{
	char		name[MAX_ID];
	vec_t		spacing;
	vboolean	(*generate)(struct spatial_dist_s *self,vsize_t cur_num,vec3_t base,vec_t *sizes);
}	spatial_dist_t, *spatial_dist_p;

*/

linear uniform distribution
{
	segment
	{
		min		1e-6
		max		3e-6
	}
}	"sys1_diameters";

/*
	for(;;)
	{
		get(token);
		if(keyword_token(token))
			break;
		push(modifier_stack,token);
	}
	sect = parse_section(modifier_stack,token);
	flush(modifier_stack);
	assert(lexNext());
	if(!lexLastIs(...,LEX_SEMICOLON)) // named section
	{
		hashSetPointer(sections,lexLast(),sect);
		assert(lexNextIs(...,LEX_SEMICOLON))
	}
	return sect;
*/

linear gaussian "sys1_diameters2"
{
	mean		2e-6
	stdev		1e-6
	segment
	{
		min		1e-6
		max		3e-6
	}
}

linear constant "sys1_densities"
{
	value		2700
}

spatial	uniform "sys1_bounds"
{
	intersection # union, subtraction
	{
		subtraction
		{
			sphere [0,0,0] 10e-6
			sphere [0,0,0] 3e-6
		}
		box [0,0,5e-6;10e-6,10e-6,10e-6]
	}
}

spatial	gaussian "sys1_bounds"
{
	mean		2e-6
	variance	5e-7

	box [0,0,5e-6;10e-6,10e-6,10e-6]
	}
}


system	"sys1"
{
	amount		100
#	current range 0...99
	range		[0,9]
	{
#	current range 0...9
		diameter	"sys1_diameter"
	}
	range		10	99
	{
		diameter	"sys1_diameter2"
	}
#	current range 0...99
	density		"sys1_density"
	
	
	
}

linear_distribution
{

	mean		0
	variance	

initial "sys1_initial"
{
	dc { range 0 0 }
	position [uniform3d "sys1_bounds"]
#	position [gaussian3d([0,0,0],[1e-6,1e-6,1e-6]) "sys1_bounds"]
	/*
	dc
	{
	}
	*/
	dc
	{
		range 1 50
		spacing 2e-7
		# spherical shell
		spatial subtraction
		{
			sphere [0,0,0] 10e-6
			sphere [0,0,0] 3e-6
		}
		timeout 10
	}
	position [uniform]
	dc { range 0 150 }
	position [file "db.txt"]
	# interaction (lj [-1e-17;1e-7])
	interaction (custom_power [-1e-24;-1])
}

environment "sys1_env"
{
	viscousity (const [1e-8])
	velocity (vec3_time_linear [3;5] [0,0,0;2e-4,0,0])
	# velocity (vec3_curve [100] [file "vel_spd"])
}

extra "sys1_extra"
{
	# force (const [0,0,0])
	# accel (const [0,0,-9.8e-7])
}

pss_monitor	"pmon"
{
	filename output.pss
	skips	0 # activate at every call
    flushes	true
	once
    {
    	print "#!/usr/bin/pssview\n"
		print "# Script: %s, job %s.\n",$(SOURCE),$(TASK)
		print "# This a machine-generated file, don't edit!\n"
		print "amount %u\n",$AMOUNT
    }
    periodic
    {
    	int limit;
		print "time %g\n",$(TIME);
		print "position ";
        limit = ($AMOUNT,1,-);
		for i = 0 to limit
			print "\t[%g,%g,%g]; [%g,%g,%g]\n",$(POSITION)[i],$(VELOCITY)[i]
	}
}

mxs_monitor "ms_out"
{
# fixme: should we pass all unknown commands to script (or use 'write' command) ???
# if yes, how to combine it with R4->R2 translations ???
# fixme: should we use info produced by 'showclass "*.*"' ???
	target R2 # perform translation from R4 names to R2 names
	skips 0
	prepend_with_source_name true
	filename "_mxs_output.ms"
	map "cellular_map"
	{
		type		Cellular
		cellColor	color	[0,0,0]
		DivColor1	color	[0.5,0.5,0.5]
		DivColor2	color	[1,1,1]
		Variation	0
		Size		0.5
		Spread		0.01
		LowThresh	0
		MidThresh	0.5
		HighThresh	1
		Type		Cellular # | Chips
		Fractal		false
	}
	material "m1"
	{
		type		StandardMaterial
		# R2 shaders: Constant, Phong, Metal, Blinn
		# R4 shaders: Anisotropic, Blinn, Metal, Multi_Layer. Oren_Nayar_Blinn, Phong, Strauss
		ShaderType	Blinn # can be only R2 shader since target == R2
		TwoSided	false
		Wire		false
		Ambient		color	[0,0,0]
		Diffuse 	color	[1,1,0]
		Specular	color	[1,1,1]
		SpecularLevel	90
		Glossiness		20
		SelfIllumAmount	40
		Opacity			60
		OpacityType		Additive # | Filter | Subtractive
		OpacityFalloff	20
		maps
		{
			Diffuse	0.4	"cellular_map"
		}
	}
	material "m2"
	{
		Source	"m1"
		Wire	true
	}
	Background	color [0,0,0]
	
	# noflush
	scale 1e7
	particle_material "m1"
	planar_constrain_material "m2"
	spherical_constrain_material "m2"
	cylindrical_constrain_material "m2"
}

drw_monitor "wire_draw"
{
	view_width 1024
	view_height 768
	max_fps 30
	antialias true
	use_camera false
	variable_linewidth false
	background_color [1,1,1]
	particle_color [0,0,0.5]
	inner_grid_color [0,0.8,0.6]
	outer_grid_color [0,0.8,0.6]
	detailed_particles true
	draw_planar_constrains true
	planar_constrains_color [1,0,0]
	draw_spherical_constrains true
	spherical_constrains_color [1,0,0]
	draw_cylindrical_constrains true
	cylindrical_constrains_color [1,0,0]
}

bkp_monitor "backup" # setup for backup monitor
{
	skips 9 # activate at every 10th call
	prepend_with_source_name true
	save	"_backup.phase" # will be overwritten at any call
	save	"_%06u.phase" # will not be overwritten - contains valid format pattern for sprintf()
}

solution
{
	system "system_1"
	{
		particles sys1_particles
		initial sys1_initial
		boundary sys1_boundary
		environment sys1_env
		extra sys1_extra
	}
	# only "load", "save", "resume", "default", "stage" are allowed;
	# there can be no "load" if already there is a "resume";
	# only one "save" allowed (will be executed after _last_ stage).
	prepend_with_source_name true
	resume "_backup.phase" # same as 'load' but no warnings even if file doesn't exists
	default # will be prepended to each stage
	{
		monitor_cps 10
		RK_timeout 10
		realtime_limit 600
	}
	stage
	{
		monitor "backup"
		monitor "wire_draw"
		upper_time 10
		save "_stage1_end.phase"
	}
	stage
	{
		# monitor_cps 10
		RK_timeout 5
		realtime_limit 300
		# load "_stage1_end.phase"
		RK_tolerance 1e-2
		monitor "wire_draw"
		upper_time 20
		save "_stage2_low_end.phase"
	}
	stage
	{
		# monitor_cps 10
		RK_timeout 25
		realtime_limit 3000
		load "_stage1_end.phase" # after 'load', solver's time is 10, not 20
		RK_tolerance 1e-4 # perform calculation with increased precision
		monitor "wire_draw"
		upper_time 20
		save "_stage2_hi_end.phase" # may be outside stage section
	}
}
