4.5.5. OLGA-Fluent Coupling Examples

4.5.5.1. OLGA OPC Example Code

These examples are intended to demonstrate usage of the OLGA OPC Connector libraries to communicate and control an OLGA session with a running OLGA OPC Server. In order to work, the OLGA OPC Connector application must also be running on the OLGA machine (OLGA OPC Connector).

Establishing an OLGA Connection and Exception Handling

This example represents the simplest case of connecting to an OLGA session through the OLGA OPC Connector and requesting a status report from OLGA. It also illustrates a recommended approach for using try/except blocks for catching exceptions:

######################################################################
#                   OLGA OPC Server Version Report                   #  
######################################################################

import clr
import sys

# Load the DLLs that define the OLGA OPC connection classes and structures
sys.path.append('C:/Program Files/ANSYS Inc/v182/fluent/fluent18.2.0/addons/corba/DotNetFramework40')
clr.AddReferenceToFile('DotNetCoOpcUnit.dll')
clr.AddReferenceToFile('AasCoOpcUnit.dll')

# Import classes and structures
import AAS_CORBA
from AAS_CORBA.OPC import DotNetCoOpcServer
from AAS_CORBA.OPC import ICoOpcUnitError

try:
	# Instantiate a DotNetCoOpcServer object and use the ConnectToServerFromIorFile
	# method to connect using a provided keyfile
	iOPCServer=DotNetCoOpcServer()
	iOPCServer.ConnectToServerFromIorFile('C:/Work/OLGA-Coupling-Demo/aaS_OpcId.txt')

	# Request a status report and print the output
	print iOPCServer.getStatusReport()
except ICoOpcUnitError as error:
   # An exception was thrown by the OLGA OPC connector
	print "ICoOpcUnitError Exception caught:"
	print "\tCode       = ", error.m_iCode
	print "\tDescription = ",error.m_stringDescription
	print "\tInterface   = ",error.m_stringInterfaceName
	print "\tMore Info   = ",error.m_stringMoreInfo
	print "\tOperation   = ",error.m_stringOperation
	print "\tScope       = ",error.m_stringScope
except Exception as ex:
	print "Exception caught..."
	print ex.message
except:
	print "Unknown Exception..."

Creating and Manipulating Groups and Items in OLGA

This example creates an OPC item to act as a simple external clock for the OLGA simulation and increments the clock time. It demonstrates creation and manipulation of groups and items in OLGA. Such an external clock is a crucial component of coupled transient simulations.

######################################################################
#                   OLGA OPC Sample Clock Operations                 #  
######################################################################

import clr
import sys

from System.IO import File
sys.path.append('C:/Program Files/ANSYS Inc/v182/fluent/fluent18.2.0/addons/corba/DotNetFramework40')
clr.AddReferenceToFile('DotNetCoOpcUnit.dll')
clr.AddReferenceToFile('AasCoOpcUnit.dll')

import AAS_CORBA
from AAS_CORBA.OPC import DotNetCoOpcServer

try:
	iOPCServer=DotNetCoOpcServer()
	iOPCServer.ConnectToServerFromIorFile('C:/Work/OLGA-Coupling-Demo/aaS_OpcId.txt')
	
	# Add a Group and use the GetIOPCSyncIO method to instantiate a syncrhonous I/O object
	iOPCItemMgt=iOPCServer.AddGroup("WB")
	iOPCSyncIO=iOPCItemMgt.GetIOPCSyncIO()

	# Add the OPC items ExternalClock and INITTIME to the newly created WB group	
	ioSimServerDemoExternalClock            = iOPCItemMgt.AddItem('Sim.ServerDemo.ExternalClock')
	ioSimServerDemoINITTIME                 = iOPCItemMgt.AddItem('Sim.ServerDemo.INITTIME')

	# Use the synchronous I/O object to get a numerical and a string representation of the INITTIME value.
	stringInitTime=iOPCSyncIO.ReadStringifiedItem(ioSimServerDemoINITTIME.m_iOPCHandle)
	doubleInitTime=iOPCSyncIO.ReadItemAsDouble(ioSimServerDemoINITTIME.m_iOPCHandle)
	print 'Sim.ServerDemo.INITTIME:'
	print '\tStringified value:',stringInitTime
	print '\tNumerical value:',doubleInitTime, 'days'

	# Use the synchronous I/O object to get a numerical and a string representation of the ExternalClock value.
	stringExternalClock=iOPCSyncIO.ReadStringifiedItem(ioSimServerDemoExternalClock.m_iOPCHandle)
	doubleExternalClock=iOPCSyncIO.ReadItemAsDouble(ioSimServerDemoExternalClock.m_iOPCHandle)
	print 'Sim.ServerDemo.ExternalClock:'
	print '\tStringified value:',stringExternalClock
	print '\tNumerical value:',doubleExternalClock, ' days'

	#advance clock 1 day
	doubleExternalClock=doubleExternalClock+oneDay
	iOPCSyncIO.WriteItemAsDouble(ioSimServerDemoExternalClock.m_iOPCHandle,
		ioSimServerDemoExternalClock.m_iCOMType,doubleExternalClock)
	stringExternalClock=iOPCSyncIO.ReadStringifiedItem(ioSimServerDemoExternalClock.m_iOPCHandle)
	doubleExternalClock=iOPCSyncIO.ReadItemAsDouble(ioSimServerDemoExternalClock.m_iOPCHandle)
	print 'Sim.ServerDemo.ExternalClock advanced 1 day:'
	print '\tStringified value:',stringExternalClock
	print '\tNumerical value:',doubleExternalClock, ' days'

	# Remove the group
	iOPCServer.RemoveGroup(iOPCItemMgt)
except ICoOpcUnitError as error:
	print "ICoOpcUnitError Exception caught:"
	print "\tCode       = ", error.m_iCode
	print "\tDescription = ",error.m_stringDescription
	print "\tInterface   = ",error.m_stringInterfaceName
	print "\tMore Info   = ",error.m_stringMoreInfo
	print "\tOperation   = ",error.m_stringOperation
	print "\tScope       = ",error.m_stringScope
except Exception as ex:
	print "Exception caught..."
	print ex.message
except:
	print "Unknown Exception..."

Release 18.2 - © ANSYS, Inc. All rights reserved.