# -*- coding: utf-8 -*-

# 2018-06-13 MW using the fabric tensor of spam

import numpy, math
import spam.label.toolkit as ltk

TEXTOUTPUT = True

folder   = "cyclic/results/"

# input
filenames = [
             'triax.-1.eps=0.spheres',
             'triax.0.eps=-0.0025.b.spheres',
             'triax.0.eps=-0.0025.a2.spheres',
             'triax.0.eps=-0.0025.spheres',
             'triax.1.eps=-50145.5.a2.spheres',
             'triax.1.eps=-50145.5.b.spheres',
             'triax.1.eps=-50145.5.a1.spheres',
             'triax.1.eps=-50145.5.spheres',
             'triax.2.eps=-0.0025.a1.spheres',
             'triax.2.eps=-0.0025.b.spheres',
             'triax.2.eps=-0.0025.a2.spheres',
             'triax.2.eps=-0.0025.spheres',
             'triax.3.eps=-0.005.spheres',
             'triax.4.eps=-50145.5.a2.spheres',
             'triax.4.eps=-50145.5.b.spheres',
             'triax.4.eps=-50145.5.a1.spheres',
             'triax.4.eps=-50145.5.spheres',
             'triax.5.eps=-0.005.a1.spheres',
             'triax.5.eps=-0.005.b.spheres',
             'triax.5.eps=-0.005.a2.spheres',
             'triax.5.eps=-0.005.spheres',
             'triax.6.eps=-0.01.spheres',
             'triax.7.eps=-50145.5.a2.spheres',
             'triax.7.eps=-50145.5.b.spheres',
             'triax.7.eps=-50145.5.a1.spheres',
             'triax.7.eps=-50145.5.spheres',
             'triax.8.eps=-0.01.a1.spheres',
             'triax.8.eps=-0.01.b.spheres',
             'triax.8.eps=-0.01.a2.spheres',
             'triax.8.eps=-0.01.spheres',
             'triax.9.eps=-0.02.spheres',
             'triax.10.eps=-50145.5.a2.spheres',
             'triax.10.eps=-50145.5.b.spheres',
             'triax.10.eps=-50145.5.a1.spheres',
             'triax.10.eps=-50145.5.spheres',
             'triax.11.eps=-0.02.a1.spheres',
             'triax.11.eps=-0.02.b.spheres',
             'triax.11.eps=-0.02.a2.spheres',
             'triax.11.eps=-0.02.spheres',
             'triax.12.eps=-0.03.spheres',
             'triax.13.eps=-0.04.spheres',
             'triax.14.eps=-0.05.spheres',
             'triax.15.eps=-50145.5.a2.spheres',
             'triax.15.eps=-50145.5.b.spheres',
             'triax.15.eps=-50145.5.a1.spheres',
             'triax.15.eps=-50145.5.spheres',
             'triax.16.eps=-0.05.a1.spheres',
             'triax.16.eps=-0.05.b.spheres',
             'triax.16.eps=-0.05.a2.spheres',
             'triax.16.eps=-0.05.spheres',
             'triax.17.eps=-0.06.spheres',
             'triax.18.eps=-0.07.spheres',
             'triax.19.eps=-0.08.spheres',
             'triax.20.eps=-0.09.spheres',
             'triax.21.eps=-0.1.spheres',
             'triax.22.eps=-50145.5.a2.spheres',
             'triax.22.eps=-50145.5.b.spheres',
             'triax.22.eps=-50145.5.a1.spheres',
             'triax.22.eps=-50145.5.spheres',
             'triax.23.eps=-0.1.a1.spheres',
             'triax.23.eps=-0.1.b.spheres',
             'triax.23.eps=-0.1.a2.spheres',
             'triax.23.eps=-0.1.spheres',
             'triax.24.eps=-0.11.spheres',
             'triax.25.eps=-0.12.spheres',
             'triax.26.eps=-0.13.spheres',
             'triax.27.eps=-0.14.spheres',
             'triax.28.eps=-0.15.spheres',
             'triax.29.eps=-50145.5.a2.spheres',
             'triax.29.eps=-50145.5.b.spheres',
             'triax.29.eps=-50145.5.a1.spheres',
             'triax.29.eps=-50145.5.spheres',
             'triax.30.eps=-0.15.a1.spheres',
             'triax.30.eps=-0.15.b.spheres',
             'triax.30.eps=-0.15.a2.spheres',
             'triax.30.eps=-0.15.spheres',
             'triax.31.eps=-0.16.spheres',
             'triax.32.eps=-0.17.spheres',
             'triax.33.eps=-0.18.spheres',
             'triax.34.eps=-0.19.spheres',
             'triax.35.eps=-0.2.spheres',
             'triax.36.eps=-50145.5.a2.spheres',
             'triax.36.eps=-50145.5.b.spheres',
             'triax.36.eps=-50145.5.a1.spheres',
             'triax.36.eps=-50145.5.spheres',
                      ]

filenames_output = ["triax_loose_cycles_fabric-tensor_VTK.txt"]

if TEXTOUTPUT:
    f = open(folder+filenames_output[0],'w')
    f.write('#state F[1,1] F[1,2]=F[2,1] F[1,3]=F[3,1] F[2,2] F[2,3]=F[3,2] F[3,3] a\n')
    f.close()

counter = 0

for filename in filenames:
    # loading the dem file
    print "\nLoading ", folder + filename + "_image_gaussian=08_noise=03-local=180_orientations_VTK.txt", "...",
    contacts = numpy.loadtxt( folder + filename+"_image_gaussian=08_noise=03-local=180_orientations_VTK.txt",usecols=(2,3,4) )
    print " done"
    
    N, F, a = ltk.fabricTensor(contacts)
    
    print N
    print ""
    print F
    print ""
    print a
    
    if TEXTOUTPUT:
        f = open(folder+filenames_output[0],'a')
        f.write('%02d %10.8f %10.8f %10.8f %10.8f %10.8f %10.8f %10.8f '%(counter, N[0,0], N[0,1], N[0,2], N[1,1], N[1,2], N[2,2], a))
        f.write('\n')
        f.close() 
        
    counter += 1
