Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to import implicit_plot3d into .py file?

I'm working on some code for creating 3D molecular structures and everything seems to be working well aside from the fact that I have no idea how to import the 'implicit_plot3d' function into my script. I want to run the code as 'Molecule.py' directly inside of sage. When I try to run it right now I get the following error:

name 'implicit_plot3d' is not defined

Can anyone help me identify the issue? I'm not very experienced with either Python or Sage.

import sys
from sage.plot.plot3d.shapes import LineSegment, Sphere
from sage.all import rainbow

BC = ['white','black','blue','red','grey','red','pink','cyan']

Radii = {'O': 0.73, 'N': 0.75, 'C': 0.77, 'He': 0.32, 'H': 0.37, 'S': 1.02, 'Cl': 0.99, 
'F': 0.71, 'Xe': 1.30, 'Si': 1.11, 'B': 0.82, 'P': 1.06, 'Br': 1.14}

Valency = {'O': 6, 'N': 5, 'C': 4, 'He': 0, 'H': 1, 'S': 6, 'Cl': 7, 
'F': 7, 'Xe': 8, 'Si': 4, 'B': 3, 'P': 5, 'Br': 7}

Colors = {'O': 'red', 'N': 'green', 'C': 'black', 'He': 'cyan', 'H': 'white', 'S': 'yellow',
'Si': 'purple', 'Xe': 'pink', 'F': 'blue', 'Cl': 'green', 'B': 'pink', 'P': 'orange',
'Br': 'red'}

Here's a sample function

def bent_geometry():

    x = input("Enter bent molecule A: ")
    y = input("Enter bent molecule B: ")
    X =(0,0,0)
    Y1 = (0, 0.75, -0.70)
    Y2 = (0, -0.75,-0.70)

    C2_a = (0, 0, -1.5)
    C2_b = (0, 0, 1.5)

    XY2  = Sphere(Normalize(Radii[x]), color=Colors[x]).translate(X)
    XY2 += Sphere(Normalize(Radii[y]), color=Colors[y]).translate(Y1)
    XY2 += Sphere(Normalize(Radii[y]), color=Colors[y]).translate(Y2)

    XY2 += LineSegment(X, Y1, 1, color=BC[0], axes=False, frame=False)
    XY2 += LineSegment(X, Y2, 1, color=BC[0], axes=False, frame=False)
    XY2 += LineSegment(C2_a, C2_b, 1, color = BC[2], axes=True, frame=True)

    XY2 += implicit_plot3d(lambda x,y,z: x, (-1,1), (-1,1), (-1,1),color='cyan',opacity=0.8)
    XY2 += implicit_plot3d(lambda x,y,z: y, (-1,1), (-1,1), (-1,1),color='white',opacity=0.8)

    XY2 += text3d('C2',(0,0,1.575))
    return XY2

How to import implicit_plot3d into .py file?

I'm working on some code for creating 3D molecular structures and everything seems to be working well aside from the fact that I have no idea how to import the 'implicit_plot3d' function into my script. I want to run the code as 'Molecule.py' directly inside of sage. When I try to run it right now I get the following error:

NameError: name 'implicit_plot3d' is not defined

Can anyone help me identify the issue? I'm not very experienced with either Python or Sage.

import sys
from sage.plot.plot3d.shapes import LineSegment, Sphere
from sage.all import rainbow

BC = ['white','black','blue','red','grey','red','pink','cyan']

Radii = {'O': 0.73, 'N': 0.75, 'C': 0.77, 'He': 0.32, 'H': 0.37, 'S': 1.02, 'Cl': 0.99, 
'F': 0.71, 'Xe': 1.30, 'Si': 1.11, 'B': 0.82, 'P': 1.06, 'Br': 1.14}

Valency = {'O': 6, 'N': 5, 'C': 4, 'He': 0, 'H': 1, 'S': 6, 'Cl': 7, 
'F': 7, 'Xe': 8, 'Si': 4, 'B': 3, 'P': 5, 'Br': 7}

Colors = {'O': 'red', 'N': 'green', 'C': 'black', 'He': 'cyan', 'H': 'white', 'S': 'yellow',
'Si': 'purple', 'Xe': 'pink', 'F': 'blue', 'Cl': 'green', 'B': 'pink', 'P': 'orange',
'Br': 'red'}

Here's a sample function

def bent_geometry():

    x = input("Enter bent molecule A: ")
    y = input("Enter bent molecule B: ")
    X =(0,0,0)
    Y1 = (0, 0.75, -0.70)
    Y2 = (0, -0.75,-0.70)

    C2_a = (0, 0, -1.5)
    C2_b = (0, 0, 1.5)

    XY2  = Sphere(Normalize(Radii[x]), color=Colors[x]).translate(X)
    XY2 += Sphere(Normalize(Radii[y]), color=Colors[y]).translate(Y1)
    XY2 += Sphere(Normalize(Radii[y]), color=Colors[y]).translate(Y2)

    XY2 += LineSegment(X, Y1, 1, color=BC[0], axes=False, frame=False)
    XY2 += LineSegment(X, Y2, 1, color=BC[0], axes=False, frame=False)
    XY2 += LineSegment(C2_a, C2_b, 1, color = BC[2], axes=True, frame=True)

    XY2 += implicit_plot3d(lambda x,y,z: x, (-1,1), (-1,1), (-1,1),color='cyan',opacity=0.8)
    XY2 += implicit_plot3d(lambda x,y,z: y, (-1,1), (-1,1), (-1,1),color='white',opacity=0.8)

    XY2 += text3d('C2',(0,0,1.575))
    return XY2