#!/usr/bin/env python2 # -*- coding: utf-8 -*- # maillage, step espace, step tps # prévoir les options cmd pour générer les champs import hashlib import pickle import bson import json import sys import numpy from PIL import Image from pymongo import MongoClient import pymongo.errors username, password, host, dbname = 'tazio', 'taziotazio', '127.0.0.1', 'CarTata' client = MongoClient('mongodb://%s:%s@%s/%s' % (username, password, host, dbname)) #extract mesh info from json with open("input.json", "r") as input_file: json_file = json.load(input_file) #For psi_zero kx=0.0 ky=70.0 #For img to potential img_max=5 img_min=0 def psi_zero(x, y, tuple_parameters): return numpy.complex128(numpy.exp(-numpy.sqrt(x*x/2 + y*y/2))*numpy.exp(1j*(x*kx +y*ky))) #WARNING ! Black is 0 and white is 255 def img_to_mat(img_file): m=numpy.array(Image.open(img_file)) arr=numpy.absolute( m - m.max() ) return img_min + (arr / arr.max())*img_max def img_to_potential(x, y, (img_mat,)): return numpy.float64(img_mat[x][y]) def null_potential(x, y, tuple_parameters): return numpy.float64(0) def field_generator(mesh, generate_function, generate_function_parameters=None): field = [] for i in range(mesh['width']): x = startX + i * mesh["stitch"]["width"] field.append([]) for j in range(mesh['length']): y = startY + j * mesh["stitch"]["length"] field[i].append(generate_function(x, y, generate_function_parameters)) return numpy.array(field) def potential_field_generator(mesh, generate_function, generate_function_parameters=None): field = [] for i in range(mesh['width']): x = i field.append([]) for j in range(mesh['length']): y = j field[i].append(generate_function(x, y, generate_function_parameters)) return numpy.array(field) def hash_data(content): m = hashlib.md5() m.update(content) return m.hexdigest() mesh = json_file["sizes"] img_name= json_file["img_name"] #mesh = { # 'width': sizes['width'], # 'length': sizes['length'], # 'stitch': { # 'width': sizes['stitch']['width'], # 'length': sizes['stitch']['length'] # } #} startX = json_file["startX"] startY = json_file["startY"] #generate fields with the mesh field_potential = potential_field_generator(mesh, img_to_potential, (img_to_mat(img_name),) ) #field_potential = potential_field_generator(mesh, null_potential, None) field_psi = field_generator(mesh, psi_zero) try: psi_bin = bson.binary.Binary(pickle.dumps(field_psi, protocol=2)) potential_bin = bson.binary.Binary(pickle.dumps(field_potential, protocol=2)) try: current_run_id = client[dbname].data_psi.find_one(sort=[("run_id", -1)])["run_id"] + 1 #-1 is for DESCENDING sort except TypeError: current_run_id = 0 hash_p=hash_data(potential_bin) hash_m=hash_data(psi_bin) client[dbname]['data_psi'].insert_one({'mat':psi_bin, 'mat_bin_hash':hash_m, 'run_id':current_run_id, 'iteration':0}) client[dbname]['data_input'].insert_one({ 'field_potential':potential_bin, 'field_potential_bin_hash':hash_p, 'img_fact':json_file["img_factor"], 'run_id':current_run_id, 'nb_iter':json_file["nb_iter"], 'dtime':json_file["dtime"], 'img_name':img_name, 'mesh':mesh }) client[dbname]['data_psi'].create_index([('run_id',1),('iteration',1)]) client[dbname]['data_input'].create_index([('run_id',1)]) except pymongo.errors.OperationFailure as e: print("ERROR: %s" % (e)) print("Run id : " + str(current_run_id))