Skip to content

API: Objectives

matmdl.objectives

Choose the objective function form. Currently only rmse exists.

best_rmse

Calculate the RMSE for the best parameter set.

Best parameters determined by objective function that wrote to out_errors.txt. Use root mean squared error so that error value is more interpretable.

calculate

calc_error(exp_data, orientation, sim_data=None)

Give error value for run compared to experimental data.

Calculates relative (%) root mean squared error between experimental and calculated stress-strain curves. Interpolation of experimental data depends on :ref:i_powerlaw.

Parameters:

Name Type Description Default
exp_data 'Nx2 matrix'

Array of experimental strain-stress, as from exp_data.data[orientation]['raw'].

required
orientation str

Orientation nickname.

required
Source code in matmdl/objectives/calculate.py
def calc_error(exp_data: "Nx2 matrix", orientation: str, sim_data=None) -> float:
	"""
	Give error value for run compared to experimental data.

	Calculates relative (%) root mean squared error between experimental and calculated
	stress-strain curves. Interpolation of experimental data depends on :ref:`i_powerlaw`.

	Args:
	    exp_data: Array of experimental strain-stress, as from
	        ``exp_data.data[orientation]['raw']``.
	    orientation: Orientation nickname.
	"""
	if sim_data is None:
		simSS = np.loadtxt(f"temp_time_disp_force_{orientation}.csv", delimiter=",", skiprows=1)[
			1:, 1:
		]
		# if loading force-displacement from file, normalize to engineering stress-strain:
		simSS[:, 0] = simSS[:, 0] / uset.length
		simSS[:, 1] = simSS[:, 1] / uset.area
	else:
		# TODO do checks for sim_data format/types
		simSS = sim_data
	# TODO get simulation dimensions at beginning of running this file, pass to this function

	expSS = deepcopy(exp_data)

	if uset.is_compression:
		expSS *= -1.0
		simSS *= -1.0

	# deal with unequal data lengths
	if simSS[-1, 0] > expSS[-1, 0]:
		# chop off simSS
		cutoff = np.where(simSS[:, 0] > expSS[-1, 0])[0][0] - 1
		simSS = simSS[:cutoff, :]
		cutoff_strain = simSS[-1, 0]
	elif simSS[-1, 0] < expSS[-1, 0]:
		# chop off expSS
		cutoff = np.where(simSS[-1, 0] < expSS[:, 0])[0][0] - 1
		expSS = expSS[:cutoff, :]
		cutoff_strain = expSS[-1, 0]
	else:
		cutoff_strain = simSS[-1, 0]
	begin_strain = max(min(expSS[:, 0]), min(simSS[:, 0]))

	def powerlaw(x, k, n):
		y = k * x**n
		return y

	def fit_powerlaw(x, y):
		popt, _ = curve_fit(powerlaw, x, y)
		return popt

	# interpolate points in both curves
	num_error_eval_pts = 1000
	x_error_eval_pts = np.linspace(begin_strain, cutoff_strain, num=num_error_eval_pts)
	interpSim = interp1d(simSS[:, 0], simSS[:, 1])
	if not uset.i_powerlaw:
		interpExp = interp1d(expSS[:, 0], expSS[:, 1])
	else:
		popt = fit_powerlaw(expSS[:, 0], expSS[:, 1])

		def interpExp(x):
			return powerlaw(x, *popt)

	# strictly limit to interpolation
	while x_error_eval_pts[-1] >= expSS[-1, 0]:
		x_error_eval_pts = np.delete(x_error_eval_pts, -1)

	# error function
	stress_error = _stress_diff(x_error_eval_pts, interpSim, interpExp)
	slope_error = _slope_diff(x_error_eval_pts, interpSim, interpExp)
	w = uset.slope_weight
	error = (1 - w) * stress_error + w * slope_error

	return error

ddx_pointwise(curve, x)

Give point-to-point slope values of curve over x

Source code in matmdl/objectives/calculate.py
def ddx_pointwise(curve, x):
	"""Give point-to-point slope values of curve over x"""
	return (curve(x[1:]) - curve(x[:-1])) / (x[1:] - x[:-1])

ddx_rolling(curve, x, window)

Give rolling window slope of curve

Source code in matmdl/objectives/calculate.py
def ddx_rolling(curve, x, window):
	"""Give rolling window slope of curve"""
	n = int(window)
	assert n > 1, "Rolling average requires window width of 2 or more points"
	num_windows = len(x) - window
	slopes = np.empty(num_windows)
	for i in range(num_windows):
		slopes[i] = np.polyfit(x[i : i + n], curve(x[i : i + n]), 1)[0]
	return slopes

recalculate

Recalculates error values based on saved stress-strain data.

Reloads *.npy files to recalculation of individual error values. Moves current out_errors.txt to dated filename before rewriting. Uses current error settings