Python Scripting for Abaqus - python

I am new to Abaqus Python Scripting. The following line of code in my script is throwing a keyword error (Type Error: Keyword error on mergeWire).
myPart.WirePolyLine(points = myPoints, mergeWire = OFF, meshable = ON)
where
myPoints = ((datum_points[crossPart_datums_keys[0]],datum_points[crossPart_datums_keys[1]]),datum_points[crossPart_datums_keys[2]],datum_points[crossPart_datums_keys[3](datum_points[crossPart_datums_keys[4]],datum_points[crossPart_datums_keys[5]]),(datum_points[crossPart_datums_keys[6]],datum_points[crossPart_datums_keys[7]]),(datum_points[crossPart_datums_keys[8]],datum_points[crossPart_datums_keys[9]]),(datum_points[crossPart_datums_keys[10]],datum_points[crossPart_datums_keys[11]]),(datum_points[crossPart_datums_keys[12]],datum_points[crossPart_datums_keys[13]]),(datum_points[crossPart_datums_keys[14]],datum_points[crossPart_datums_keys[15]]))
Can someone help me to fix this?

Looking at Abaqus 6.14 Scripting Reference Guide, WirePolyLine has two arguments, points and mergeType. mergeWire and meshable are not listed as arguments. Perhaps you should use:
from abaqusConstants import SEPARATE
myPart.WirePolyLine(points = myPoints, mergeType = SEPARATE)
I strongly suggest you consult section 37.2.56 of Abaqus 6.14 Scripting Reference Guide.

dear friend, I am also a beginner in abacus, but I think the contents of this link can help you in this matter, the good thing is that it is free.
Caeassistant

Related

Getting name of element type doesn't work in Iron python for Revit

I am working in Iron python in pyRevit environment and my code is as follows:
element_types = \
DB.FilteredElementCollector(doc)\
.OfCategory(DB.BuiltInCategory.OST_Walls)\
.WhereElementIsElementType()\ # getting family types not elements
.ToElements()
for ele in element_types:
print(ele.Name)
As per Revit API documentation this should work and probably works in C#. There ele.Name works both as setter and getter.
But in Ironpython above code fails, returning an AttributeError: Name. But when i try ele.Name = "new_family_type_name" it works fine.
So my question is how to make ele.Name work to get the family type name.
This is normally one of the earliest quirks that you come across with RPS - but not to worry, its an easy fix. Try:
for ele in element_Types:
print Element.Name.__get__(ele)
Please try this code
from rpw import db
collector = db.Collector(of_class='WallType')
element_types = collector.get_elements()
for ele in element_types:
print(ele.name)

How do I tell which module a function comes from?

I'm trying to read the help on what various things do as I'm reading through code. I'm getting a bit lost in how to determine which module a function comes from. Here is my current example:
import quandl
import numpy as np
import matplotlib.pyplot as plt
amzn = quandl.get("WIKI/AMZN", start_date="2018-01-01", end_date="2019-01-01")
amzn_daily_close = amzn[['Adj. Close']]
amzn_daily_log_returns = np.log(amzn_daily_close.pct_change()+1)
monthly = amzn.resample('BM').apply(lambda x: x[-1])
So given this block of code, I can do help (quandl.get) to see information about that and help (np.log) to see what that does. But when I get to amzn.resample, where is that resample coming from? What should I be entering to see some help information on the resample stuff?
Look at the docstring of quandl.get method to get the help message about the return object. This will contain a statement as returns x-object. Googling about x-object will give you more info on this.
Alternatively, you can do this. To identify what is the object you can do the below.
amzn_type = type(amzn)
This gives the monthly object type. Googling for this type value will give you more insights about that object.Example -
a = 10
print(type(a))
The above code returns <class 'int'> output. Googling about int class in python3 will be helpful.
Inspection
You can 'inspect' the method to find the implementation:
import inspect
print(inspect.getfile(amzn.resample))
# /opt/miniconda/envs/stackoverflow/lib/python3.6/site-packages/pandas/core/generic.py
IDE
Or you can use a good IDE (e.g. PyCharm or IntelliJ) which supports you with some neat features:
Generally, these modules should be documented somewhere. They are usually "packaged" and made available on Python Package Index (pypi). You can search there for your package name and find the quandl page. That may have a link to the projects home page with more documentation.

Call Python code from an existing project written in Swift

I need a way to call Python code from Swift on an Apple platform. A library would be ideal. I've done a considerable amount of Google searching, and the closest material I found is for Objective-C.
In swift 5 you can try PythonKit framework.
Here's example of the usage:
import PythonKit
let sys = try Python.import("sys")
print("Python \(sys.version_info.major).\(sys.version_info.minor)")
print("Python Version: \(sys.version)")
print("Python Encoding: \(sys.getdefaultencoding().upper())")
I found this excellent and up to date gist that walks you through a complete solution: https://github.com/ndevenish/Site-ndevenish/blob/master/_posts/2017-04-11-using-python-with-swift-3.markdown
If you can get away with just using NSTask to launch a Python process, that's a pretty good option too.
In Swift 4.2 there was an approved feature to allow dynamic languages to be ported directly into swift
https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md
Will look similar to:
// import pickle
let pickle = Python.import("pickle")
// file = open(filename)
let file = Python.open(filename)
// blob = file.read()
let blob = file.read()
// result = pickle.loads(blob)
let result = pickle.loads(blob)
If anyone is ever interested in calling python from swift, here is some helpful material I found:
U the python framework - https://developer.apple.com/library/ios/technotes/tn2328/_index.html
PyObjC (a little more challenging) -
Cobbal - https://github.com/cobbal/python-for-iphone
Python docs (you would need to make C-Swift bridge)
Most of it is for Objective-c, but if you need to use swift you can easily just create an ObjC-Swift bridge (super-super easy) - Lookup the apple docs

Accessing Math functions from rootpy

I'm currently trying to access Math functions such as DeltaR from rootpy, but I'm not sure how this is done. I've seen nothing in the documentation or in any examples. The C++ equivalent would be something like:
double dR = ROOT::Math::VectorUtil::DeltaR((jets)[i],(partons)[i]);
But I'm unable to find a rootpy or even pyroot equivalent that'll work. If I try in pyroot with
import ROOT as r
r.Math.VectorUtil.DeltaR(jets[i],partons[i])
I get the error:
AttributeError: type object 'ROOT::Math' has no attribute 'VectorUtil'
When it quite clearly should, unless I don't understand correctly what it means by 'Attribute'. Anyway, I don't want to ask pyroot questions here :) I just put this down to a quirk in the way that pyroot handles such things, which is why I thought I'd give rootpy a try. I'm not sure if this is possible however.
Cheers,
Joseph
The functions from ROOT::Math::VectorUtil are in libGenVector which is loaded automatically in neither CINT nor PyROOT. Manually loading it (like you probably do in your root_logon.C) makes the functions available, e.g.
import ROOT as r
r.gSystem.Load('libGenVector')
# ...
r.Math.VectorUtil.DeltaR(jets[i],partons[i])
If jets and partons are TLorentzVectors then you should be able to do:
from ROOT import *
dR = jet.DeltaR(parton)

How to debug Python memory fault?

Edit: Really appreciate help in finding bug - but since it might prove hard to find/reproduce, any general debug help would be greatly appreciated too! Help me help myself! =)
Edit 2: Narrowing it down, commenting out code.
Edit 3: Seems lxml might not be the culprit, thanks! The full script is here. I need to go over it looking for references. What do they look like?
Edit 4: Actually, the scripts stops (goes 100%) in this, the parse_og part of it. So edit 3 is false - it must be lxml somehow.
Edit 5 MAJOR EDIT: As suggested by David Robinson and TankorSmash below, I've found a type of data content that will send lxml.etree.HTML( data ) in a wild loop. (I carelessly disregarded it, but find my sins redeemed as I've paid a price to the tune of an extra two days of debug! ;) A working crashing script is here. (Also opened a new question.)
Edit 6: Turns out this is a bug with lxml version 2.7.8 and below (at
least). Updated to lxml 2.9.0, and bug is gone. Thanks also to the fine folks over at this follow-up question.
I don't know how to debug this weird problem I'm having.
The below code runs fine for about five minutes, when the RAM is suddenly completely filled up (from 200MB to 1700MB during the 100% period - then when memory is full, it goes into blue wait state).
It's due to the code below, specifically the first two lines. That's for sure. But what is going on? What could possibly explain this behaviour?
def parse_og(self, data):
""" lxml parsing to the bone! """
try:
tree = etree.HTML( data ) # << break occurs on this line >>
m = tree.xpath("//meta[#property]")
#for i in m:
# y = i.attrib['property']
# x = i.attrib['content']
# # self.rj[y] = x # commented out in this example because code fails anyway
tree = ''
m = ''
x = ''
y = ''
i = ''
del tree
del m
del x
del y
del i
except Exception:
print 'lxml error: ', sys.exc_info()[1:3]
print len(data)
pass
You can try Low-level Python debugging with GDB. Probably there is a bug in Python interpreter or in lxml library and it is hard to find it without extra tools.
You can interrupt your script running under gdb when CPU usage goes to 100% and look at stack trace. It will probably help to understand what's going on inside script.
it must be due to some references which keep the documents alive. one must always be careful with string results from xpath evaluation. I see you have assigned None to tree and m but not to y,x and i .
Can you also assign None to y,x and i .
Tools are also helpful when trying to track down memory problems. I've found guppy to be a very useful Python memory profiling and exploration tool.
It is not the easiest to get started with due to a lack of good tutorials / documentation, but once you get to grips with it you will find it very useful. Features I make use of:
Remote memory profiling (via sockets)
Basic GUI for charting usage, optionally showing live data
Powerful, and consistent, interfaces for exploring data usage in a Python shell

Categories