mwe_phi = nfem2.make_mwe("mwe_phi", [(1, element_phi)]) mwe_J = nfem2.make_mwe("mwe_J", [(1, element_J)]) diffop_laplace = nfem2.diffop("-<d/dxj drho_by_dt|sigma|d/dxj phi>, j:2") diffop_J = nfem2.diffop("<J(k)|sigma|d/dxk phi>, k:2") # Initial conductivity is spatially constant: def fun_sigma0(dof_name_indices, position): return sigma0 # Later on, we will modify this field: field_sigma = nfem2.make_field(mwe_sigma, fun_sigma0) # Dirichlet Boundary Conditions on our sample: def laplace_dbc(coords): if (abs(coords[1]) > (2.5 - 0.05)): return 1 else: return 0 def laplace_dbc_values(dof_name_indices, coords): if (coords[1] > 0.0): return 1.0 else:
if "nan" in str(magn): #print ">>"+str(magn)+"<<<<<<<" m[ax + "." + bx[:8] + "," + ay + "." + by[:8]] = [0.0, 0.0, 0.0] else: # print ax+"."+bx[:8]+","+ay+"."+by[:8], ">>"+str(magn)+"<<" m[ax + "." + bx[:8] + "," + ay + "." + by[:8]] = magn # Later on, we will modify this field: field_sigma = None field_m = None ocaml.sys_check_heap() field_sigma = nfem2.make_field(mwe_sigma, fun_sigma0) field_m = nfem2.make_field(mwe_M, fun_fill_m) # Note the ignore_jumps parameter: # we indeed ARE interested just in the # surface outflow of electrical current! for i in range(1, 5): # was: range(1,5) update_sigma(field_sigma, i) print "Forcing ocaml GC!" sys.stdout.flush() ocaml.sys_check_heap() #print "Iteration ",i," sigma at origin: ",nfem2.probe_field(field_sigma,[0.0,0.0]) total_current = compute_total_current(field_sigma)
def constant_current(voltage): # Dirichlet Boundary Conditions on our sample: def laplace_dbc(coords): # top contact bottom contact if (coords[1] > 990.0 and coords[0] > 420.0) or (coords[1] < -40.0): return 1 else: return 0 def laplace_dbc_values(dof_name_indices, coords): if (coords[1] > 500.0 ): # greater or smaller than 1/2height of the sample return voltage else: return 0.0 prematrix_laplace = nfem2.prematrix(diffop_laplace, mwe_drho_by_dt, mwe_phi, mwe_mid=mwe_sigma) prematrix_J = nfem2.prematrix(diffop_J, mwe_J, mwe_phi, mwe_mid=mwe_sigma) code_recompute_conductivity = """ if(have_sigma_Py) { double len2_J, sprod_MJ, cos2; double len2_M=M(0)*M(0)+M(1)*M(1)+M(2)*M(2); len2_J=J(0)*J(0)+J(1)*J(1)+J(2)*J(2); sprod_MJ=M(0)*J(0)+M(1)*J(1)+M(2)*J(2); cos2=(fabs(len2_J)<1e-8)?0.0:(sprod_MJ*sprod_MJ/(len2_M*len2_J)); sigma_Py = sigma0_Py /( 1. + alpha * cos2); } /* printf(\"cos2=%f sigma = %8.6f\\n \",cos2, sigma); */ """ recompute_conductivity = nfem2.site_wise_applicator( parameter_names=["sigma0_Py", "sigma0_Au", "alpha"], # all the names of extra parameters code=code_recompute_conductivity, field_mwes=[mwe_sigma, mwe_J, mwe_M]) # computing the total current through the sample: code_integrate_div_J = """ if(coords(1)>990.0 && coords(0)>420.0) { total_current_up+=drho_by_dt; } else if(coords(1)<-40.0) { total_current_down+=drho_by_dt; } else { double j=drho_by_dt; if(j>0)total_bad_current_plus+=j; else total_bad_current_minus+=j; } """ accumulate_J_total = nfem2.site_wise_applicator( parameter_names=[ "total_current_up", "total_current_down", "total_bad_current_plus", "total_bad_current_minus" ], code=code_integrate_div_J, position_name="coords", cofield_mwes=[mwe_drho_by_dt]) def update_sigma(the_field_sigma, i): compute_J = nfem2.prematrix_applicator( prematrix_J, field_mid=the_field_sigma) laplace_solver = nfem2.laplace_solver( prematrix_laplace, dirichlet_bcs=[(-1, 4, laplace_dbc), (-1, 6, laplace_dbc)], mwe_mid=the_field_sigma) cofield_drho_by_dt = nfem2.make_cofield(mwe_drho_by_dt) # field_phi = laplace_solver(cofield_drho_by_dt, dbc_values=laplace_dbc_values) field_J = nfem2.cofield_to_field(compute_J(field_phi)) # Next, let us compute the new condictivity by site-wise operation on # field_J. We just overwrite field_sigma: recompute_conductivity( [sigma0_Py, sigma0_Au, alpha], fields=[the_field_sigma, field_J, field_m]) return the_field_sigma def compute_total_current(the_field_sigma): laplace_solver = nfem2.laplace_solver( prematrix_laplace, # boundary conditions are defined on objects 4 and 6 dirichlet_bcs=[(-1, 4, laplace_dbc), (-1, 6, laplace_dbc)], mwe_mid=the_field_sigma) compute_div_J = nfem2.prematrix_applicator( prematrix_laplace, field_mid=the_field_sigma) cofield_drho_by_dt = nfem2.make_cofield(mwe_drho_by_dt) field_phi = laplace_solver(cofield_drho_by_dt, dbc_values=laplace_dbc_values) compute_J = nfem2.prematrix_applicator( prematrix_J, field_mid=the_field_sigma) field_J = nfem2.cofield_to_field(compute_J(field_phi)) cofield_div_J = compute_div_J(field_phi) field_div_J = nfem2.cofield_to_field(cofield_div_J) current = accumulate_J_total([0.0, 0.0, 0.0, 0.0], cofields=[cofield_div_J]) return current, field_phi, the_field_sigma, field_J global field_m global field_sigma # Later on, we will modify this fields: field_sigma = None field_m = None ocaml.sys_check_heap() field_sigma = nfem2.make_field(mwe_sigma, fun_sigma0) ## print "#######################################" ## print "## FIRST BATCH SIGMA ##" ## print "#######################################" ## nfem2.field_print_contents(field_sigma) field_m = nfem2.make_field(mwe_M, fun_fill_m) ## print "#######################################" ## print "## FIRST BATCH M ##" ## print "#######################################" ## nfem2.field_print_contents(field_m) # update sigma for i in range(1, 3): update_sigma(field_sigma, i) print "Forcing ocaml GC!" sys.stdout.flush() ## nfem2.field_print_contents(field_sigma) ocaml.sys_check_heap() total_current, field_phi, field_sigma, field_J = compute_total_current( field_sigma) nfem2_visual.fields2vtkfile(field_sigma, "sigma" + str(fi) + ".vtk") nfem2_visual.fields2vtkfile(field_phi, "phi" + str(fi) + ".vtk") nfem2_visual.fields2vtkfile( field_J, "current-density" + str(fi) + ".vtk") voltage_contact7 = nfem2.probe_field( field_phi, [-20.0, 630.0, 40.0])[0][1] voltage_contact8 = nfem2.probe_field( field_phi, [-20.0, 385.0, 40.0])[0][1] print total_current resistance = voltage / total_current[ 1] # resistance: potential / (downward current) print "Current: ", total_current print "Resistance: ", resistance # ideal voltage is 150 in our units (to respect 150 microA in the experiment) string2file = "voltage-contacts_11-9: %f\t total current: %f\t contact7: %f\t contact8: %f\n" % ( voltage, total_current[1], voltage_contact7, voltage_contact8) amrDataFile.write( str(fi) + " " + str(resistance) + " " + str(total_current) + "\n") amrDataFile.write(string2file) amrDataFile.flush() return total_current[1] - 150.
ay, by = y.split(".") if "nan" in str(magn): # print ">>"+str(magn)+"<<<<<<<" m[ax + "." + bx[:8] + "," + ay + "." + by[:8]] = [0.0, 0.0, 0.0] else: # print ax+"."+bx[:8]+","+ay+"."+by[:8], ">>"+str(magn)+"<<" m[ax + "." + bx[:8] + "," + ay + "." + by[:8]] = magn # Later on, we will modify this field: field_sigma = None field_m = None ocaml.sys_check_heap() field_sigma = nfem2.make_field(mwe_sigma, fun_sigma0) field_m = nfem2.make_field(mwe_M, fun_fill_m) # Note the ignore_jumps parameter: # we indeed ARE interested just in the # surface outflow of electrical current! for i in range(1, 5): # was: range(1,5) update_sigma(field_sigma, i) print "Forcing ocaml GC!" sys.stdout.flush() ocaml.sys_check_heap() # print "Iteration ",i," sigma at origin: ",nfem2.probe_field(field_sigma,[0.0,0.0]) total_current = compute_total_current(field_sigma)
def constant_current( voltage ): # Dirichlet Boundary Conditions on our sample: def laplace_dbc(coords): # top contact bottom contact if (coords[1] > 990.0 and coords[0] > 420.0) or (coords[1] < -40.0) : return 1 else: return 0 def laplace_dbc_values(dof_name_indices,coords): if(coords[1] > 500.0): # greater or smaller than 1/2height of the sample return voltage else: return 0.0 prematrix_laplace=nfem2.prematrix(diffop_laplace, mwe_drho_by_dt,mwe_phi, mwe_mid=mwe_sigma) prematrix_J=nfem2.prematrix(diffop_J, mwe_J,mwe_phi, mwe_mid=mwe_sigma) code_recompute_conductivity=""" if(have_sigma_Py) { double len2_J, sprod_MJ, cos2; double len2_M=M(0)*M(0)+M(1)*M(1)+M(2)*M(2); len2_J=J(0)*J(0)+J(1)*J(1)+J(2)*J(2); sprod_MJ=M(0)*J(0)+M(1)*J(1)+M(2)*J(2); cos2=(fabs(len2_J)<1e-8)?0.0:(sprod_MJ*sprod_MJ/(len2_M*len2_J)); sigma_Py = sigma0_Py /( 1. + alpha * cos2); } /* printf(\"cos2=%f sigma = %8.6f\\n \",cos2, sigma); */ """ recompute_conductivity=nfem2.site_wise_applicator(parameter_names=["sigma0_Py","sigma0_Au","alpha"], # all the names of extra parameters code=code_recompute_conductivity, field_mwes=[mwe_sigma,mwe_J,mwe_M] ) # computing the total current through the sample: code_integrate_div_J=""" if(coords(1)>990.0 && coords(0)>420.0) { total_current_up+=drho_by_dt; } else if(coords(1)<-40.0) { total_current_down+=drho_by_dt; } else { double j=drho_by_dt; if(j>0)total_bad_current_plus+=j; else total_bad_current_minus+=j; } """ accumulate_J_total=nfem2.site_wise_applicator(parameter_names=["total_current_up", "total_current_down", "total_bad_current_plus", "total_bad_current_minus" ], code=code_integrate_div_J, position_name="coords", cofield_mwes=[mwe_drho_by_dt]) def update_sigma(the_field_sigma,i): compute_J=nfem2.prematrix_applicator(prematrix_J, field_mid=the_field_sigma) laplace_solver=nfem2.laplace_solver(prematrix_laplace, dirichlet_bcs=[(-1,4,laplace_dbc),(-1,6,laplace_dbc)], mwe_mid=the_field_sigma) cofield_drho_by_dt=nfem2.make_cofield(mwe_drho_by_dt) # field_phi = laplace_solver(cofield_drho_by_dt, dbc_values=laplace_dbc_values) field_J = nfem2.cofield_to_field(compute_J(field_phi)) # Next, let us compute the new condictivity by site-wise operation on # field_J. We just overwrite field_sigma: recompute_conductivity([sigma0_Py,sigma0_Au,alpha],fields=[the_field_sigma,field_J,field_m]) return the_field_sigma def compute_total_current(the_field_sigma): laplace_solver=nfem2.laplace_solver(prematrix_laplace, # boundary conditions are defined on objects 4 and 6 dirichlet_bcs=[(-1,4,laplace_dbc),(-1,6,laplace_dbc)], mwe_mid=the_field_sigma) compute_div_J=nfem2.prematrix_applicator(prematrix_laplace, field_mid=the_field_sigma) cofield_drho_by_dt=nfem2.make_cofield(mwe_drho_by_dt) field_phi=laplace_solver(cofield_drho_by_dt, dbc_values=laplace_dbc_values) compute_J=nfem2.prematrix_applicator(prematrix_J, field_mid=the_field_sigma) field_J = nfem2.cofield_to_field(compute_J(field_phi)) cofield_div_J=compute_div_J(field_phi) field_div_J = nfem2.cofield_to_field(cofield_div_J) current = accumulate_J_total([0.0,0.0,0.0,0.0],cofields=[cofield_div_J]) return current, field_phi, the_field_sigma, field_J global field_m global field_sigma # Later on, we will modify this fields: field_sigma = None field_m = None ocaml.sys_check_heap() field_sigma=nfem2.make_field(mwe_sigma,fun_sigma0) ## print "#######################################" ## print "## FIRST BATCH SIGMA ##" ## print "#######################################" ## nfem2.field_print_contents(field_sigma) field_m=nfem2.make_field(mwe_M, fun_fill_m) ## print "#######################################" ## print "## FIRST BATCH M ##" ## print "#######################################" ## nfem2.field_print_contents(field_m) # update sigma for i in range(1,3): update_sigma(field_sigma,i) print "Forcing ocaml GC!" sys.stdout.flush() ## nfem2.field_print_contents(field_sigma) ocaml.sys_check_heap() total_current, field_phi, field_sigma, field_J = compute_total_current(field_sigma) nfem2_visual.fields2vtkfile( field_sigma, "sigma"+str(fi)+".vtk") nfem2_visual.fields2vtkfile( field_phi, "phi"+str(fi)+".vtk") nfem2_visual.fields2vtkfile( field_J, "current-density"+str(fi)+".vtk") voltage_contact7 = nfem2.probe_field(field_phi,[-20.0,630.0, 40.0])[0][1] voltage_contact8 = nfem2.probe_field(field_phi,[-20.0,385.0, 40.0])[0][1] print total_current resistance = voltage/total_current[1] # resistance: potential / (downward current) print "Current: ",total_current print "Resistance: ",resistance # ideal voltage is 150 in our units (to respect 150 microA in the experiment) string2file = "voltage-contacts_11-9: %f\t total current: %f\t contact7: %f\t contact8: %f\n" % (voltage, total_current[1], voltage_contact7, voltage_contact8) amrDataFile.write(str(fi)+" "+str(resistance)+" "+str(total_current)+"\n") amrDataFile.write(string2file) amrDataFile.flush() return total_current[1]-150.
mwe_sigma = nfem2.make_mwe("mwe_sigma", [(1,element_sigma)]) mwe_drho_by_dt = nfem2.make_mwe("mwe_drho_by_dt",[(1,element_drho_by_dt)]) mwe_phi = nfem2.make_mwe("mwe_phi", [(1,element_phi)]) mwe_J = nfem2.make_mwe("mwe_J", [(1,element_J)]) diffop_laplace=nfem2.diffop("-<d/dxj drho_by_dt|sigma|d/dxj phi>, j:2") diffop_J=nfem2.diffop("<J(k)|sigma|d/dxk phi>, k:2") # Initial conductivity is spatially constant: def fun_sigma0(dof_name_indices,position): return sigma0 # Later on, we will modify this field: field_sigma=nfem2.make_field(mwe_sigma,fun_sigma0) # Dirichlet Boundary Conditions on our sample: def laplace_dbc(coords): if(abs(coords[1]) > (2.5-0.05)): return 1 else: return 0 def laplace_dbc_values(dof_name_indices,coords): if(coords[1] > 0.0): return 1.0 else: return -1.0