function opt2dot(opt, filename) % Produce a GraphViz .dot file from an Optickle model. % The .dot format is documented here: % http://www.graphviz.org/Documentation/dotguide.pdf isPDF = 0; if nargin < 2 fid = 1; else if strcmpi(filename(end-3:end),'.pdf') isPDF = 1; pdffilename = filename; filename = [filename(1:end-4),'.dot']; end fid = fopen(filename, 'w'); end % suppress optickle's warnings, if optickle set a warning id, % we wouldn't need to set all off warning('off','all'); fprintf(fid, 'digraph G {\n'); % output the optics (graph nodes) for snOptic=1:opt.Noptic, optic = opt.optic{snOptic}; fprintf(fid, ' %s', optic.name); switch class(optic) case 'Source', P0=(optic.vArf((length(optic.vArf)+1)/2))^2; fprintf(fid, ' [shape=box,style=filled,fillcolor=grey,label="%s\\nP0=%.1f W"]',optic.name,P0); case 'RFmodulator', fprintf(fid, ' [shape=box,style=filled,fillcolor=orange,label="%s\\nfMod=%.2f MHz\\naMod=%.2f+%.2fi"]',optic.name,optic.fMod/1e6,real(optic.aMod),imag(optic.aMod)); case 'Mirror', fprintf(fid, ' [shape=box,style=filled,fillcolor=lightblue,label="%s\\nChr=1/%.1f m\\nThr=%.2f %% "]',optic.name,1/optic.Chr,optic.Thr*100); case 'BeamSplitter', fprintf(fid, ' [shape=box,style=filled,fillcolor=lightskyblue,label="%s\\nChr=1/%.1f m\\nThr=%.2f %%"]',optic.name,1/optic.Chr,optic.Thr*100); case 'Telescope', fprintf(fid, ' [shape=diamond,style=filled,fillcolor=violet,label="%s\\nf=%.1f m"]',optic.name,optic.f); case 'GouyPhase', fprintf(fid, ' [shape=hexagon,style=filled,fillcolor=yellow,label="%s"]',optic.name); case 'Sink', fprintf(fid, ' [shape=ellipse,style=filled,fillcolor=limegreen,label="%s\\nloss=%.2f"]',optic.name,optic.loss); end fprintf(fid, ';\n'); end % output the links (graph edges) for snLink=1:opt.Nlink, [src_name, src_sn, src_port] = getSourceName(opt, snLink); [snk_name, snk_sn, snk_port] = getSinkName(opt, snLink); fprintf(fid, ' %s -> %s [label="%s→%s\\n%.4f m",color=red];\n', ... getOpticName(opt, src_sn), ... getOpticName(opt, snk_sn), ... opt.optic{src_sn}.outNames{src_port}{1}, ... opt.optic{snk_sn}.inNames{snk_port}{1}, ... opt.link(snLink).len); end % output the probes for snProbe=1:opt.Nprobe, probe = opt.probe(snProbe); if probe.freq==0, fprintf(fid, ' %s [color=blue,label="%s\\nDC"]', probe.name,probe.name); else fprintf(fid, ' %s [color=blue,label="%s\\nfreq=%.2f MHz\\nphase=%.1f deg"]', probe.name,probe.name,probe.freq/1e6,probe.phase); end fprintf(fid, ';\n'); for snOptic=1:opt.Noptic, optic = opt.optic{snOptic}; try if optic.in(1)==probe.nField, fprintf(fid, ' %s -> %s [label="%s",color=red];\n',optic.name,probe.name,optic.inNames{1}{1}); break elseif optic.in(3)==probe.nField, fprintf(fid, ' %s -> %s [label="%s",color=red];\n',optic.name,probe.name,optic.inNames{3}{1}); break end end end end fprintf(fid, '}\n'); % now turn warnings back on warning('on','all'); if fid ~= 1 fclose(fid); end if isPDF system(['dot -Tpdf ',filename,' > ',pdffilename]); end