ABAQUS script for a custom field output - python

I am new to ABAQUS scripting and I am trying to calculate micromotion using COPEN, CSLIP1 and CSLIP2. I came up with the code below:
from abaqusConstants import *
from odbAccess import *
from odbMaterial import *
from odbSection import *
from math import *
from copy import deepcopy
from caeModules import *
from driverUtils import executeOnCaeStartup
from numpy import fabs as fabs
import numpy as np
from types import IntType
odb = session.openOdb(name='E:\PDP02.odb', readOnly=FALSE)
odb = session.odbs['E:\PDP02.odb']
print odb.rootAssembly.instances.keys()
grout_instance = odb.rootAssembly.instances['PROX-1#PROXIMAL-1']
keys = odb.steps.keys()
for key in keys:
step = odb.steps[key]
for frame in step.frames:
print frame.description
Copen = frame.fieldOutputs['COPEN']
Cslip1 = frame.fieldOutputs['CSLIP1']
Cslip2 = frame.fieldOutputs['CSLIP2']
Micromotion = sqrt(power(Copen,2)+power(Cslip1,2)+power(Cslip2,2))
#Micromotion =sqrt(power(Cslip2,2))
#float(Micromotion)
frame.FieldOutput(name='Micromotion', description='Average Micromotion', field=Micromotion)
odb.update()
odb.save()
After executing the code, i get the following error message: "OdiError: Expression evaluates to an overflow or underflow". Please help me understand this error message and how to rectify it. I am happy to provide the .inp and .odb files for reference and verification.

Simply put, overflow and underflow happen when we assign a value that is out of range of the declared data type of the variable. If the (absolute) value is too big, we call it overflow, if the value is too small, we call it underflow.

Related

Print scientific variable on txt file using f.write()

Since a couple of hours, I am trying to print a simple time vector in a txt file using Python.
import numpy as np
Tp = 2000 * 10**(-9)
dt = Tp / (90000)
t = np.linspace(0,Tp,dt)
timing = open("time.txt","w")
for ii in range(len(t)) :
timing.write(str(t[ii]))
timing.write("\n")
timing.close()
But I still get an empty file and I don't understand at all why.
Maybe I have to be more specific in the function with the precision I want.
Since I have a lot of small numbers (4e-10 ..) to process I would like to understand a general method to write variable (not the entire vector at once) on a txt file with a exponential notation (In Matlab it's kind of automatic I think).
Thx
You have an error using linspace. Please check https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
Try this:
import numpy as np
Tp = 2000 * 10**(-9)
# dt = Tp / 90000.0
dt = 90000
t = np.linspace(0,Tp,dt)
timing = open("time.txt","w")
for ii in range(len(t)) :
timing.write(str(t[ii]))
timing.write("\n")
timing.close()

How to fix the error "can't convert expression to float"

Its a simple line of code but still I can't figure out why its giving such an error "can't convert expression to float"
from __future__ import division
from pylab import *
from sympy import *
r = symbols('r')
P = 1023732.58489791*cos(r) + 355250.305250305*cos(2*r)
P1 = solve(P,r)
print P1
Errors Image is attached here:
Error message snapshot

ExcelPython return type mismatch issue

so I'm calling a python script from my Excel file with VBA and the ExcelPython reference. The python script is working fine except for Excel keeps telling me I have a type mismatch on the noted line:
Function calcCapValue(times As Range, fRates As Range, strike As Range, vol As Range, delta As Double, pv As Range) As Variant
Set methods = PyModule("PyFunctions", AddPath:=ThisWorkbook.Path)
Set result = PyCall(methods, "CalculateCapValue", KwArgs:=PyDict("times", times.Value2, "fwdRates", fRates.Value2, "strike", strike.Cells(1, 1).Value2, "flatVol", vol.Cells(1, 1).Value2, "delta", delta, "pv", pv.Cells(1, 1).Value2))
calcCapValue = PyVar(PyGetItem(result, "res")) ' <--- TYPE MISMATCH HERE
Exit Function
End Function
Can't figure out why, I'm following the example code from here: https://sourceforge.net/p/excelpython/wiki/Putting%20it%20all%20together/
and here: http://www.codeproject.com/Articles/639887/Calling-Python-code-from-Excel-with-ExcelPython
Still getting this type mismatch and I can't figure out why.
Here's the python script:
#imports
import numpy as np
from scipy.stats import norm
#
# Cap Price Calculation
#
def CalculateCapValue(times, fwdRates, strike, flatVol, delta, pv):
capPrice = 0;
#Iterate through each time for caplet price
for i in range(0, len(times)):
ifr = float(fwdRates[i][0])
iti = float(times[i][0])
d1 = (np.log(ifr/strike)+((flatVol**2)*iti)/2)/(flatVol*np.sqrt(iti))
d2 = d1 - (flatVol*np.sqrt(iti))
Nd1 = norm.cdf(d1)
Nd2 = norm.cdf(d2)
capPrice += pv*delta*(ifr*Nd1 - strike*Nd2)
return {"res": capPrice}
Thanks!
Question answered on ExcelPython discussion forums on Sourceforge:
http://sourceforge.net/p/excelpython/discussion/general/thread/97f6c1b0/

numpy measure time - syntax error

I want to measure the time through numpy but I am not sure if I have the right arguments.
import numpy as np
import timeit
def svdsolve(a,b):
u,s,v = np.linalg.svd(a)
c = np.dot(u.T,b)
w = np.linalg.solve(np.diag(s),c)
x = np.dot(v.T,w)
return x
A_=np.fromfile('dataA.bin',count=-1,dtype=np.float32)
B_=np.fromfile('dataB.bin',count=-1,dtype=np.float32)
s='svdsolve({0},{1})'.format(A,B)
mytime= timeit.Timer(stmt=s,setup='import numpy').timeit(100)
print mytime
Right now it gives me :
File "/usr/lib64/python2.7/timeit.py", line 136, in init
code = compile(src, dummy_src_name, "exec") File "", line 6
svdsolve([[ 1.86248358e+09 1.54404045e+09]
^ SyntaxError: invalid syntax
Also , I didn' understand (neither have I found a reference) on what arguments to pass at timer.
And I am not sure how to use the repetition timeit(100)
Neither the str nor repr of a NumPy array is guaranteed to produce an output that can be used to reconstruct the original array. (format uses str, but repr wouldn't help.) Instead, import the arrays into the timed code's namespace. Assuming this code is being run as a script, that would be
mytime = timeit.Timer(stmt='svdsolve(A, B)',
setup='from __main__ import A, B, svdsolve'
).timeit(100)

Cosine scoring in whoosh, python

I've implemented some python code to run search on whoosh using BM25 and all went out ok, but now that I'm trying to change scoring mechanism to Cosine, I'm getting this error:
File "TFIDF.py", line 18, in tfidf
with ix.searcher(weighting=whoosh.scoring.Cosine()) as searcher: AttributeError: 'module' object has no attribute 'Cosine'
If I import Cosine
from whoosh.scoring import Cosine
I get this:
File "TFIDF.py", line 4, in <module>
from whoosh.scoring import Cosine
ImportError: cannot import name Cosine
My code is below:
import whoosh
from whoosh.scoring import Cosine
from whoosh.fields import *
from whoosh.scoring import *
from whoosh.qparser import *
from whoosh.query import *
from whoosh.index import open_dir
#Index path
lab3dir= "../lab3/Aula3_1/"
ix = open_dir(lab3dir + "indexdir") #Index generated in previous lab
def cosine(queryTerms):
list=[]
dict={} # dict com ID do doc e Similiaridade
with ix.searcher(weighting=whoosh.scoring.Cosine()) as searcher:
query = QueryParser("content", ix.schema, group=OrGroup).parse(u(queryTerms))
results = searcher.search(query, limit=100)
for i,r in enumerate(results):
list.append(r["id"])
print r, results.score(i)
dict[r["id"]]= results.score(i)
return dict
Any ideias???
Thank you!
The documentation on http://pythonhosted.org/Whoosh/searching.html#the-searcher-object, which I assume is what you're looking at is incorrect as you have found. Look at the documentation (http://pythonhosted.org/Whoosh/api/scoring.html#module-whoosh.scoring) or the source code (https://bitbucket.org/mchaput/whoosh/src/362fc2999c8cabc51370f433de7402fafd536ec6/src/whoosh/scoring.py?at=default) for the options actually available.

Categories