/**
MONOPOLE LOOPS IN QED:
1. run a qed lattice model,
2. then find the monopole number of the dual lattice:
at each site the net flow of four 3-dimensional cubes is calculated (adjacent plaquettes of the cube are subtracted from each other);
[testsum must be 0, then the link variables are in right order]
data = one of [-2,-1,0,1,2]
3. store original qed lattice and monopole loops

KV, CBL, 02/2010, in preparation for SBEII
**/

	// read stored data: 
	q = q ?? ();
	(
////	var pfade = "~/Desktop/SBE2_Data/MOL_configs/MOL_mono_at_*.zar".pathMatch;
//	var pfade = Document.current.dir ++ "/MOL/data_10x10/monopoles_*.dat".pathMatch;
//	var archives = pfade.collect { |path| ZArchive.read(path).readItem };
	q.paths = pathMatch("data_10x10/*".resolveRelative);
	q.allRawData = q.paths.collect { |path| 
		FileReader.read(path, true, true, { |x| x.asInteger })
	};
	q.allRawData.shape.postln;
	// special operation!!
	q.allData = q.allRawData.collect { |x|
			//x.flat.clump(4).clump(1000).collect { |x| x.clump(100).collect { |x| x.clump(10) } }
			x.flat.clump(4).clump(10).clump(10).clump(10);
	};
	q.mono = q.allData.at(0); // many many loops
	q.mono = q.allData.at(30); // very few, small loops

	q.data0 = q.allData.at(0);
	q.data095 = q.allData.at(19);
	q.data105 = q.allData.at(21);
	q.data15 = q.allData.at(30);
	)
	/* test data:
		// shape stimmt
		q.mono095.shape
		q.mono095.flat.abs.sum;
		q.mono105.flat.abs.sum;
	*/

		// versuch die verteilung statistisch zu hoeren
(
SynthDef(\ting, { |out, freq = 400, amp = 0.1, pan, sustain = 0.01| 
	OffsetOut.ar(out, 
		Pan2.ar(
			SinOsc.ar(freq) 
			* EnvGen.ar(Env.perc(sustain * 0.1, amp, sustain * 0.9), doneAction: 2), 
			amp
		)
	)
}).store;
)

(instrument: \ting, freq: 500, sustain: 0.01).play;

	// zufaellige einzelpunkte spielen
(
Tdef(\data, { 
	1000.do { 
		var val = q.data105.choose.choose.choose.choose.sum;
		var freq = val.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.01).play;
		0.02.rand.wait;
	}
}).play;
)

	// zufaellige einzelpunkte aus den 4 datensaetzen
(
Tdef(\data, { 
	
	[q.data095, q.data097, q.data102, q.data105].do { |data| 
		q.data = data;
		0.5.wait;
		800.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			var freq = val.linexp(0, 4, 100, 10000);
			(instrument: \ting, freq: freq, sustain: 0.01).play;
			0.01.rand.wait;
		};
	}
}).play;
)

	// random mittelwerte vergleichen
(
Tdef(\data, { 
		var n = 300;
		var freq; 
		q.data = q.data095;
		0.5.wait;
		q.meanval = 0; 
		n.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			var freq = val.linexp(0, 4, 100, 10000);
			q.meanval = q.meanval + val;
			(instrument: \ting, freq: freq, sustain: 0.01).play;
			0.02.rand.wait;
		};
		freq = q.meanval / n;
		(instrument: \ting, freq: freq, sustain: 0.1).play;
}).play;
)

	// mehrere pro datensatz mittelwerte vergleichen
(
Tdef(\data, { 
		var n = 800, freq;
		q.data = q.data102;
		q.meanval = 0;
		10.do { 
		n.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			q.meanval = q.meanval + val;
		};
		q.meanval = q.meanval / n;
		freq = q.meanval.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
}).play;
)

	// mehrere random mittelwerte vergleichen, 
	// ab wievielen ist der mittelwert aussagekraeftig?
(
Tdef(\data, { 
		var n = 800, freq;
		q.data = q.data06;
		q.meanval = 0;
[q.data06, q.data095, q.data097, q.data102, q.data105, q.data14 ].do { |data| 
		q.data = data;
		0.5.wait;		
		5.do { 
		n.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			q.meanval = q.meanval + val;
		};
		q.meanval = q.meanval / n;
		freq = q.meanval.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
	}
}).play;
)

/// 5 configs vom gleichem e-wert vergleichen:
/// streuung ueberraschend gross!

	(
	var pfade = "~/Desktop/SBE2_Data/MOL095/MOL_data_at_*.zar".pathMatch;
	q.mol095 = pfade.collect { |path| path.basename.postln; ZArchive.read(path).readItem; };
	)

(
Tdef(\data, { 
	var n = 800, freq;

	q.meanval = 0;
	q.mol095.do { |data| 
		q.data = data;
		0.5.wait;		
		5.do { 
		n.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			q.meanval = q.meanval + val;
		};
		q.meanval = q.meanval / n;
		freq = q.meanval.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
	}
}).play;
)

////////////////////////////////
///// monopole-daten mit gleicher strategie

	(
	var pfade = "~/Desktop/SBE2_Data/MOLNEWDATA/MOL_mono_at_*.zar".pathMatch;
	q.monos = pfade.collect { |path| var key = path.basename.drop(12).drop(-4).asSymbol.postcs; 
		ZArchive.read(path).readItem
	}
	)

(
Tdef(\data, { 
	var n = 800, freq;
	
	q.meanval = 0;
	q.monos.do { |data| 
		q.data = data;
		0.5.wait;		
		5.do { 
		n.do { 
			var val = q.data.choose.choose.choose.choose.choose.abs;
			q.meanval = q.meanval + val;
		};
		q.meanval = (q.meanval / n).postcs;
		freq = q.meanval.linexp(0, 2, 100, 100000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
	}
}).play;
)

	// mittelwert von allem
(
Tdef(\data, { 
	var freq;
	
	q.meanval = 0;
	q.monos.do { |data| 
		q.data = data;
		0.5.wait;		
		q.meanval = q.data.flat.abs.mean.postcs;
		freq = q.meanval.linexp(0, 2, 100, 100000);
	//	(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
}).play;
)

/// 5 configs vom gleichem e-wert vergleichen:

	(
	var pfade = "~/Desktop/SBE2_Data/MOL06/MOL_data_at_*.zar".pathMatch;
	q.mol06 = pfade.collect { |path| path.basename.postln; ZArchive.read(path).readItem; };
	)
	(
	var pfade = "~/Desktop/SBE2_Data/MOL14/MOL_data_at_*.zar".pathMatch;
	q.mol14 = pfade.collect { |path| path.basename.postln; ZArchive.read(path).readItem; };
	)

(
Tdef(\data, { 
	var n = 800, freq;

	q.meanval = 0;
	q.mol14.do { |data| 
		q.data = data;
		0.5.wait;		
		1.do { 
		n.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			q.meanval = q.meanval + val;
		};
		q.meanval = q.meanval / n;
		freq = q.meanval.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
	}
}).play;
)

///////////////////// LatticeQEDs neu generieren, 
	immer naechste konfiguration hoeren - wie gross drift,
	wie aehnlich sind mittelwerte ueber verschiedene temperaturen?

q.beta = 1.4;
q.a = LatticeQED.new(10,q.beta,0.5,10); // needs a compiled SC version with qed model!!!
q.a.sweeps(1);
q.a.data;

(
q.beta = 2;
q.b = LatticeQED.new(10, q.beta, 0.5, 100); // needs a compiled SC version with qed model!!!
q.b.sweeps(1);
q.b.data;
q.num = 800;
q.numSweeps = 1;
)

(
Tdef(\data, { 
	
	q.meanval = 0;
	
	1000.do { 
		var freq;
		q.b.sweeps(q.numSweeps);
		q.data = q.b.data;
		// 0.1.wait;		
		q.num.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			q.meanval = q.meanval + val;
		};
		q.meanval = q.meanval / q.num;
		freq = q.meanval.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.08.wait;
	}
}).play;
)

(
Tdef(\data, { 
		var n = 800, freq;
		q.data = q.data102;
		q.meanval = 0;
		10.do { 
		n.do { 
			var val = q.data.choose.choose.choose.choose.sum;
			q.meanval = q.meanval + val;
		};
		q.meanval = q.meanval / n;
		freq = q.meanval.linexp(0, 4, 100, 10000);
		(instrument: \ting, freq: freq, sustain: 0.1).play;
		0.1.wait;
		}
}).play;
)

	// die ganze konfig abwandern, oder subwuerfel ...
	// koennte man auch noch raeumlich machen ...
	//