TypeError: can't multiply nsequence by non-int of type 'float' - calculation

I've written code to calculate for the voltage drop at much lower temperatures, but I can't seem to get past line 47 in my code. Specifically the line where I'm calculating tc right after calculating rca1.
'''
# =============================================================
# (22-05-11_0929T)
# Attempt at getting the values I need to figure the low end of the Celsius spectrum.
# Made by Mike Valero
#
# =============================================================
# Global Variables.
i = input("What is the amperage?") # in amps.
tc = input("What is the conductor temperature?") # arbitrary numerical for average conductor temperature in degrees C.
ta = input("What is the ambient temperature?") # ambient temperature in degrees C.
L1 = input("What is cable run distance?") # length of cable run we are doing.
rca = 0.0 # thermal resistance in thermal ohm feet.
rho = 10.09 # specific resistance at 20 degree C.
diam = 0.087 # the non-insulated diameter for Belden 27600.
L = 12.0 # approximate length of cable we care about. This is in inches as in 1 foot or 12 inches.
pc1 = 10.371 # circular mil oms per foot of conductor at 20 degrees C. (10.371 for 100% IACS copper)
tah = 234.5 # absolute value of inferred temperature of zero resistance. (234.5 degrees C for copper)
# ref1. multiplier for converting rdc to AC resistance
# and accounts for proximal heating effect from adjacent conductors.
Area = float(3.14 * (float(diam) / 2.0) ** 2.0) # approximate area that is seen for the cross-section of the cable.
rdc1 = rho * (L / Area) # resistance of one foot of conductor in ohms (micro-ohms).
rdc = ((1.02*pc1)/Area)*((tah-float(tc))/(tah+20.0))
rca1 = (float(tc)-float(ta))/(float(i)**2*rdc) # ref1.
tc = ((pc1*tah*rca1*float(i)*0.001*float(i)*0.001*1.02)+(ta*(tah+20.0)*(Area*0.000001)))/((tah+20.0)*(Area*0.000001)-(pc1*rca1*float(i)*0.001*float(i)*0.001*1.02))
k = 1.02*pc1*((tah+tc)/(tah+20.0))
vd = (2*k*float(L1)*float(i))/Area
print("Area of the cross-section of cable in inches:" + str(Area))
print("The resistance of one foot of conductor in ohms:" + str(rdc))
print("The new temperature for the conductor is:" + str(tc))
print("The k value is:" + str(k))
print("The voltage drop is:" + str(vd))
'''

Related

While executing the program, getting ValueError: math domain error

I am using a micropython library for collecting the data from the sensor MQ135.
The micropython code for the library is as below:
import math
import time
import machine
from machine import ADC
class MQ135(object):
""" Class for dealing with MQ13 Gas Sensors """
# The load resistance on the board
RLOAD = 10.0
# Calibration resistance at atmospheric CO2 level
RZERO = 76.63
# Parameters for calculating ppm of CO2 from sensor resistance
PARA = 116.6020682
PARB = 2.769034857
# Parameters to model temperature and humidity dependence
CORA = 0.00035
CORB = 0.02718
CORC = 1.39538
CORD = 0.0018
CORE = -0.003333333
CORF = -0.001923077
CORG = 1.130128205
# Atmospheric CO2 level for calibration purposes
ATMOCO2 = 397.13
def __init__(self, pin):
self.pin = pin
def get_correction_factor(self, temperature, humidity):
"""Calculates the correction factor for ambient air temperature and relative humidity
Based on the linearization of the temperature dependency curve
under and above 20 degrees Celsius, asuming a linear dependency on humidity,
provided by Balk77 https://github.com/GeorgK/MQ135/pull/6/files
"""
if temperature < 20:
return self.CORA * temperature * temperature - self.CORB * temperature + self.CORC - (humidity - 33.) * self.CORD
return self.CORE * temperature + self.CORF * humidity + self.CORG
def get_resistance(self):
"""Returns the resistance of the sensor in kOhms // -1 if not value got in pin"""
adc = machine.ADC(machine.Pin(self.pin)) # adc = ADC(self.pin)
value = adc.read()
if value == 0:
return -1
return (1023./value - 1.) * self.RLOAD
def get_corrected_resistance(self, temperature, humidity):
"""Gets the resistance of the sensor corrected for temperature/humidity"""
return self.get_resistance()/ self.get_correction_factor(temperature, humidity)
def get_ppm(self):
"""Returns the ppm of CO2 sensed (assuming only CO2 in the air)"""
return self.PARA * math.pow((self.get_resistance()/ self.RZERO), -self.PARB)
def get_corrected_ppm(self, temperature, humidity):
"""Returns the ppm of CO2 sensed (assuming only CO2 in the air)
corrected for temperature/humidity"""
return self.PARA * math.pow((self.get_corrected_resistance(temperature, humidity)/ self.RZERO), -self.PARB)
def get_rzero(self):
"""Returns the resistance RZero of the sensor (in kOhms) for calibratioin purposes"""
return self.get_resistance() * math.pow((self.ATMOCO2/self.PARA), (1./self.PARB))
def get_corrected_rzero(self, temperature, humidity):
"""Returns the resistance RZero of the sensor (in kOhms) for calibration purposes
corrected for temperature/humidity"""
return self.get_corrected_resistance(temperature, humidity) * math.pow((self.ATMOCO2/self.PARA), (1./self.PARB))
def mq135lib_example():
"""MQ135 lib example"""
# setup
temperature = 21.0
humidity = 25.0
mq135 = MQ135(36) # analog PIN 36
# loop
while True:
rzero = mq135.get_rzero()
corrected_rzero = mq135.get_corrected_rzero(temperature, humidity)
resistance = mq135.get_resistance()
ppm = mq135.get_ppm()
corrected_ppm = mq135.get_corrected_ppm(temperature, humidity)
print("MQ135 RZero: " + str(rzero) +"\t Corrected RZero: "+ str(corrected_rzero)+
"\t Resistance: "+ str(resistance) +"\t PPM: "+str(ppm)+
"\t Corrected PPM: "+str(corrected_ppm)+"ppm")
time.sleep(0.3)
if __name__ == "__main__":
mq135lib_example()
But when I executr this, I am getting the following error:
Traceback (most recent call last):
File "<stdin>", line 98, in <module>
File "<stdin>", line 89, in mq135lib_example
File "<stdin>", line 59, in get_ppm
ValueError: math domain error
I am not able to figure out what might be causing the problem. I am running this code in an ESp32.
How can this be resolved!
Any help is appreciated.
Thanks in advance.

Extracting avg. nodal stresses from odb file and nodeset using python

What I am trying to do:
I want to extract nodal displacements (U2) and stresses (S22), which have been transformed into a local coordinate system, in order to animate their variation with each frame of a given analysis step.
Extracting history outputs and the nodal displacement field is OK. However, getting the stresses seems more cumbersome.
Animation
Problems:
The NODAL specified stress field is not node averaged. How do I obtain these?
The nodal stress output seems a bit random even though it has been specified through subsetregion = nodeSet. It looks like I am only accessing half of the non-averaged nodal stresses along the path nodeset which I have defined.
I have posted the entire script, but where it goes wrong is from the for loop over frames in the step: for frameNo in range(0,nFrames):
Any input or ideas would be greatly appreciated.
odb = openOdb(path = "PostJob.odb")
partName = odb.rootAssembly.instances.keys()
print('AVAILABLE PARTS AND PART NAMES IN THE MODEL', partName)
nSets = len(odb.rootAssembly.instances[partName[0]].nodeSets.keys())
nSetKeys = odb.rootAssembly.instances[partName[0]].nodeSets.keys()
print('AVAILABLE SETS IN THE PART ',partName[0],'sets',nSetKeys)
print('NO. OF SETS IN PART INSTANCE',nSets)
# ------------------------------------------------------------------------
# FORCE-DISPLACEMENT CURVES ----------------------------------------------
# ------------------------------------------------------------------------
# Extract U1 and RF1 from the history output
U1data = odb.steps['CZ Analysis'].historyRegions['Node ASSEMBLY.1'].historyOutputs['U1'].data
RF1data = odb.steps['CZ Analysis'].historyRegions['Node ASSEMBLY.1'].historyOutputs['RF1'].data
# The data is in the form of tuples, which we will convert to np. arrays instead.
U1data=np.asarray(U1data)
RF1data = np.array(RF1data)
# Plotting the force-displacement curve
plt.plot(U1data[:,1],RF1data[:,1])
plt.ylabel('Force: RF1 in [N]')
plt.xlabel('Displacement: U1 in [mm]')
plt.xlim((0,2))
plt.title('Force-Displacement Curve')
#plt.show()
# ------------------------------------------------------------------------
# INTERFACE TRACTIONS ----------------------------------------------------
# ------------------------------------------------------------------------
# Access the test node path and create a node path in the visulalization module
alpha = 10.0*math.pi/180 # with X-axis
# Perform a coordinate transformation on the nodal coordinates by using the transformation matrix T
R = np.array([[math.cos(alpha),-math.sin(alpha),0],
[math.sin(alpha), math.cos(alpha),0],
[ 0, 0,1]])
# T = [cos alpha, -sin alpha, 0; sin alpha, cos alpha, 0; 0, 0, 1]
print(R)
e1 = np.array([1,0,0])
e2 = np.array([0,1,0])
e3 = np.array([0,0,1])
e1p = np.dot(R, e1.transpose())
e2p = np.dot(R, e2.transpose())
e3p = np.dot(R, e3.transpose())
origin=[0,0,0]
#plt.figure()
origin = np.array([0, 0, 0])
#plt.quiver([e1p[0],e2p[0],e3p[0]],[e1p[1],e2p[1],e3p[1]], color=['r','g','b'],scale=21)
# Create a new coordinate system
coordSys = odb.rootAssembly.DatumCsysByThreePoints(name='Bond-CSYS', coordSysType=CARTESIAN,
origin=(0.0,0.0,0.0),
point1=tuple(e1p),
point2=tuple(e2p))
# Extract the node set and stress field of the node path.
instance = odb.rootAssembly.instances['ADHESIVE']
bondPath = instance.nodeSets['MIDNODESET']
allBondPathNodes = bondPath.nodes[0:-1]
nNodes = len(bondPath.nodes)
print('NUMBER OF NODES IN BONDLINE PATH FOUND FROM MIDNODESET', nNodes)
nodeCoord = np.zeros([3,nNodes])
# Transform the coordinates into the local coordinates of the bondline
jj = -1
for node in allBondPathNodes:
jj += 1
nodeCoord[0,jj] = node.coordinates[0]
nodeCoord[1,jj] = node.coordinates[1]
nodeCoord[2,jj] = node.coordinates[2]
# plt.figure()
# plt.plot(nodeCoord[0,:],nodeCoord[1,:])
# Transform the coordinates and determine the
transNodeCoord = np.dot(np.linalg.inv(R),nodeCoord)
# Test coordinates by plotting the line
#plt.plot(transNodeCoord[0,:],transNodeCoord[1,:])
# EXTRACT RELEVANT FIELDS ------
# Create a for loop to loop over the number of frames in the odb:
step = odb.steps['CZ Analysis']
nFrames = len(step.frames)
print("No. of Frames", nFrames)
# Initialize arrays outside for loop
U11 = np.zeros([nNodes,nFrames])
U22 = np.zeros([nNodes,nFrames])
t11 = np.zeros([nNodes,nFrames])
t22 = np.zeros([nNodes,nFrames])
t12 = np.zeros([nNodes,nFrames])
for frameNo in range(0,nFrames):
# Current frame no in analysis step 'CZ Analysis'
frame = step.frames[frameNo]
# Get the stress and displacement field for the current frame
stressField = frame.fieldOutputs['S']
displacement = frame.fieldOutputs['U']
# Transform the displacement and stress field to local coordinate system
transfStressField = stressField.getTransformedField(datumCsys=coordSys)
transfDisplacementField = displacement.getTransformedField(datumCsys = coordSys)
# Extract nodal field results for subset region = bondPath
pathStressField = transfStressField.getSubset(region=bondPath,position=NODAL)
pathDisplacementField = transfDisplacementField.getSubset(region=bondPath)
# Extract displacement field U1 component
dispU1 = pathDisplacementField.getScalarField(componentLabel='U1')
dispU2 = pathDisplacementField.getScalarField(componentLabel='U2')
# Loop over nodal values and append them to np.array
jj = -1
for u,v, stress in zip(dispU1.values, dispU2.values,transfStressField.values):
jj += 1
U11[jj][frameNo] = u.data
U22[jj][frameNo] = v.data
t11[jj][frameNo] = stress.data[0]
t22[jj][frameNo] = stress.data[1]
t12[jj][frameNo] = stress.data[3]
#print('Displacement node label', u.nodeLabel,v.nodeLabel,'Stress object node label', stress.nodeLabel)
#print("Node Label", v.nodeLabel, "Displacement Value", v.data,"S11",stress.data[0], "S22",stress.data[1],"S12",stress.data[3])
#print('POSITION: ', v.position, "Coordinate", node.coordinates, "Displacement Value", v.data)
fig, axs = plt.subplots(2)
axs[1].set_xlabel('x-coordinate')
def animate(i):
axs[0].clear()
axs[1].clear()
#ax.plot(transNodeCoord[0,:],t22[:,i]) # stresses are discontinous because they are specified at nodes
#ax.set_title('Displacement: U2', 'Frame No:',i)
axs[0].set_title('Displacement: U2 , Frame No: {}'.format(i))
axs[0].plot(np.flip(transNodeCoord[0,:]),U22[:,i]) # Displacements are continous, but look weird.-
axs[1].set_title('Traction: T22 , Frame No: {}'.format(i))
axs[1].plot(np.flip(transNodeCoord[0,:]),t22[:,i])
#ax.set_xlim([0,transNodeCoord[-1,:]])
# Create animation
ani1 = FuncAnimation(fig,animate,frames=32,interval=500,blit=False,repeat=True)
np.set_printoptions(threshold=np.inf)
Abaqus calculates stress at integration points located inside the 3D element (number and position depend on the element type). After the stress is calculated it is extrapolated to the nodes of the element.
Thus, each node would get a value from each element sharing that node. If the meshing is not fine enough such an interpolation could result in high differences in node values obtained from adjacent elements. Abaqus by default averages these values for visualization purposes.
For your task, you can export values at nodes and perform averaging by yourself. Or use results obtained at the element centroid/at integration points and pair these values with the corresponding coordinate. After obtaining Streess vs X-coordinate and Strain vs X-coordinate data sets you can interplate first or second so X-coordinates of both are matching.

CFA in data with 3 levels - estimating factor scores at level 2?

I am working on a dataset with 3 levels:
Teacher
School
Country
Using survey responses from the teachers, the aim is to use Confirmatory Factor Analysis (CFA) with the ultimate goal of having the factor scores at the school level.
A further objective is to test for measurement invariance across countries.
I want to use the lavaan package in R, because it is able to deal with the complex survey design of my data trough the lavaan.survey-extension (sampling design, weights etc.)
I have done some preliminary analysis, where i use country-ID as the group argument in the cfa-function. This gives me the possibility to perform the measurement invariance analysis across countries. The issue is, that my factor scores are given at individual teacher level, and i am interested in the school-level.
Any ideas about how to get these factor scores at the school level?
Here are some examples of the functions i use. I do not think that i data sample is needed, but i will create some if it is requested.
library(lavaan)
library(SEMtools)
#define model
reduced_mod <-'
leadership_sup =~ TC3G22D + TC3G22E + TC3G22K
continous_develop_collab =~ TT3G32A + TT3G32B + TT3G32C + TT3G32D '
#Fit model with different restraints:
fit_no_restraint <- cfa(model = reduced_mod, data = cfa_data, group="countryID")
fit_metric <- cfa(model = reduced_mod, data = cfa_data, group="countryID", group.equal = c("loadings"))
fit_scalar <- cfa(model = reduced_mod, data = cfa_data, group="countryID", group.equal = c("loadings", "intercepts"))
#Compare fit statistics
compareFit(scalar = fit_scalar , metric = fit_metric , config = fit_no_restraint)
It seems that you want multilevel measurement invariance. You should use the measEq.syntax() from the semTools package:
## ---------------------
## Multilevel Invariance
## ---------------------
## To test invariance across levels in a MLSEM, specify syntax as though
## you are fitting to 2 groups instead of 2 levels.
mlsem <- ' f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6 '
## metric invariance
syntax.metric <- measEq.syntax(configural.model = mlsem, meanstructure = TRUE,
ID.fac = "std.lv", sample.nobs = c(1, 1),
group = "cluster", group.equal = "loadings")
## by definition, Level-1 means must be zero, so fix them
syntax.metric <- update(syntax.metric,
change.syntax = paste0("y", 1:6, " ~ c(0, NA)*1"))
## save as a character string
mod.metric <- as.character(syntax.metric, groups.as.blocks = TRUE)
## convert from multigroup to multilevel
mod.metric <- gsub(pattern = "group:", replacement = "level:",
x = mod.metric, fixed = TRUE)
## fit model to data
fit.metric <- lavaan(mod.metric, data = Demo.twolevel, cluster = "cluster")
summary(fit.metric)
Source

Weka IBk parameter details (distanceWeighting, meanSquared)

I am using kNN algorithm to classify. In weka they have provided various parameter setting for kNN. I am intersted to know about the distanceWeighting, meanSquared.
In distanceWeighting we have three values (No distance weighting, weight by 1/distance and weight by 1-distance). What are these values and what is their impact?
Can someone please expalin me? :)
If one uses "no distance weighting", then the predicted value for your data points is the average of all k neighbors. For example
# if values_of_3_neigbors = 4, 5, 6
# then predicted_value = (4+5+6)/3 = 5
For 1/distance weighting, the weight of each neigbor is inversely proportional to the distance to it. The idea is: the closer the neighbor, the more it influences the predicted value. For example
# distance_to_3_neigbors = 1,3,5
# weights_of_neighbors = 1/1, 1/3, 1/5 # sum = 1 + 0.33 + 0.2 = 1.53
# normalized_weights_of_neighbors = 1/1.53, 0.33/1.53, 0.2/1.53 = 0.654, 0.216, 0.131
# then predicted_values = 4*0.654 + 5*0.216 + 6*0.131 = 4.48
For 1-distance it is similar. This is only applicable when all your distances are in the [0,1] range.
Hope this helps

Emgu CV - Anisotropic Diffusion

Can anybody guide me to some existing implementations of anisotropic diffusion, preferably the perona-malik diffusion?
translate the following MATLAB code :
% pm2.m - Anisotropic Diffusion routines
function ZN = pm2(ZN,K,iterate);
[m,n] = size(ZN);
% lambda = 0.250;
lambda = .025;
%K=16;
rowC = [1:m]; rowN = [1 1:m-1]; rowS = [2:m m];
colC = [1:n]; colE = [2:n n]; colW = [1 1:n-1];
result_save=0;
for i = 1:iterate,
%i;
% result=PSNR(Z,ZN);
% if result>result_save
% result_save=result;
% else
% break;
% end
deltaN = ZN(rowN,colC) - ZN(rowC,colC);
deltaS = ZN(rowS,colC) - ZN(rowC,colC);
deltaE = ZN(rowC,colE) - ZN(rowC,colC);
deltaW = ZN(rowC,colW) - ZN(rowC,colC);
% deltaN = deltaN .*abs(deltaN<K);
% deltaS = deltaS .*abs(deltaS<K);
% deltaE = deltaE .*abs(deltaE<K);
% deltaW = deltaW .*abs(deltaW<K);
fluxN = deltaN .* exp(-((abs(deltaN) ./ K).^2) );
fluxS = deltaS .* exp(-((abs(deltaS) ./ K).^2) );
fluxE = deltaE .* exp(-((abs(deltaE) ./ K).^2) );
fluxW = deltaW .* exp(-((abs(deltaW) ./ K).^2) );
ZN = ZN + lambda*(fluxN +fluxS + fluxE + fluxW);
%ZN=max(0,ZN);ZN=min(255,ZN);
end
the code is not mine and has been taken from: http://www.csee.wvu.edu/~xinl/code/pm2.m
OpenCV Implementation (It needs 3 channel image):
from cv2.ximgproc import anisotropicDiffusion
ultrasound_ad_cv2 = anisotropicDiffusion(im,0.075 ,80, 100)
Juxtapose comparison
From scratch in Python: (For grayscale image only)
import scipy.ndimage.filters as flt
import numpy as np
import warnings
def anisodiff(img,niter=1,kappa=50,gamma=0.1,step=(1.,1.),sigma=0, option=1,ploton=False):
"""
Anisotropic diffusion.
Usage:
imgout = anisodiff(im, niter, kappa, gamma, option)
Arguments:
img - input image
niter - number of iterations
kappa - conduction coefficient 20-100 ?
gamma - max value of .25 for stability
step - tuple, the distance between adjacent pixels in (y,x)
option - 1 Perona Malik diffusion equation No 1
2 Perona Malik diffusion equation No 2
ploton - if True, the image will be plotted on every iteration
Returns:
imgout - diffused image.
kappa controls conduction as a function of gradient. If kappa is low
small intensity gradients are able to block conduction and hence diffusion
across step edges. A large value reduces the influence of intensity
gradients on conduction.
gamma controls speed of diffusion (you usually want it at a maximum of
0.25)
step is used to scale the gradients in case the spacing between adjacent
pixels differs in the x and y axes
Diffusion equation 1 favours high contrast edges over low contrast ones.
Diffusion equation 2 favours wide regions over smaller ones.
"""
# ...you could always diffuse each color channel independently if you
# really want
if img.ndim == 3:
warnings.warn("Only grayscale images allowed, converting to 2D matrix")
img = img.mean(2)
# initialize output array
img = img.astype('float32')
imgout = img.copy()
# initialize some internal variables
deltaS = np.zeros_like(imgout)
deltaE = deltaS.copy()
NS = deltaS.copy()
EW = deltaS.copy()
gS = np.ones_like(imgout)
gE = gS.copy()
# create the plot figure, if requested
if ploton:
import pylab as pl
from time import sleep
fig = pl.figure(figsize=(20,5.5),num="Anisotropic diffusion")
ax1,ax2 = fig.add_subplot(1,2,1),fig.add_subplot(1,2,2)
ax1.imshow(img,interpolation='nearest')
ih = ax2.imshow(imgout,interpolation='nearest',animated=True)
ax1.set_title("Original image")
ax2.set_title("Iteration 0")
fig.canvas.draw()
for ii in np.arange(1,niter):
# calculate the diffs
deltaS[:-1,: ] = np.diff(imgout,axis=0)
deltaE[: ,:-1] = np.diff(imgout,axis=1)
if 0<sigma:
deltaSf=flt.gaussian_filter(deltaS,sigma);
deltaEf=flt.gaussian_filter(deltaE,sigma);
else:
deltaSf=deltaS;
deltaEf=deltaE;
# conduction gradients (only need to compute one per dim!)
if option == 1:
gS = np.exp(-(deltaSf/kappa)**2.)/step[0]
gE = np.exp(-(deltaEf/kappa)**2.)/step[1]
elif option == 2:
gS = 1./(1.+(deltaSf/kappa)**2.)/step[0]
gE = 1./(1.+(deltaEf/kappa)**2.)/step[1]
# update matrices
E = gE*deltaE
S = gS*deltaS
# subtract a copy that has been shifted 'North/West' by one
# pixel. don't as questions. just do it. trust me.
NS[:] = S
EW[:] = E
NS[1:,:] -= S[:-1,:]
EW[:,1:] -= E[:,:-1]
# update the image
imgout += gamma*(NS+EW)
if ploton:
iterstring = "Iteration %i" %(ii+1)
ih.set_data(imgout)
ax2.set_title(iterstring)
fig.canvas.draw()
# sleep(0.01)
return imgout
Usage
:
#anisodiff(img,niter=1,kappa=50,gamma=0.1,step=(1.,1.),sigma=0, option=1,ploton=False)
us_im_ad = anisodiff(ultrasound,100,80,0.075,(1,1),2.5,1)
Source
Juxtapose comparison

Resources