Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation of object-oriented optical element class in python optical simulation?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Python optical simulation object-oriented optical element class implementation is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Optical element class

The planar mirror is a very simple model because we only need to consider one plane. But other optical elements may become a little complicated: we have to consider the behavior of light on the incident and outgoing planes.

This is nonsense, of course, and we have a preliminary solution: split the optical element into the front surface and the rear surface. If light needs to be reflected multiple times in the optical element, the optical element can be divided into the number of surfaces that need to be reflected, perfect and brainless.

This shows that we are familiar with the thinking of programmers, our view of the world is no longer a WYSIWYG world, we see the performance of abstract components. But don't panic, there may not be a big difference between programmers and normal people, because we can not only disassemble the world, but also reconstruct the disassembled parts back to the world.

Try to think about the problem more complicated. there are many optical elements in the optical system, and the light will pass through each optical element many times, and each incident point and exit point will have a certain deviation. Because the optical element may absorb the energy of light, thus causing heat. And each time the incident point and exit point are different, the heating position is also different. Because the heat will lead to the deformation of the optical element, the action of the light and the optical element will also change next time.

In other words, for each optical element, there are not only fixed front surface and rear surface, but also constantly changing parameters such as incident point, exit point, heating, deformation and so on. Such a too practical problem urges us to construct a more realistic data type, in other words, we want to create an object that can encapsulate various variables and functions, and we enter a parameter. the state of the object will also change.

This is called object-oriented.

Class Opti (): def _ _ init__ (self,edge1,edge2): self.edge1 = edge1 self.edge2 = edge2

In the above example, we define a class of optical elements that have two surfaces, which can be either flat or curved. In this way, we have created a class. Where _ _ init__ is the initialization method and self represents the class itself that we created. In general, if a method in a class does not have a modifier, self must be taken as the first parameter.

Self.edge1 indicates that there is a member of the Opti class whose name is edge1. When this class is initialized, we can assign it.

Some elements may have only one surface, such as a full mirror, and some may have multiple surfaces, such as a polarized cube. Moreover, when we do the experiment, we also need to compare different optical elements in order to get the best experimental results. So, what should we do if we want to change the optical elements that have been built?

In fact, it is very simple, as long as you add a method so that you can insert or delete new surfaces.

# File Opti.pyclass Opti (): def _ _ init__ (self,edges= [[(0mairel 1), (0jue 1)], [(0je 1), (0mai Lay 1), (1pm 2m 0)]]): self.edges = [{'index':i,'dots':edges [I]} for i in range (len (edges))] # edge format is (dot1,dot2,...) Def insertEdge (self,edge,albedo=0): self.edges.append ({'index':len (self.edges),' dots':edge}) # acceptable number and point set def delEdge (self,edge): try: if isinstance (edge) List): # if the type of edge is list for edg in self.edges: if edg ['dots'] = = edge: edge = edg [' index'] del self.edges [edge] except: print ("no this edge")

In the above code, you can see that the initialization function is preset with some values, which is no different from the ordinary function. We can see that the two surfaces inserted by default are the plane [(0memmel), (0Magne1)] and the arc surface [(0L1), (0memly1), (1comp2)]. It can be seen that a flat-convex mirror is generated by default.

The member variable self.edges is the list of optical surfaces, and each optical surface has two parameters, namely index index and point set dots. From the previous optical abstraction, when there are two points in the point pair, it represents the plane; when there are three points, it represents the arc surface.

Method insertEdge is to insert an optical surface, where it is numbered as the index number of the optical surface in self.edges; delEdge, as its name implies, deletes an optical surface. If the edge passed in is a list, the surface passed in is a parameterized surface, and the surface is found by traversing self.edges and its index is obtained.

If the parameter passed in is a single value, it means that the parameter passed in is an index number, so you can delete it directly.

In this method, a new code block, try:...except..., is used, which is an exception mechanism that tries to run the code in the try: block, and if it fails, executes except. If we fail to execute the delEdge successfully, it means that the surface of our input is not in this optical element, so we output "no this edge".

This seems to be the first time I have seen the command print. Generally speaking, this should be the first function to come into contact with. After all, for most programmers, the first line of code typed is

Print ("hello world") print ('hello world')

At the same time, in addition to the numerical type, we also know another data type, that is, characters. In python, you can use double or single quotation marks to represent a single character or string. That is, in the above hello world code, both lines are correct, and there is no difference.

Now that we have written a class, we can create an object and enter it on the command line:

> from Opti import Opti > Opti.__name__ # what the heck is this? 'Opti' > x = Opti () # create an object because no parameters have been entered Therefore, they are all default values > x.edges # real class members [{'index': 0,' dots': [(0,-1), (0, 1)]}, {'index': 1,' dots': [(0,1), (0,-1), (0.5,0)]} > > x.delEdge (1) # calling class methods > x.edges # without an edge [{'index': 0]. 'dots': [(0,-1), (0, 1)]}] > x.delEdge (1) # it is impossible to delete an edge that does not exist no this edge >

First of all, the two Opti of from Opti import Opti are not the same. The former represents the package `Opti.py', and the latter represents the class' Opti',import in Opti.py. Then you can call it.

Then there is a more paradoxical thing, we do not define _ _ name__, in the class, but a value is generated after the call.

Don't panic, it's an old acquaintance. You can think of _ _ name__ as a built-in property within python, and when we execute a .py file directly, the value of this _ _ name__ is _ _ main__, otherwise it is the name of the class. So, by this time, we seem to be able to understand the true meaning of the entry function.

Keep going down, and almost everything will be as expected.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report